Class: SyntaxTree::OpAssign

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

OpAssign represents assigning a value to a variable or constant using an operator like += or ||=.

variable += 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:, operator:, value:, location:, comments: []) ⇒ OpAssign

Returns a new instance of OpAssign.



6643
6644
6645
6646
6647
6648
6649
# File 'lib/syntax_tree/node.rb', line 6643

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6641
6642
6643
# File 'lib/syntax_tree/node.rb', line 6641

def comments
  @comments
end

#operatorObject (readonly)

Op

the operator being used for the assignment



6635
6636
6637
# File 'lib/syntax_tree/node.rb', line 6635

def operator
  @operator
end

#targetObject (readonly)

ARefField | ConstPathField | Field | TopConstField | VarField

the target

to assign the result of the expression to



6632
6633
6634
# File 'lib/syntax_tree/node.rb', line 6632

def target
  @target
end

#valueObject (readonly)

untyped

the expression to be assigned



6638
6639
6640
# File 'lib/syntax_tree/node.rb', line 6638

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object



6651
6652
6653
# File 'lib/syntax_tree/node.rb', line 6651

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

#child_nodesObject Also known as: deconstruct



6655
6656
6657
# File 'lib/syntax_tree/node.rb', line 6655

def child_nodes
  [target, operator, value]
end

#deconstruct_keys(_keys) ⇒ Object



6661
6662
6663
6664
6665
6666
6667
6668
6669
# File 'lib/syntax_tree/node.rb', line 6661

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

#format(q) ⇒ Object



6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
# File 'lib/syntax_tree/node.rb', line 6671

def format(q)
  q.group do
    q.format(target)
    q.text(" ")
    q.format(operator)

    if skip_indent?
      q.text(" ")
      q.format(value)
    else
      q.indent do
        q.breakable
        q.format(value)
      end
    end
  end
end