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.



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

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



6658
6659
6660
# File 'lib/syntax_tree/node.rb', line 6658

def comments
  @comments
end

#operatorObject (readonly)

Op

the operator being used for the assignment



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

def operator
  @operator
end

#targetObject (readonly)

ARefField | ConstPathField | Field | TopConstField | VarField

the target

to assign the result of the expression to



6649
6650
6651
# File 'lib/syntax_tree/node.rb', line 6649

def target
  @target
end

#valueObject (readonly)

untyped

the expression to be assigned



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

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object



6668
6669
6670
# File 'lib/syntax_tree/node.rb', line 6668

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

#child_nodesObject Also known as: deconstruct



6672
6673
6674
# File 'lib/syntax_tree/node.rb', line 6672

def child_nodes
  [target, operator, value]
end

#deconstruct_keys(_keys) ⇒ Object



6678
6679
6680
6681
6682
6683
6684
6685
6686
# File 'lib/syntax_tree/node.rb', line 6678

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

#format(q) ⇒ Object



6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
# File 'lib/syntax_tree/node.rb', line 6688

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