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

Constructor Details

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

Returns a new instance of OpAssign.



7367
7368
7369
7370
7371
7372
7373
# File 'lib/syntax_tree/node.rb', line 7367

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



7365
7366
7367
# File 'lib/syntax_tree/node.rb', line 7365

def comments
  @comments
end

#operatorObject (readonly)

Op

the operator being used for the assignment



7359
7360
7361
# File 'lib/syntax_tree/node.rb', line 7359

def operator
  @operator
end

#targetObject (readonly)

ARefField | ConstPathField | Field | TopConstField | VarField

the target

to assign the result of the expression to



7356
7357
7358
# File 'lib/syntax_tree/node.rb', line 7356

def target
  @target
end

#valueObject (readonly)

untyped

the expression to be assigned



7362
7363
7364
# File 'lib/syntax_tree/node.rb', line 7362

def value
  @value
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



7375
7376
7377
# File 'lib/syntax_tree/node.rb', line 7375

def child_nodes
  [target, operator, value]
end

#deconstruct_keys(keys) ⇒ Object



7381
7382
7383
7384
7385
7386
7387
7388
7389
# File 'lib/syntax_tree/node.rb', line 7381

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

#format(q) ⇒ Object



7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
# File 'lib/syntax_tree/node.rb', line 7391

def format(q)
  q.group do
    q.format(target)
    q.text(" ")
    q.format(operator)
    q.indent do
      q.breakable
      q.format(value)
    end
  end
end

#pretty_print(q) ⇒ Object



7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
# File 'lib/syntax_tree/node.rb', line 7403

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("opassign")

    q.breakable
    q.pp(target)

    q.breakable
    q.pp(operator)

    q.breakable
    q.pp(value)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
# File 'lib/syntax_tree/node.rb', line 7420

def to_json(*opts)
  {
    type: :opassign,
    target: target,
    op: operator,
    value: value,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end