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.



4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
# File 'lib/prism/node.rb', line 4834

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



4879
4880
4881
# File 'lib/prism/node.rb', line 4879

def name
  @name
end

#valueObject (readonly)

attr_reader value: Prism::node



4908
4909
4910
# File 'lib/prism/node.rb', line 4908

def value
  @value
end

Class Method Details

.typeObject

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



4926
4927
4928
# File 'lib/prism/node.rb', line 4926

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.



4932
4933
4934
4935
4936
4937
4938
# File 'lib/prism/node.rb', line 4932

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



4846
4847
4848
# File 'lib/prism/node.rb', line 4846

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

#child_nodesObject Also known as: deconstruct

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



4851
4852
4853
# File 'lib/prism/node.rb', line 4851

def child_nodes
  [value]
end

#comment_targetsObject

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



4861
4862
4863
# File 'lib/prism/node.rb', line 4861

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

#compact_child_nodesObject

def compact_child_nodes: () -> Array



4856
4857
4858
# File 'lib/prism/node.rb', line 4856

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



4866
4867
4868
# File 'lib/prism/node.rb', line 4866

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 }



4874
4875
4876
# File 'lib/prism/node.rb', line 4874

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



4916
4917
4918
# File 'lib/prism/node.rb', line 4916

def inspect
  InspectVisitor.compose(self)
end

#name_locObject

attr_reader name_loc: Location



4882
4883
4884
4885
4886
# File 'lib/prism/node.rb', line 4882

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



4911
4912
4913
# File 'lib/prism/node.rb', line 4911

def operator
  operator_loc.slice
end

#operator_locObject

attr_reader operator_loc: Location



4895
4896
4897
4898
4899
# File 'lib/prism/node.rb', line 4895

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.



4890
4891
4892
# File 'lib/prism/node.rb', line 4890

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.



4903
4904
4905
# File 'lib/prism/node.rb', line 4903

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

#typeObject

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



4921
4922
4923
# File 'lib/prism/node.rb', line 4921

def type
  :constant_or_write_node
end