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, #pretty_print, #to_json

Constructor Details

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

Returns a new instance of MLHS.



6362
6363
6364
6365
6366
6367
# File 'lib/syntax_tree/node.rb', line 6362

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



6357
6358
6359
# File 'lib/syntax_tree/node.rb', line 6357

def comma
  @comma
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6360
6361
6362
# File 'lib/syntax_tree/node.rb', line 6360

def comments
  @comments
end

#partsObject (readonly)

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



6352
6353
6354
# File 'lib/syntax_tree/node.rb', line 6352

def parts
  @parts
end

Instance Method Details

#accept(visitor) ⇒ Object



6369
6370
6371
# File 'lib/syntax_tree/node.rb', line 6369

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

#child_nodesObject Also known as: deconstruct



6373
6374
6375
# File 'lib/syntax_tree/node.rb', line 6373

def child_nodes
  parts
end

#deconstruct_keys(_keys) ⇒ Object



6379
6380
6381
# File 'lib/syntax_tree/node.rb', line 6379

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

#format(q) ⇒ Object



6383
6384
6385
6386
# File 'lib/syntax_tree/node.rb', line 6383

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