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.



7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
# File 'lib/prism/node.rb', line 7780

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



7825
7826
7827
# File 'lib/prism/node.rb', line 7825

def name
  @name
end

#valueObject (readonly)

attr_reader value: Prism::node



7854
7855
7856
# File 'lib/prism/node.rb', line 7854

def value
  @value
end

Class Method Details

.typeObject

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



7872
7873
7874
# File 'lib/prism/node.rb', line 7872

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.



7878
7879
7880
7881
7882
7883
7884
# File 'lib/prism/node.rb', line 7878

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



7792
7793
7794
# File 'lib/prism/node.rb', line 7792

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

#child_nodesObject Also known as: deconstruct

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



7797
7798
7799
# File 'lib/prism/node.rb', line 7797

def child_nodes
  [value]
end

#comment_targetsObject

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



7807
7808
7809
# File 'lib/prism/node.rb', line 7807

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

#compact_child_nodesObject

def compact_child_nodes: () -> Array



7802
7803
7804
# File 'lib/prism/node.rb', line 7802

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



7812
7813
7814
# File 'lib/prism/node.rb', line 7812

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 }



7820
7821
7822
# File 'lib/prism/node.rb', line 7820

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:



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

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

#inspectObject

def inspect -> String



7862
7863
7864
# File 'lib/prism/node.rb', line 7862

def inspect
  InspectVisitor.compose(self)
end

#name_locObject

attr_reader name_loc: Location



7828
7829
7830
7831
7832
# File 'lib/prism/node.rb', line 7828

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



7857
7858
7859
# File 'lib/prism/node.rb', line 7857

def operator
  operator_loc.slice
end

#operator_locObject

attr_reader operator_loc: Location



7841
7842
7843
7844
7845
# File 'lib/prism/node.rb', line 7841

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.



7836
7837
7838
# File 'lib/prism/node.rb', line 7836

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.



7849
7850
7851
# File 'lib/prism/node.rb', line 7849

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

#typeObject

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



7867
7868
7869
# File 'lib/prism/node.rb', line 7867

def type
  :global_variable_or_write_node
end