Class: SyntaxTree::OpAssign

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.rb

Overview

OpAssign represents assigning a value to a variable or constant using an operator like += or ||=.

variable += value

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of OpAssign.



8335
8336
8337
8338
8339
8340
8341
# File 'lib/syntax_tree.rb', line 8335

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



8333
8334
8335
# File 'lib/syntax_tree.rb', line 8333

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



8330
8331
8332
# File 'lib/syntax_tree.rb', line 8330

def location
  @location
end

#operatorObject (readonly)

Op

the operator being used for the assignment



8324
8325
8326
# File 'lib/syntax_tree.rb', line 8324

def operator
  @operator
end

#targetObject (readonly)

ARefField | ConstPathField | Field | TopConstField | VarField

the target

to assign the result of the expression to



8321
8322
8323
# File 'lib/syntax_tree.rb', line 8321

def target
  @target
end

#valueObject (readonly)

untyped

the expression to be assigned



8327
8328
8329
# File 'lib/syntax_tree.rb', line 8327

def value
  @value
end

Instance Method Details

#child_nodesObject



8343
8344
8345
# File 'lib/syntax_tree.rb', line 8343

def child_nodes
  [target, operator, value]
end

#format(q) ⇒ Object



8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
# File 'lib/syntax_tree.rb', line 8347

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



8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
# File 'lib/syntax_tree.rb', line 8359

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



8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
# File 'lib/syntax_tree.rb', line 8376

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