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

#pretty_print, #to_json

Constructor Details

#initialize(target:, operator:, value:, location:, comments: []) ⇒ OpAssign

Returns a new instance of OpAssign.



6391
6392
6393
6394
6395
6396
6397
# File 'lib/syntax_tree/node.rb', line 6391

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



6389
6390
6391
# File 'lib/syntax_tree/node.rb', line 6389

def comments
  @comments
end

#operatorObject (readonly)

Op

the operator being used for the assignment



6383
6384
6385
# File 'lib/syntax_tree/node.rb', line 6383

def operator
  @operator
end

#targetObject (readonly)

ARefField | ConstPathField | Field | TopConstField | VarField

the target

to assign the result of the expression to



6380
6381
6382
# File 'lib/syntax_tree/node.rb', line 6380

def target
  @target
end

#valueObject (readonly)

untyped

the expression to be assigned



6386
6387
6388
# File 'lib/syntax_tree/node.rb', line 6386

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object



6399
6400
6401
# File 'lib/syntax_tree/node.rb', line 6399

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

#child_nodesObject Also known as: deconstruct



6403
6404
6405
# File 'lib/syntax_tree/node.rb', line 6403

def child_nodes
  [target, operator, value]
end

#deconstruct_keys(keys) ⇒ Object



6409
6410
6411
6412
6413
6414
6415
6416
6417
# File 'lib/syntax_tree/node.rb', line 6409

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

#format(q) ⇒ Object



6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
# File 'lib/syntax_tree/node.rb', line 6419

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