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.



6558
6559
6560
6561
6562
6563
6564
# File 'lib/syntax_tree/node.rb', line 6558

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



6556
6557
6558
# File 'lib/syntax_tree/node.rb', line 6556

def comments
  @comments
end

#operatorObject (readonly)

Op

the operator being used for the assignment



6550
6551
6552
# File 'lib/syntax_tree/node.rb', line 6550

def operator
  @operator
end

#targetObject (readonly)

ARefField | ConstPathField | Field | TopConstField | VarField

the target

to assign the result of the expression to



6547
6548
6549
# File 'lib/syntax_tree/node.rb', line 6547

def target
  @target
end

#valueObject (readonly)

untyped

the expression to be assigned



6553
6554
6555
# File 'lib/syntax_tree/node.rb', line 6553

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object



6566
6567
6568
# File 'lib/syntax_tree/node.rb', line 6566

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

#child_nodesObject Also known as: deconstruct



6570
6571
6572
# File 'lib/syntax_tree/node.rb', line 6570

def child_nodes
  [target, operator, value]
end

#deconstruct_keys(_keys) ⇒ Object



6576
6577
6578
6579
6580
6581
6582
6583
6584
# File 'lib/syntax_tree/node.rb', line 6576

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

#format(q) ⇒ Object



6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
# File 'lib/syntax_tree/node.rb', line 6586

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