Class: SyntaxTree::MAssign

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of MAssign.



7982
7983
7984
7985
7986
7987
# File 'lib/syntax_tree.rb', line 7982

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



7980
7981
7982
# File 'lib/syntax_tree.rb', line 7980

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



7977
7978
7979
# File 'lib/syntax_tree.rb', line 7977

def location
  @location
end

#targetObject (readonly)

MLHS | MLHSParen

the target of the multiple assignment



7971
7972
7973
# File 'lib/syntax_tree.rb', line 7971

def target
  @target
end

#valueObject (readonly)

untyped

the value being assigned



7974
7975
7976
# File 'lib/syntax_tree.rb', line 7974

def value
  @value
end

Instance Method Details

#child_nodesObject



7989
7990
7991
# File 'lib/syntax_tree.rb', line 7989

def child_nodes
  [target, value]
end

#format(q) ⇒ Object



7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
# File 'lib/syntax_tree.rb', line 7993

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

#pretty_print(q) ⇒ Object



8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
# File 'lib/syntax_tree.rb', line 8004

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("massign")

    q.breakable
    q.pp(target)

    q.breakable
    q.pp(value)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



8018
8019
8020
8021
8022
8023
8024
8025
8026
# File 'lib/syntax_tree.rb', line 8018

def to_json(*opts)
  {
    type: :massign,
    target: target,
    value: value,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end