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:, comments: []) ⇒ MAssign

Returns a new instance of MAssign.



6187
6188
6189
6190
6191
6192
# File 'lib/syntax_tree/node.rb', line 6187

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

Instance Attribute Details

#comments ⇒ Object (readonly)

[Array[ Comment | EmbDoc ]] the comments attached to this node



6185
6186
6187
# File 'lib/syntax_tree/node.rb', line 6185

def comments
  @comments
end

#target ⇒ Object (readonly)

[MLHS | MLHSParen] the target of the multiple assignment



6179
6180
6181
# File 'lib/syntax_tree/node.rb', line 6179

def target
  @target
end

#value ⇒ Object (readonly)

[untyped] the value being assigned



6182
6183
6184
# File 'lib/syntax_tree/node.rb', line 6182

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object



6194
6195
6196
# File 'lib/syntax_tree/node.rb', line 6194

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

#child_nodes ⇒ Object Also known as: deconstruct



6198
6199
6200
# File 'lib/syntax_tree/node.rb', line 6198

def child_nodes
  [target, value]
end

#deconstruct_keys(_keys) ⇒ Object



6204
6205
6206
# File 'lib/syntax_tree/node.rb', line 6204

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

#format(q) ⇒ Object



6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
# File 'lib/syntax_tree/node.rb', line 6208

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