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:) ⇒ OpAssign

Returns a new instance of OpAssign.



7935
7936
7937
7938
7939
7940
7941
# File 'lib/syntax_tree/node.rb', line 7935

def initialize(target:, operator:, value:, location:)
  @target = target
  @operator = operator
  @value = value
  @location = location
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7933
7934
7935
# File 'lib/syntax_tree/node.rb', line 7933

def comments
  @comments
end

#operatorObject (readonly)

Op

the operator being used for the assignment



7927
7928
7929
# File 'lib/syntax_tree/node.rb', line 7927

def operator
  @operator
end

#targetObject (readonly)

ARefField | ConstPathField | Field | TopConstField | VarField

the target

to assign the result of the expression to



7924
7925
7926
# File 'lib/syntax_tree/node.rb', line 7924

def target
  @target
end

#valueObject (readonly)

untyped

the expression to be assigned



7930
7931
7932
# File 'lib/syntax_tree/node.rb', line 7930

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



7994
7995
7996
7997
# File 'lib/syntax_tree/node.rb', line 7994

def ===(other)
  other.is_a?(OpAssign) && target === other.target &&
    operator === other.operator && value === other.value
end

#accept(visitor) ⇒ Object



7943
7944
7945
# File 'lib/syntax_tree/node.rb', line 7943

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

#child_nodesObject Also known as: deconstruct



7947
7948
7949
# File 'lib/syntax_tree/node.rb', line 7947

def child_nodes
  [target, operator, value]
end

#copy(target: nil, operator: nil, value: nil, location: nil) ⇒ Object



7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
# File 'lib/syntax_tree/node.rb', line 7951

def copy(target: nil, operator: nil, value: nil, location: nil)
  node =
    OpAssign.new(
      target: target || self.target,
      operator: operator || self.operator,
      value: value || self.value,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



7966
7967
7968
7969
7970
7971
7972
7973
7974
# File 'lib/syntax_tree/node.rb', line 7966

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

#format(q) ⇒ Object



7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
# File 'lib/syntax_tree/node.rb', line 7976

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