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.



6482
6483
6484
6485
6486
6487
6488
# File 'lib/syntax_tree/node.rb', line 6482

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



6480
6481
6482
# File 'lib/syntax_tree/node.rb', line 6480

def comments
  @comments
end

#operatorObject (readonly)

Op

the operator being used for the assignment



6474
6475
6476
# File 'lib/syntax_tree/node.rb', line 6474

def operator
  @operator
end

#targetObject (readonly)

ARefField | ConstPathField | Field | TopConstField | VarField

the target

to assign the result of the expression to



6471
6472
6473
# File 'lib/syntax_tree/node.rb', line 6471

def target
  @target
end

#valueObject (readonly)

untyped

the expression to be assigned



6477
6478
6479
# File 'lib/syntax_tree/node.rb', line 6477

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object



6490
6491
6492
# File 'lib/syntax_tree/node.rb', line 6490

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

#child_nodesObject Also known as: deconstruct



6494
6495
6496
# File 'lib/syntax_tree/node.rb', line 6494

def child_nodes
  [target, operator, value]
end

#deconstruct_keys(_keys) ⇒ Object



6500
6501
6502
6503
6504
6505
6506
6507
6508
# File 'lib/syntax_tree/node.rb', line 6500

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

#format(q) ⇒ Object



6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
# File 'lib/syntax_tree/node.rb', line 6510

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