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.



6646
6647
6648
6649
6650
6651
6652
# File 'lib/syntax_tree/node.rb', line 6646

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



6644
6645
6646
# File 'lib/syntax_tree/node.rb', line 6644

def comments
  @comments
end

#operatorObject (readonly)

Op

the operator being used for the assignment



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

def operator
  @operator
end

#targetObject (readonly)

ARefField | ConstPathField | Field | TopConstField | VarField

the target

to assign the result of the expression to



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

def target
  @target
end

#valueObject (readonly)

untyped

the expression to be assigned



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

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [target, operator, value]
end

#deconstruct_keys(_keys) ⇒ Object



6664
6665
6666
6667
6668
6669
6670
6671
6672
# File 'lib/syntax_tree/node.rb', line 6664

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

#format(q) ⇒ Object



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

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