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.



8880
8881
8882
8883
8884
8885
8886
# File 'lib/syntax_tree.rb', line 8880

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



8878
8879
8880
# File 'lib/syntax_tree.rb', line 8878

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



8875
8876
8877
# File 'lib/syntax_tree.rb', line 8875

def location
  @location
end

#operatorObject (readonly)

Op

the operator being used for the assignment



8869
8870
8871
# File 'lib/syntax_tree.rb', line 8869

def operator
  @operator
end

#targetObject (readonly)

ARefField | ConstPathField | Field | TopConstField | VarField

the target

to assign the result of the expression to



8866
8867
8868
# File 'lib/syntax_tree.rb', line 8866

def target
  @target
end

#valueObject (readonly)

untyped

the expression to be assigned



8872
8873
8874
# File 'lib/syntax_tree.rb', line 8872

def value
  @value
end

Instance Method Details

#child_nodesObject



8888
8889
8890
# File 'lib/syntax_tree.rb', line 8888

def child_nodes
  [target, operator, value]
end

#format(q) ⇒ Object



8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
# File 'lib/syntax_tree.rb', line 8892

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



8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
# File 'lib/syntax_tree.rb', line 8904

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



8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
# File 'lib/syntax_tree.rb', line 8921

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