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.



6593
6594
6595
6596
6597
6598
6599
# File 'lib/syntax_tree/node.rb', line 6593

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

Instance Attribute Details

#comments ⇒ Object (readonly)

[Array[ Comment | EmbDoc ]] the comments attached to this node



6591
6592
6593
# File 'lib/syntax_tree/node.rb', line 6591

def comments
  @comments
end

#operator ⇒ Object (readonly)

[Op] the operator being used for the assignment



6585
6586
6587
# File 'lib/syntax_tree/node.rb', line 6585

def operator
  @operator
end

#target ⇒ Object (readonly)

[ARefField | ConstPathField | Field | TopConstField | VarField] the target to assign the result of the expression to



6582
6583
6584
# File 'lib/syntax_tree/node.rb', line 6582

def target
  @target
end

#value ⇒ Object (readonly)

[untyped] the expression to be assigned



6588
6589
6590
# File 'lib/syntax_tree/node.rb', line 6588

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object



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

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

#child_nodes ⇒ Object Also known as: deconstruct



6605
6606
6607
# File 'lib/syntax_tree/node.rb', line 6605

def child_nodes
  [target, operator, value]
end

#deconstruct_keys(_keys) ⇒ Object



6611
6612
6613
6614
6615
6616
6617
6618
6619
# File 'lib/syntax_tree/node.rb', line 6611

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

#format(q) ⇒ Object



6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
# File 'lib/syntax_tree/node.rb', line 6621

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