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.



4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
# File 'lib/prism/node.rb', line 4157

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



4202
4203
4204
# File 'lib/prism/node.rb', line 4202

def name
  @name
end

#valueObject (readonly)

attr_reader value: Prism::node



4219
4220
4221
# File 'lib/prism/node.rb', line 4219

def value
  @value
end

Class Method Details

.typeObject

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



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

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.



4243
4244
4245
4246
4247
4248
4249
# File 'lib/prism/node.rb', line 4243

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



4169
4170
4171
# File 'lib/prism/node.rb', line 4169

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

#child_nodesObject Also known as: deconstruct

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



4174
4175
4176
# File 'lib/prism/node.rb', line 4174

def child_nodes
  [value]
end

#comment_targetsObject

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



4184
4185
4186
# File 'lib/prism/node.rb', line 4184

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

#compact_child_nodesObject

def compact_child_nodes: () -> Array



4179
4180
4181
# File 'lib/prism/node.rb', line 4179

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



4189
4190
4191
# File 'lib/prism/node.rb', line 4189

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 }



4197
4198
4199
# File 'lib/prism/node.rb', line 4197

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:



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

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

#inspectObject

def inspect -> String



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

def inspect
  InspectVisitor.compose(self)
end

#name_locObject

attr_reader name_loc: Location



4205
4206
4207
4208
4209
# File 'lib/prism/node.rb', line 4205

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



4222
4223
4224
# File 'lib/prism/node.rb', line 4222

def operator
  operator_loc.slice
end

#operator_locObject

attr_reader operator_loc: Location



4212
4213
4214
4215
4216
# File 'lib/prism/node.rb', line 4212

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

#typeObject

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



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

def type
  :constant_or_write_node
end