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

Returns a new instance of MAssign.



5991
5992
5993
5994
5995
5996
# File 'lib/syntax_tree/node.rb', line 5991

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



5989
5990
5991
# File 'lib/syntax_tree/node.rb', line 5989

def comments
  @comments
end

#targetObject (readonly)

[MLHS | MLHSParen] the target of the multiple assignment



5983
5984
5985
# File 'lib/syntax_tree/node.rb', line 5983

def target
  @target
end

#valueObject (readonly)

[untyped] the value being assigned



5986
5987
5988
# File 'lib/syntax_tree/node.rb', line 5986

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object



5998
5999
6000
# File 'lib/syntax_tree/node.rb', line 5998

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

#child_nodesObject Also known as: deconstruct



6002
6003
6004
# File 'lib/syntax_tree/node.rb', line 6002

def child_nodes
  [target, value]
end

#deconstruct_keys(keys) ⇒ Object



6008
6009
6010
# File 'lib/syntax_tree/node.rb', line 6008

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

#format(q) ⇒ Object



6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
# File 'lib/syntax_tree/node.rb', line 6012

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