Class: Prism::ConstantOrWriteNode

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 constant.

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) ⇒ ConstantOrWriteNode

Initialize a new ConstantOrWriteNode node.



5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
# File 'lib/prism/node.rb', line 5169

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



5221
5222
5223
# File 'lib/prism/node.rb', line 5221

def name
  @name
end

#valueObject (readonly)

attr_reader value: Prism::node



5250
5251
5252
# File 'lib/prism/node.rb', line 5250

def value
  @value
end

Class Method Details

.typeObject

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



5268
5269
5270
# File 'lib/prism/node.rb', line 5268

def self.type
  :constant_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.



5274
5275
5276
5277
5278
5279
5280
# File 'lib/prism/node.rb', line 5274

def ===(other)
  other.is_a?(ConstantOrWriteNode) &&
    (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



5181
5182
5183
# File 'lib/prism/node.rb', line 5181

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



5186
5187
5188
# File 'lib/prism/node.rb', line 5186

def child_nodes
  [value]
end

#comment_targetsObject

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



5203
5204
5205
# File 'lib/prism/node.rb', line 5203

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

#compact_child_nodesObject

def compact_child_nodes: () -> Array



5198
5199
5200
# File 'lib/prism/node.rb', line 5198

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) -> ConstantOrWriteNode



5208
5209
5210
# File 'lib/prism/node.rb', line 5208

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)
  ConstantOrWriteNode.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 }



5216
5217
5218
# File 'lib/prism/node.rb', line 5216

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:



189
190
191
# File 'lib/prism/desugar_compiler.rb', line 189

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

#each_child_node {|value| ... } ⇒ Object

def each_child_node: () { (Prism::node) -> void } -> void | () -> Enumerator

Yields:



5191
5192
5193
5194
5195
# File 'lib/prism/node.rb', line 5191

def each_child_node
  return to_enum(:each_child_node) unless block_given?

  yield value
end

#inspectObject

def inspect -> String



5258
5259
5260
# File 'lib/prism/node.rb', line 5258

def inspect
  InspectVisitor.compose(self)
end

#name_locObject

attr_reader name_loc: Location



5224
5225
5226
5227
5228
# File 'lib/prism/node.rb', line 5224

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



5253
5254
5255
# File 'lib/prism/node.rb', line 5253

def operator
  operator_loc.slice
end

#operator_locObject

attr_reader operator_loc: Location



5237
5238
5239
5240
5241
# File 'lib/prism/node.rb', line 5237

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.



5232
5233
5234
# File 'lib/prism/node.rb', line 5232

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.



5245
5246
5247
# File 'lib/prism/node.rb', line 5245

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

#typeObject

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



5263
5264
5265
# File 'lib/prism/node.rb', line 5263

def type
  :constant_or_write_node
end