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



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

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



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

def comments
  @comments
end

#contentsObject (readonly)

nil | untyped

the expression inside the parentheses



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

def contents
  @contents
end

#lparenObject (readonly)

LParen

the left parenthesis that opened this statement



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

def lparen
  @lparen
end

Instance Method Details

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [lparen, contents]
end

#deconstruct_keys(keys) ⇒ Object



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

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

#format(q) ⇒ Object



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

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