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.



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

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



7533
7534
7535
# File 'lib/syntax_tree/node.rb', line 7533

def comma
  @comma
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7536
7537
7538
# File 'lib/syntax_tree/node.rb', line 7536

def comments
  @comments
end

#partsObject (readonly)

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



7528
7529
7530
# File 'lib/syntax_tree/node.rb', line 7528

def parts
  @parts
end

Instance Method Details

#===(other) ⇒ Object



7576
7577
7578
7579
# File 'lib/syntax_tree/node.rb', line 7576

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  parts
end

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



7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
# File 'lib/syntax_tree/node.rb', line 7553

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



7567
7568
7569
# File 'lib/syntax_tree/node.rb', line 7567

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

#format(q) ⇒ Object



7571
7572
7573
7574
# File 'lib/syntax_tree/node.rb', line 7571

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