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.



6604
6605
6606
6607
6608
6609
6610
# File 'lib/syntax_tree/node.rb', line 6604

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



6602
6603
6604
# File 'lib/syntax_tree/node.rb', line 6602

def comments
  @comments
end

#operatorObject (readonly)

Op

the operator being used for the assignment



6596
6597
6598
# File 'lib/syntax_tree/node.rb', line 6596

def operator
  @operator
end

#targetObject (readonly)

ARefField | ConstPathField | Field | TopConstField | VarField

the target

to assign the result of the expression to



6593
6594
6595
# File 'lib/syntax_tree/node.rb', line 6593

def target
  @target
end

#valueObject (readonly)

untyped

the expression to be assigned



6599
6600
6601
# File 'lib/syntax_tree/node.rb', line 6599

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object



6612
6613
6614
# File 'lib/syntax_tree/node.rb', line 6612

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

#child_nodesObject Also known as: deconstruct



6616
6617
6618
# File 'lib/syntax_tree/node.rb', line 6616

def child_nodes
  [target, operator, value]
end

#deconstruct_keys(_keys) ⇒ Object



6622
6623
6624
6625
6626
6627
6628
6629
6630
# File 'lib/syntax_tree/node.rb', line 6622

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

#format(q) ⇒ Object



6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
# File 'lib/syntax_tree/node.rb', line 6632

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