Class: Prism::ClassVariableOrWriteNode

Inherits:
PrismNode
  • Object
show all
Defined in:
lib/prism/node.rb,
lib/prism/desugar_compiler.rb,
ext/prism/api_node.c

Overview

Represents the use of the ‘||=` operator for assignment to a class variable.

@@target ||= value
^^^^^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, name, name_loc, operator_loc, value) ⇒ ClassVariableOrWriteNode

Initialize a new ClassVariableOrWriteNode node.



4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
# File 'lib/prism/node.rb', line 4216

def initialize(source, node_id, location, flags, name, name_loc, operator_loc, value)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @name = name
  @name_loc = name_loc
  @operator_loc = operator_loc
  @value = value
end

Instance Attribute Details

#nameObject (readonly)

attr_reader name: Symbol



4261
4262
4263
# File 'lib/prism/node.rb', line 4261

def name
  @name
end

#valueObject (readonly)

attr_reader value: Prism::node



4290
4291
4292
# File 'lib/prism/node.rb', line 4290

def value
  @value
end

Class Method Details

.typeObject

Return a symbol representation of this node type. See ‘Node::type`.



4308
4309
4310
# File 'lib/prism/node.rb', line 4308

def self.type
  :class_variable_or_write_node
end

Instance Method Details

#===(other) ⇒ Object

Implements case-equality for the node. This is effectively == but without comparing the value of locations. Locations are checked only for presence.



4314
4315
4316
4317
4318
4319
4320
# File 'lib/prism/node.rb', line 4314

def ===(other)
  other.is_a?(ClassVariableOrWriteNode) &&
    (name === other.name) &&
    (name_loc.nil? == other.name_loc.nil?) &&
    (operator_loc.nil? == other.operator_loc.nil?) &&
    (value === other.value)
end

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



4228
4229
4230
# File 'lib/prism/node.rb', line 4228

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array[nil | Node]



4233
4234
4235
# File 'lib/prism/node.rb', line 4233

def child_nodes
  [value]
end

#comment_targetsObject

def comment_targets: () -> Array[Node | Location]



4243
4244
4245
# File 'lib/prism/node.rb', line 4243

def comment_targets
  [name_loc, operator_loc, value] #: Array[Prism::node | Location]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



4238
4239
4240
# File 'lib/prism/node.rb', line 4238

def compact_child_nodes
  [value]
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, name: self.name, name_loc: self.name_loc, operator_loc: self.operator_loc, value: self.value) ⇒ Object

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?name: Symbol, ?name_loc: Location, ?operator_loc: Location, ?value: Prism::node) -> ClassVariableOrWriteNode



4248
4249
4250
# File 'lib/prism/node.rb', line 4248

def copy(node_id: self.node_id, location: self.location, flags: self.flags, name: self.name, name_loc: self.name_loc, operator_loc: self.operator_loc, value: self.value)
  ClassVariableOrWriteNode.new(source, node_id, location, flags, name, name_loc, operator_loc, value)
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, name: Symbol, name_loc: Location, operator_loc: Location, value: Prism::node }



4256
4257
4258
# File 'lib/prism/node.rb', line 4256

def deconstruct_keys(keys)
  { node_id: node_id, location: location, name: name, name_loc: name_loc, operator_loc: operator_loc, value: value }
end

#desugarObject

:nodoc:



170
171
172
# File 'lib/prism/desugar_compiler.rb', line 170

def desugar # :nodoc:
  DesugarOrWriteDefinedNode.new(self, source, :class_variable_read_node, :class_variable_write_node, name: name).compile
end

#inspectObject

def inspect -> String



4298
4299
4300
# File 'lib/prism/node.rb', line 4298

def inspect
  InspectVisitor.compose(self)
end

#name_locObject

attr_reader name_loc: Location



4264
4265
4266
4267
4268
# File 'lib/prism/node.rb', line 4264

def name_loc
  location = @name_loc
  return location if location.is_a?(Location)
  @name_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end

#operatorObject

def operator: () -> String



4293
4294
4295
# File 'lib/prism/node.rb', line 4293

def operator
  operator_loc.slice
end

#operator_locObject

attr_reader operator_loc: Location



4277
4278
4279
4280
4281
# File 'lib/prism/node.rb', line 4277

def operator_loc
  location = @operator_loc
  return location if location.is_a?(Location)
  @operator_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end

#save_name_loc(repository) ⇒ Object

Save the name_loc location using the given saved source so that it can be retrieved later.



4272
4273
4274
# File 'lib/prism/node.rb', line 4272

def save_name_loc(repository)
  repository.enter(node_id, :name_loc)
end

#save_operator_loc(repository) ⇒ Object

Save the operator_loc location using the given saved source so that it can be retrieved later.



4285
4286
4287
# File 'lib/prism/node.rb', line 4285

def save_operator_loc(repository)
  repository.enter(node_id, :operator_loc)
end

#typeObject

Return a symbol representation of this node type. See ‘Node#type`.



4303
4304
4305
# File 'lib/prism/node.rb', line 4303

def type
  :class_variable_or_write_node
end