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, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

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

Returns a new instance of OpAssign.



8068
8069
8070
8071
8072
8073
8074
# File 'lib/syntax_tree/node.rb', line 8068

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



8066
8067
8068
# File 'lib/syntax_tree/node.rb', line 8066

def comments
  @comments
end

#operatorObject (readonly)

Op

the operator being used for the assignment



8060
8061
8062
# File 'lib/syntax_tree/node.rb', line 8060

def operator
  @operator
end

#targetObject (readonly)

ARefField | ConstPathField | Field | TopConstField | VarField

the target

to assign the result of the expression to



8057
8058
8059
# File 'lib/syntax_tree/node.rb', line 8057

def target
  @target
end

#valueObject (readonly)

Node

the expression to be assigned



8063
8064
8065
# File 'lib/syntax_tree/node.rb', line 8063

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



8127
8128
8129
8130
# File 'lib/syntax_tree/node.rb', line 8127

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

#accept(visitor) ⇒ Object



8076
8077
8078
# File 'lib/syntax_tree/node.rb', line 8076

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

#child_nodesObject Also known as: deconstruct



8080
8081
8082
# File 'lib/syntax_tree/node.rb', line 8080

def child_nodes
  [target, operator, value]
end

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



8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
# File 'lib/syntax_tree/node.rb', line 8084

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



8099
8100
8101
8102
8103
8104
8105
8106
8107
# File 'lib/syntax_tree/node.rb', line 8099

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

#format(q) ⇒ Object



8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
# File 'lib/syntax_tree/node.rb', line 8109

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