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.



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

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



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

def comma
  @comma
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



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

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



7635
7636
7637
# File 'lib/syntax_tree/node.rb', line 7635

def parts
  @parts
end

Instance Method Details

#===(other) ⇒ Object



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

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  parts
end

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



7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
# File 'lib/syntax_tree/node.rb', line 7660

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



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

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

#format(q) ⇒ Object



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

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