Class: Prism::GlobalVariableOrWriteNode

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 global 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) ⇒ GlobalVariableOrWriteNode

Initialize a new GlobalVariableOrWriteNode node.



8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
# File 'lib/prism/node.rb', line 8344

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



8396
8397
8398
# File 'lib/prism/node.rb', line 8396

def name
  @name
end

#valueObject (readonly)

attr_reader value: Prism::node



8425
8426
8427
# File 'lib/prism/node.rb', line 8425

def value
  @value
end

Class Method Details

.typeObject

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



8443
8444
8445
# File 'lib/prism/node.rb', line 8443

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



8449
8450
8451
8452
8453
8454
8455
# File 'lib/prism/node.rb', line 8449

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



8356
8357
8358
# File 'lib/prism/node.rb', line 8356

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



8361
8362
8363
# File 'lib/prism/node.rb', line 8361

def child_nodes
  [value]
end

#comment_targetsObject

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



8378
8379
8380
# File 'lib/prism/node.rb', line 8378

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

#compact_child_nodesObject

def compact_child_nodes: () -> Array



8373
8374
8375
# File 'lib/prism/node.rb', line 8373

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



8383
8384
8385
# File 'lib/prism/node.rb', line 8383

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



8391
8392
8393
# File 'lib/prism/node.rb', line 8391

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:



207
208
209
# File 'lib/prism/desugar_compiler.rb', line 207

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

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

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

Yields:



8366
8367
8368
8369
8370
# File 'lib/prism/node.rb', line 8366

def each_child_node
  return to_enum(:each_child_node) unless block_given?

  yield value
end

#inspectObject

def inspect -> String



8433
8434
8435
# File 'lib/prism/node.rb', line 8433

def inspect
  InspectVisitor.compose(self)
end

#name_locObject

attr_reader name_loc: Location



8399
8400
8401
8402
8403
# File 'lib/prism/node.rb', line 8399

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



8428
8429
8430
# File 'lib/prism/node.rb', line 8428

def operator
  operator_loc.slice
end

#operator_locObject

attr_reader operator_loc: Location



8412
8413
8414
8415
8416
# File 'lib/prism/node.rb', line 8412

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.



8407
8408
8409
# File 'lib/prism/node.rb', line 8407

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.



8420
8421
8422
# File 'lib/prism/node.rb', line 8420

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

#typeObject

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



8438
8439
8440
# File 'lib/prism/node.rb', line 8438

def type
  :global_variable_or_write_node
end