Class: SyntaxTree::MLHSParen

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

Overview

MLHSParen represents parentheses being used to destruct values in a multiple assignment on the left hand side.

(left, right) = value

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(contents:, comma: false, location:) ⇒ MLHSParen

Returns a new instance of MLHSParen.



7599
7600
7601
7602
7603
7604
# File 'lib/syntax_tree/node.rb', line 7599

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

Instance Attribute Details

#commaObject

boolean

whether or not there is a trailing comma at the end of this

list, which impacts destructuring. It’s an attr_accessor so that while the syntax tree is being built it can be set by its parent node



7594
7595
7596
# File 'lib/syntax_tree/node.rb', line 7594

def comma
  @comma
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7597
7598
7599
# File 'lib/syntax_tree/node.rb', line 7597

def comments
  @comments
end

#contentsObject (readonly)

MLHS | MLHSParen

the contents inside of the parentheses



7589
7590
7591
# File 'lib/syntax_tree/node.rb', line 7589

def contents
  @contents
end

Instance Method Details

#===(other) ⇒ Object



7652
7653
7654
# File 'lib/syntax_tree/node.rb', line 7652

def ===(other)
  other.is_a?(MLHSParen) && contents === other.contents
end

#accept(visitor) ⇒ Object



7606
7607
7608
# File 'lib/syntax_tree/node.rb', line 7606

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

#child_nodesObject Also known as: deconstruct



7610
7611
7612
# File 'lib/syntax_tree/node.rb', line 7610

def child_nodes
  [contents]
end

#copy(contents: nil, location: nil) ⇒ Object



7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
# File 'lib/syntax_tree/node.rb', line 7614

def copy(contents: nil, location: nil)
  node =
    MLHSParen.new(
      contents: contents || self.contents,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



7627
7628
7629
# File 'lib/syntax_tree/node.rb', line 7627

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

#format(q) ⇒ Object



7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
# File 'lib/syntax_tree/node.rb', line 7631

def format(q)
  parent = q.parent

  if parent.is_a?(MAssign) || parent.is_a?(MLHSParen)
    q.format(contents)
    q.text(",") if comma
  else
    q.text("(")
    q.group do
      q.indent do
        q.breakable_empty
        q.format(contents)
      end

      q.text(",") if comma
      q.breakable_empty
    end
    q.text(")")
  end
end