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

#construct_keys, #pretty_print, #to_json

Constructor Details

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

Returns a new instance of OpAssign.



6838
6839
6840
6841
6842
6843
6844
# File 'lib/syntax_tree/node.rb', line 6838

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



6836
6837
6838
# File 'lib/syntax_tree/node.rb', line 6836

def comments
  @comments
end

#operatorObject (readonly)

Op

the operator being used for the assignment



6830
6831
6832
# File 'lib/syntax_tree/node.rb', line 6830

def operator
  @operator
end

#targetObject (readonly)

ARefField | ConstPathField | Field | TopConstField | VarField

the target

to assign the result of the expression to



6827
6828
6829
# File 'lib/syntax_tree/node.rb', line 6827

def target
  @target
end

#valueObject (readonly)

untyped

the expression to be assigned



6833
6834
6835
# File 'lib/syntax_tree/node.rb', line 6833

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object



6846
6847
6848
# File 'lib/syntax_tree/node.rb', line 6846

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

#child_nodesObject Also known as: deconstruct



6850
6851
6852
# File 'lib/syntax_tree/node.rb', line 6850

def child_nodes
  [target, operator, value]
end

#deconstruct_keys(_keys) ⇒ Object



6856
6857
6858
6859
6860
6861
6862
6863
6864
# File 'lib/syntax_tree/node.rb', line 6856

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

#format(q) ⇒ Object



6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
# File 'lib/syntax_tree/node.rb', line 6866

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_space
        q.format(value)
      end
    end
  end
end