Class: SyntaxTree::MAssign

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

MAssign is a parent node of any kind of multiple assignment. This includes splitting out variables on the left like:

first, second, third = value

as well as splitting out variables on the right, as in:

value = first, second, third

Both sides support splats, as well as variables following them. There’s also destructuring behavior that you can achieve with the following:

first, = 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(target:, value:, location:) ⇒ MAssign

Returns a new instance of MAssign.



7469
7470
7471
7472
7473
7474
# File 'lib/syntax_tree/node.rb', line 7469

def initialize(target:, value:, location:)
  @target = target
  @value = value
  @location = location
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7467
7468
7469
# File 'lib/syntax_tree/node.rb', line 7467

def comments
  @comments
end

#targetObject (readonly)

MLHS | MLHSParen

the target of the multiple assignment



7461
7462
7463
# File 'lib/syntax_tree/node.rb', line 7461

def target
  @target
end

#valueObject (readonly)

Node

the value being assigned



7464
7465
7466
# File 'lib/syntax_tree/node.rb', line 7464

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



7513
7514
7515
# File 'lib/syntax_tree/node.rb', line 7513

def ===(other)
  other.is_a?(MAssign) && target === other.target && value === other.value
end

#accept(visitor) ⇒ Object



7476
7477
7478
# File 'lib/syntax_tree/node.rb', line 7476

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

#child_nodesObject Also known as: deconstruct



7480
7481
7482
# File 'lib/syntax_tree/node.rb', line 7480

def child_nodes
  [target, value]
end

#copy(target: nil, value: nil, location: nil) ⇒ Object



7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
# File 'lib/syntax_tree/node.rb', line 7484

def copy(target: nil, value: nil, location: nil)
  node =
    MAssign.new(
      target: target || self.target,
      value: value || self.value,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



7498
7499
7500
# File 'lib/syntax_tree/node.rb', line 7498

def deconstruct_keys(_keys)
  { target: target, value: value, location: location, comments: comments }
end

#format(q) ⇒ Object



7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
# File 'lib/syntax_tree/node.rb', line 7502

def format(q)
  q.group do
    q.group { q.format(target) }
    q.text(" =")
    q.indent do
      q.breakable_space
      q.format(value)
    end
  end
end