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

#pretty_print, #to_json

Constructor Details

#initialize(target:, value:, location:, comments: []) ⇒ MAssign



5467
5468
5469
5470
5471
5472
# File 'lib/syntax_tree/node.rb', line 5467

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



5465
5466
5467
# File 'lib/syntax_tree/node.rb', line 5465

def comments
  @comments
end

#targetObject (readonly)

MLHS | MLHSParen

the target of the multiple assignment



5459
5460
5461
# File 'lib/syntax_tree/node.rb', line 5459

def target
  @target
end

#valueObject (readonly)

untyped

the value being assigned



5462
5463
5464
# File 'lib/syntax_tree/node.rb', line 5462

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object



5474
5475
5476
# File 'lib/syntax_tree/node.rb', line 5474

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

#child_nodesObject Also known as: deconstruct



5478
5479
5480
# File 'lib/syntax_tree/node.rb', line 5478

def child_nodes
  [target, value]
end

#deconstruct_keys(keys) ⇒ Object



5484
5485
5486
# File 'lib/syntax_tree/node.rb', line 5484

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

#format(q) ⇒ Object



5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
# File 'lib/syntax_tree/node.rb', line 5488

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