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

#pretty_print, #to_json

Constructor Details

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

Returns a new instance of MLHS.



5559
5560
5561
5562
5563
5564
# File 'lib/syntax_tree/node.rb', line 5559

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



5554
5555
5556
# File 'lib/syntax_tree/node.rb', line 5554

def comma
  @comma
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



5557
5558
5559
# File 'lib/syntax_tree/node.rb', line 5557

def comments
  @comments
end

#partsObject (readonly)

Array[ARefField | ArgStar | Field | Ident | MLHSParen | VarField] the parts of the left-hand side of a multiple assignment



5549
5550
5551
# File 'lib/syntax_tree/node.rb', line 5549

def parts
  @parts
end

Instance Method Details

#accept(visitor) ⇒ Object



5566
5567
5568
# File 'lib/syntax_tree/node.rb', line 5566

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

#child_nodesObject Also known as: deconstruct



5570
5571
5572
# File 'lib/syntax_tree/node.rb', line 5570

def child_nodes
  parts
end

#deconstruct_keys(keys) ⇒ Object



5576
5577
5578
# File 'lib/syntax_tree/node.rb', line 5576

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

#format(q) ⇒ Object



5580
5581
5582
5583
# File 'lib/syntax_tree/node.rb', line 5580

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