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



7611
7612
7613
7614
7615
7616
# File 'lib/syntax_tree/node.rb', line 7611

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



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

def comma
  @comma
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



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

def comments
  @comments
end

#contentsObject (readonly)

MLHS | MLHSParen

the contents inside of the parentheses



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

def contents
  @contents
end

Instance Method Details

#===(other) ⇒ Object



7664
7665
7666
# File 'lib/syntax_tree/node.rb', line 7664

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

#accept(visitor) ⇒ Object



7618
7619
7620
# File 'lib/syntax_tree/node.rb', line 7618

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

#child_nodesObject Also known as: deconstruct



7622
7623
7624
# File 'lib/syntax_tree/node.rb', line 7622

def child_nodes
  [contents]
end

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



7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
# File 'lib/syntax_tree/node.rb', line 7626

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



7639
7640
7641
# File 'lib/syntax_tree/node.rb', line 7639

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

#format(q) ⇒ Object



7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
# File 'lib/syntax_tree/node.rb', line 7643

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