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.



8748
8749
8750
8751
8752
8753
8754
# File 'lib/syntax_tree.rb', line 8748

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



8746
8747
8748
# File 'lib/syntax_tree.rb', line 8746

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



8743
8744
8745
# File 'lib/syntax_tree.rb', line 8743

def location
  @location
end

#operatorObject (readonly)

Op

the operator being used for the assignment



8737
8738
8739
# File 'lib/syntax_tree.rb', line 8737

def operator
  @operator
end

#targetObject (readonly)

ARefField | ConstPathField | Field | TopConstField | VarField

the target

to assign the result of the expression to



8734
8735
8736
# File 'lib/syntax_tree.rb', line 8734

def target
  @target
end

#valueObject (readonly)

untyped

the expression to be assigned



8740
8741
8742
# File 'lib/syntax_tree.rb', line 8740

def value
  @value
end

Instance Method Details

#child_nodesObject



8756
8757
8758
# File 'lib/syntax_tree.rb', line 8756

def child_nodes
  [target, operator, value]
end

#format(q) ⇒ Object



8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
# File 'lib/syntax_tree.rb', line 8760

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



8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
# File 'lib/syntax_tree.rb', line 8772

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



8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
# File 'lib/syntax_tree.rb', line 8789

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