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:) ⇒ MLHS

Returns a new instance of MLHS.



7550
7551
7552
7553
7554
7555
# File 'lib/syntax_tree/node.rb', line 7550

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



7545
7546
7547
# File 'lib/syntax_tree/node.rb', line 7545

def comma
  @comma
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7548
7549
7550
# File 'lib/syntax_tree/node.rb', line 7548

def comments
  @comments
end

#partsObject (readonly)

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



7540
7541
7542
# File 'lib/syntax_tree/node.rb', line 7540

def parts
  @parts
end

Instance Method Details

#===(other) ⇒ Object



7588
7589
7590
7591
# File 'lib/syntax_tree/node.rb', line 7588

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

#accept(visitor) ⇒ Object



7557
7558
7559
# File 'lib/syntax_tree/node.rb', line 7557

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

#child_nodesObject Also known as: deconstruct



7561
7562
7563
# File 'lib/syntax_tree/node.rb', line 7561

def child_nodes
  parts
end

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



7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
# File 'lib/syntax_tree/node.rb', line 7565

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



7579
7580
7581
# File 'lib/syntax_tree/node.rb', line 7579

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

#format(q) ⇒ Object



7583
7584
7585
7586
# File 'lib/syntax_tree/node.rb', line 7583

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