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

Constructor Details

#initialize(target:, value:, location:) ⇒ MAssign

Returns a new instance of MAssign.



7397
7398
7399
7400
7401
7402
# File 'lib/syntax_tree/node.rb', line 7397

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



7395
7396
7397
# File 'lib/syntax_tree/node.rb', line 7395

def comments
  @comments
end

#targetObject (readonly)

MLHS | MLHSParen

the target of the multiple assignment



7389
7390
7391
# File 'lib/syntax_tree/node.rb', line 7389

def target
  @target
end

#valueObject (readonly)

untyped

the value being assigned



7392
7393
7394
# File 'lib/syntax_tree/node.rb', line 7392

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



7441
7442
7443
# File 'lib/syntax_tree/node.rb', line 7441

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

#accept(visitor) ⇒ Object



7404
7405
7406
# File 'lib/syntax_tree/node.rb', line 7404

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

#child_nodesObject Also known as: deconstruct



7408
7409
7410
# File 'lib/syntax_tree/node.rb', line 7408

def child_nodes
  [target, value]
end

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



7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
# File 'lib/syntax_tree/node.rb', line 7412

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



7426
7427
7428
# File 'lib/syntax_tree/node.rb', line 7426

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

#format(q) ⇒ Object



7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
# File 'lib/syntax_tree/node.rb', line 7430

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