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.



7581
7582
7583
7584
7585
7586
# File 'lib/syntax_tree.rb', line 7581

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



7579
7580
7581
# File 'lib/syntax_tree.rb', line 7579

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



7576
7577
7578
# File 'lib/syntax_tree.rb', line 7576

def location
  @location
end

#targetObject (readonly)

MLHS | MLHSParen

the target of the multiple assignment



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

def target
  @target
end

#valueObject (readonly)

untyped

the value being assigned



7573
7574
7575
# File 'lib/syntax_tree.rb', line 7573

def value
  @value
end

Instance Method Details

#child_nodesObject



7588
7589
7590
# File 'lib/syntax_tree.rb', line 7588

def child_nodes
  [target, value]
end

#format(q) ⇒ Object



7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
# File 'lib/syntax_tree.rb', line 7592

def format(q)
  q.group do
    q.group do
      q.format(target)
      q.text(",") if target.is_a?(MLHS) && target.comma
    end

    q.text(" =")
    q.indent do
      q.breakable
      q.format(value)
    end
  end
end

#pretty_print(q) ⇒ Object



7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
# File 'lib/syntax_tree.rb', line 7607

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



7621
7622
7623
7624
7625
7626
7627
7628
7629
# File 'lib/syntax_tree.rb', line 7621

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