Class: Prism::ConstantPathOrWriteNode

Inherits:
PrismNode
  • Object
show all
Defined in:
lib/prism/node.rb,
ext/prism/api_node.c

Overview

Represents the use of the ‘||=` operator for assignment to a constant path.

Parent::Child ||= value
^^^^^^^^^^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, target, operator_loc, value) ⇒ ConstantPathOrWriteNode

Initialize a new ConstantPathOrWriteNode node.



5644
5645
5646
5647
5648
5649
5650
5651
5652
# File 'lib/prism/node.rb', line 5644

def initialize(source, node_id, location, flags, target, operator_loc, value)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @target = target
  @operator_loc = operator_loc
  @value = value
end

Instance Attribute Details

#targetObject (readonly)

attr_reader target: ConstantPathNode



5696
5697
5698
# File 'lib/prism/node.rb', line 5696

def target
  @target
end

#valueObject (readonly)

attr_reader value: Prism::node



5712
5713
5714
# File 'lib/prism/node.rb', line 5712

def value
  @value
end

Class Method Details

.typeObject

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



5730
5731
5732
# File 'lib/prism/node.rb', line 5730

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



5736
5737
5738
5739
5740
5741
# File 'lib/prism/node.rb', line 5736

def ===(other)
  other.is_a?(ConstantPathOrWriteNode) &&
    (target === other.target) &&
    (operator_loc.nil? == other.operator_loc.nil?) &&
    (value === other.value)
end

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



5655
5656
5657
# File 'lib/prism/node.rb', line 5655

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



5660
5661
5662
# File 'lib/prism/node.rb', line 5660

def child_nodes
  [target, value]
end

#comment_targetsObject

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



5678
5679
5680
# File 'lib/prism/node.rb', line 5678

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

#compact_child_nodesObject

def compact_child_nodes: () -> Array



5673
5674
5675
# File 'lib/prism/node.rb', line 5673

def compact_child_nodes
  [target, value]
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, target: self.target, operator_loc: self.operator_loc, value: self.value) ⇒ Object

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?target: ConstantPathNode, ?operator_loc: Location, ?value: Prism::node) -> ConstantPathOrWriteNode



5683
5684
5685
# File 'lib/prism/node.rb', line 5683

def copy(node_id: self.node_id, location: self.location, flags: self.flags, target: self.target, operator_loc: self.operator_loc, value: self.value)
  ConstantPathOrWriteNode.new(source, node_id, location, flags, target, operator_loc, value)
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, target: ConstantPathNode, operator_loc: Location, value: Prism::node }



5691
5692
5693
# File 'lib/prism/node.rb', line 5691

def deconstruct_keys(keys)
  { node_id: node_id, location: location, target: target, operator_loc: operator_loc, value: value }
end

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

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

Yields:



5665
5666
5667
5668
5669
5670
# File 'lib/prism/node.rb', line 5665

def each_child_node
  return to_enum(:each_child_node) unless block_given?

  yield target
  yield value
end

#inspectObject

def inspect -> String



5720
5721
5722
# File 'lib/prism/node.rb', line 5720

def inspect
  InspectVisitor.compose(self)
end

#operatorObject

def operator: () -> String



5715
5716
5717
# File 'lib/prism/node.rb', line 5715

def operator
  operator_loc.slice
end

#operator_locObject

attr_reader operator_loc: Location



5699
5700
5701
5702
5703
# File 'lib/prism/node.rb', line 5699

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

#save_operator_loc(repository) ⇒ Object

Save the operator_loc location using the given saved source so that it can be retrieved later.



5707
5708
5709
# File 'lib/prism/node.rb', line 5707

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

#typeObject

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



5725
5726
5727
# File 'lib/prism/node.rb', line 5725

def type
  :constant_path_or_write_node
end