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:, location:, comma: false) ⇒ MLHS

Returns a new instance of MLHS.



7671
7672
7673
7674
7675
7676
# File 'lib/syntax_tree/node.rb', line 7671

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



7666
7667
7668
# File 'lib/syntax_tree/node.rb', line 7666

def comma
  @comma
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7669
7670
7671
# File 'lib/syntax_tree/node.rb', line 7669

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



7661
7662
7663
# File 'lib/syntax_tree/node.rb', line 7661

def parts
  @parts
end

Instance Method Details

#===(other) ⇒ Object



7709
7710
7711
7712
# File 'lib/syntax_tree/node.rb', line 7709

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

#accept(visitor) ⇒ Object



7678
7679
7680
# File 'lib/syntax_tree/node.rb', line 7678

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

#child_nodesObject Also known as: deconstruct



7682
7683
7684
# File 'lib/syntax_tree/node.rb', line 7682

def child_nodes
  parts
end

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



7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
# File 'lib/syntax_tree/node.rb', line 7686

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



7700
7701
7702
# File 'lib/syntax_tree/node.rb', line 7700

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

#format(q) ⇒ Object



7704
7705
7706
7707
# File 'lib/syntax_tree/node.rb', line 7704

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