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.



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

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



7524
7525
7526
# File 'lib/syntax_tree/node.rb', line 7524

def comments
  @comments
end

#targetObject (readonly)

MLHS | MLHSParen

the target of the multiple assignment



7518
7519
7520
# File 'lib/syntax_tree/node.rb', line 7518

def target
  @target
end

#valueObject (readonly)

Node

the value being assigned



7521
7522
7523
# File 'lib/syntax_tree/node.rb', line 7521

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



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

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [target, value]
end

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



7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
# File 'lib/syntax_tree/node.rb', line 7541

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



7555
7556
7557
# File 'lib/syntax_tree/node.rb', line 7555

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

#format(q) ⇒ Object



7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
# File 'lib/syntax_tree/node.rb', line 7559

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