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.



4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
# File 'lib/prism/node.rb', line 4864

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



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

def name
  @name
end

#valueObject (readonly)

attr_reader value: Prism::node



4938
4939
4940
# File 'lib/prism/node.rb', line 4938

def value
  @value
end

Class Method Details

.typeObject

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



4956
4957
4958
# File 'lib/prism/node.rb', line 4956

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.



4962
4963
4964
4965
4966
4967
4968
# File 'lib/prism/node.rb', line 4962

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



4876
4877
4878
# File 'lib/prism/node.rb', line 4876

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



4881
4882
4883
# File 'lib/prism/node.rb', line 4881

def child_nodes
  [value]
end

#comment_targetsObject

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



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

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

#compact_child_nodesObject

def compact_child_nodes: () -> Array



4886
4887
4888
# File 'lib/prism/node.rb', line 4886

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



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

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 }



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

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:



189
190
191
# File 'lib/prism/desugar_compiler.rb', line 189

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

#inspectObject

def inspect -> String



4946
4947
4948
# File 'lib/prism/node.rb', line 4946

def inspect
  InspectVisitor.compose(self)
end

#name_locObject

attr_reader name_loc: Location



4912
4913
4914
4915
4916
# File 'lib/prism/node.rb', line 4912

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



4941
4942
4943
# File 'lib/prism/node.rb', line 4941

def operator
  operator_loc.slice
end

#operator_locObject

attr_reader operator_loc: Location



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

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.



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

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.



4933
4934
4935
# File 'lib/prism/node.rb', line 4933

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

#typeObject

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



4951
4952
4953
# File 'lib/prism/node.rb', line 4951

def type
  :constant_or_write_node
end