Class: SyntaxTree::Paren

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

Paren represents using balanced parentheses in a couple places in a Ruby program. In general parentheses can be used anywhere a Ruby expression can be used.

(1 + 2)

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(lparen:, contents:, location:, comments: []) ⇒ Paren

Returns a new instance of Paren.



6944
6945
6946
6947
6948
6949
# File 'lib/syntax_tree/node.rb', line 6944

def initialize(lparen:, contents:, location:, comments: [])
  @lparen = lparen
  @contents = contents
  @location = location
  @comments = comments
end

Instance Attribute Details

#comments ⇒ Object (readonly)

[Array[ Comment | EmbDoc ]] the comments attached to this node



6942
6943
6944
# File 'lib/syntax_tree/node.rb', line 6942

def comments
  @comments
end

#contents ⇒ Object (readonly)

[nil | untyped] the expression inside the parentheses



6939
6940
6941
# File 'lib/syntax_tree/node.rb', line 6939

def contents
  @contents
end

#lparen ⇒ Object (readonly)

[LParen] the left parenthesis that opened this statement



6936
6937
6938
# File 'lib/syntax_tree/node.rb', line 6936

def lparen
  @lparen
end

Instance Method Details

#accept(visitor) ⇒ Object



6951
6952
6953
# File 'lib/syntax_tree/node.rb', line 6951

def accept(visitor)
  visitor.visit_paren(self)
end

#child_nodes ⇒ Object Also known as: deconstruct



6955
6956
6957
# File 'lib/syntax_tree/node.rb', line 6955

def child_nodes
  [lparen, contents]
end

#deconstruct_keys(_keys) ⇒ Object



6961
6962
6963
6964
6965
6966
6967
6968
# File 'lib/syntax_tree/node.rb', line 6961

def deconstruct_keys(_keys)
  {
    lparen: lparen,
    contents: contents,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
# File 'lib/syntax_tree/node.rb', line 6970

def format(q)
  q.group do
    q.format(lparen)

    if contents && (!contents.is_a?(Params) || !contents.empty?)
      q.indent do
        q.breakable("")
        q.format(contents)
      end
    end

    q.breakable("")
    q.text(")")
  end
end