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.



5849
5850
5851
5852
5853
5854
5855
# File 'lib/syntax_tree/node.rb', line 5849

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



5847
5848
5849
# File 'lib/syntax_tree/node.rb', line 5847

def comments
  @comments
end

#operatorObject (readonly)

Op

the operator being used for the assignment



5841
5842
5843
# File 'lib/syntax_tree/node.rb', line 5841

def operator
  @operator
end

#targetObject (readonly)

ARefField | ConstPathField | Field | TopConstField | VarField

the target

to assign the result of the expression to



5838
5839
5840
# File 'lib/syntax_tree/node.rb', line 5838

def target
  @target
end

#valueObject (readonly)

untyped

the expression to be assigned



5844
5845
5846
# File 'lib/syntax_tree/node.rb', line 5844

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object



5857
5858
5859
# File 'lib/syntax_tree/node.rb', line 5857

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

#child_nodesObject Also known as: deconstruct



5861
5862
5863
# File 'lib/syntax_tree/node.rb', line 5861

def child_nodes
  [target, operator, value]
end

#deconstruct_keys(keys) ⇒ Object



5867
5868
5869
5870
5871
5872
5873
5874
5875
# File 'lib/syntax_tree/node.rb', line 5867

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

#format(q) ⇒ Object



5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
# File 'lib/syntax_tree/node.rb', line 5877

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