Class: Prism::InstanceVariableOrWriteNode

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 an instance 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) ⇒ InstanceVariableOrWriteNode

Initialize a new InstanceVariableOrWriteNode node.


9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
# File 'lib/prism/node.rb', line 9996

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


10041
10042
10043
# File 'lib/prism/node.rb', line 10041

def name
  @name
end

#valueObject (readonly)

attr_reader value: Prism::node


10070
10071
10072
# File 'lib/prism/node.rb', line 10070

def value
  @value
end

Class Method Details

.typeObject

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


10088
10089
10090
# File 'lib/prism/node.rb', line 10088

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


10094
10095
10096
10097
10098
10099
10100
# File 'lib/prism/node.rb', line 10094

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


10008
10009
10010
# File 'lib/prism/node.rb', line 10008

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

#child_nodesObject Also known as: deconstruct

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


10013
10014
10015
# File 'lib/prism/node.rb', line 10013

def child_nodes
  [value]
end

#comment_targetsObject

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


10023
10024
10025
# File 'lib/prism/node.rb', line 10023

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

#compact_child_nodesObject

def compact_child_nodes: () -> Array


10018
10019
10020
# File 'lib/prism/node.rb', line 10018

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


10028
10029
10030
# File 'lib/prism/node.rb', line 10028

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


10036
10037
10038
# File 'lib/prism/node.rb', line 10036

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:


224
225
226
# File 'lib/prism/desugar_compiler.rb', line 224

def desugar # :nodoc:
  DesugarOrWriteNode.new(self, source, :instance_variable_read_node, :instance_variable_write_node, name: name).compile
end

#inspectObject

def inspect -> String


10078
10079
10080
# File 'lib/prism/node.rb', line 10078

def inspect
  InspectVisitor.compose(self)
end

#name_locObject

attr_reader name_loc: Location


10044
10045
10046
10047
10048
# File 'lib/prism/node.rb', line 10044

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


10073
10074
10075
# File 'lib/prism/node.rb', line 10073

def operator
  operator_loc.slice
end

#operator_locObject

attr_reader operator_loc: Location


10057
10058
10059
10060
10061
# File 'lib/prism/node.rb', line 10057

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.


10052
10053
10054
# File 'lib/prism/node.rb', line 10052

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.


10065
10066
10067
# File 'lib/prism/node.rb', line 10065

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

#typeObject

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


10083
10084
10085
# File 'lib/prism/node.rb', line 10083

def type
  :instance_variable_or_write_node
end