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.



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

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



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

def comments
  @comments
end

#operatorObject (readonly)

Op

the operator being used for the assignment



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

def operator
  @operator
end

#targetObject (readonly)

ARefField | ConstPathField | Field | TopConstField | VarField

the target

to assign the result of the expression to



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

def target
  @target
end

#valueObject (readonly)

untyped

the expression to be assigned



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

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [target, operator, value]
end

#deconstruct_keys(keys) ⇒ Object



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

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

#format(q) ⇒ Object



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

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