Class: SyntaxTree::MLHS

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

Overview

MLHS represents a list of values being destructured on the left-hand side of a multiple assignment.

first, second, third = value

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(parts:, comma: false, location:) ⇒ MLHS

Returns a new instance of MLHS.



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

def initialize(parts:, comma: false, location:)
  @parts = parts
  @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



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

def comma
  @comma
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



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

def comments
  @comments
end

#partsObject (readonly)

[

Array[
  ARefField | ArgStar | ConstPathField | Field | Ident | MLHSParen |
    TopConstField | VarField
]

] the parts of the left-hand side of a multiple assignment



7604
7605
7606
# File 'lib/syntax_tree/node.rb', line 7604

def parts
  @parts
end

Instance Method Details

#===(other) ⇒ Object



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

def ===(other)
  other.is_a?(MLHS) && ArrayMatch.call(parts, other.parts) &&
    comma === other.comma
end

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



7625
7626
7627
# File 'lib/syntax_tree/node.rb', line 7625

def child_nodes
  parts
end

#copy(parts: nil, location: nil, comma: nil) ⇒ Object



7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
# File 'lib/syntax_tree/node.rb', line 7629

def copy(parts: nil, location: nil, comma: nil)
  node =
    MLHS.new(
      parts: parts || self.parts,
      location: location || self.location,
      comma: comma || self.comma
    )

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

#deconstruct_keys(_keys) ⇒ Object



7643
7644
7645
# File 'lib/syntax_tree/node.rb', line 7643

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

#format(q) ⇒ Object



7647
7648
7649
7650
# File 'lib/syntax_tree/node.rb', line 7647

def format(q)
  q.seplist(parts) { |part| q.format(part) }
  q.text(",") if comma
end