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.



4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
# File 'lib/prism/node.rb', line 4120

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



4165
4166
4167
# File 'lib/prism/node.rb', line 4165

def name
  @name
end

#valueObject (readonly)

attr_reader value: Prism::node



4182
4183
4184
# File 'lib/prism/node.rb', line 4182

def value
  @value
end

Class Method Details

.typeObject

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



4200
4201
4202
# File 'lib/prism/node.rb', line 4200

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.



4206
4207
4208
4209
4210
4211
4212
# File 'lib/prism/node.rb', line 4206

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



4132
4133
4134
# File 'lib/prism/node.rb', line 4132

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

#child_nodesObject Also known as: deconstruct

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



4137
4138
4139
# File 'lib/prism/node.rb', line 4137

def child_nodes
  [value]
end

#comment_targetsObject

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



4147
4148
4149
# File 'lib/prism/node.rb', line 4147

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

#compact_child_nodesObject

def compact_child_nodes: () -> Array



4142
4143
4144
# File 'lib/prism/node.rb', line 4142

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



4152
4153
4154
# File 'lib/prism/node.rb', line 4152

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 }



4160
4161
4162
# File 'lib/prism/node.rb', line 4160

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



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

def inspect
  InspectVisitor.compose(self)
end

#name_locObject

attr_reader name_loc: Location



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

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



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

def operator
  operator_loc.slice
end

#operator_locObject

attr_reader operator_loc: Location



4175
4176
4177
4178
4179
# File 'lib/prism/node.rb', line 4175

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



4195
4196
4197
# File 'lib/prism/node.rb', line 4195

def type
  :constant_or_write_node
end