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

#pretty_print, #to_json

Constructor Details

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

Returns a new instance of Paren.



6722
6723
6724
6725
6726
6727
# File 'lib/syntax_tree/node.rb', line 6722

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



6720
6721
6722
# File 'lib/syntax_tree/node.rb', line 6720

def comments
  @comments
end

#contents ⇒ Object (readonly)

[nil | untyped] the expression inside the parentheses



6717
6718
6719
# File 'lib/syntax_tree/node.rb', line 6717

def contents
  @contents
end

#lparen ⇒ Object (readonly)

[LParen] the left parenthesis that opened this statement



6714
6715
6716
# File 'lib/syntax_tree/node.rb', line 6714

def lparen
  @lparen
end

Instance Method Details

#accept(visitor) ⇒ Object



6729
6730
6731
# File 'lib/syntax_tree/node.rb', line 6729

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

#child_nodes ⇒ Object Also known as: deconstruct



6733
6734
6735
# File 'lib/syntax_tree/node.rb', line 6733

def child_nodes
  [lparen, contents]
end

#deconstruct_keys(keys) ⇒ Object



6739
6740
6741
6742
6743
6744
6745
6746
# File 'lib/syntax_tree/node.rb', line 6739

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

#format(q) ⇒ Object



6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
# File 'lib/syntax_tree/node.rb', line 6748

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