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.



7944
7945
7946
7947
7948
7949
7950
# File 'lib/syntax_tree/node.rb', line 7944

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



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

def comments
  @comments
end

#operatorObject (readonly)

Op

the operator being used for the assignment



7936
7937
7938
# File 'lib/syntax_tree/node.rb', line 7936

def operator
  @operator
end

#targetObject (readonly)

ARefField | ConstPathField | Field | TopConstField | VarField

the target

to assign the result of the expression to



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

def target
  @target
end

#valueObject (readonly)

untyped

the expression to be assigned



7939
7940
7941
# File 'lib/syntax_tree/node.rb', line 7939

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



8003
8004
8005
8006
# File 'lib/syntax_tree/node.rb', line 8003

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

#accept(visitor) ⇒ Object



7952
7953
7954
# File 'lib/syntax_tree/node.rb', line 7952

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

#child_nodesObject Also known as: deconstruct



7956
7957
7958
# File 'lib/syntax_tree/node.rb', line 7956

def child_nodes
  [target, operator, value]
end

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



7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
# File 'lib/syntax_tree/node.rb', line 7960

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



7975
7976
7977
7978
7979
7980
7981
7982
7983
# File 'lib/syntax_tree/node.rb', line 7975

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

#format(q) ⇒ Object



7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
# File 'lib/syntax_tree/node.rb', line 7985

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