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.



7655
7656
7657
7658
7659
7660
# File 'lib/syntax_tree/node.rb', line 7655

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



7650
7651
7652
# File 'lib/syntax_tree/node.rb', line 7650

def comma
  @comma
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



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

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



7645
7646
7647
# File 'lib/syntax_tree/node.rb', line 7645

def parts
  @parts
end

Instance Method Details

#===(other) ⇒ Object



7693
7694
7695
7696
# File 'lib/syntax_tree/node.rb', line 7693

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  parts
end

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



7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
# File 'lib/syntax_tree/node.rb', line 7670

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



7684
7685
7686
# File 'lib/syntax_tree/node.rb', line 7684

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

#format(q) ⇒ Object



7688
7689
7690
7691
# File 'lib/syntax_tree/node.rb', line 7688

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