Class: Prism::LocalVariableOrWriteNode

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 local variable.

target ||= value
^^^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, name_loc, operator_loc, value, name, depth) ⇒ LocalVariableOrWriteNode

Initialize a new LocalVariableOrWriteNode node.



11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
# File 'lib/prism/node.rb', line 11919

def initialize(source, node_id, location, flags, name_loc, operator_loc, value, name, depth)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @name_loc = name_loc
  @operator_loc = operator_loc
  @value = value
  @name = name
  @depth = depth
end

Instance Attribute Details

#depthObject (readonly)

attr_reader depth: Integer



11997
11998
11999
# File 'lib/prism/node.rb', line 11997

def depth
  @depth
end

#nameObject (readonly)

attr_reader name: Symbol



11994
11995
11996
# File 'lib/prism/node.rb', line 11994

def name
  @name
end

#valueObject (readonly)

attr_reader value: Prism::node



11991
11992
11993
# File 'lib/prism/node.rb', line 11991

def value
  @value
end

Class Method Details

.typeObject

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



12015
12016
12017
# File 'lib/prism/node.rb', line 12015

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



12021
12022
12023
12024
12025
12026
12027
12028
# File 'lib/prism/node.rb', line 12021

def ===(other)
  other.is_a?(LocalVariableOrWriteNode) &&
    (name_loc.nil? == other.name_loc.nil?) &&
    (operator_loc.nil? == other.operator_loc.nil?) &&
    (value === other.value) &&
    (name === other.name) &&
    (depth === other.depth)
end

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



11932
11933
11934
# File 'lib/prism/node.rb', line 11932

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

#child_nodesObject Also known as: deconstruct

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



11937
11938
11939
# File 'lib/prism/node.rb', line 11937

def child_nodes
  [value]
end

#comment_targetsObject

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



11947
11948
11949
# File 'lib/prism/node.rb', line 11947

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

#compact_child_nodesObject

def compact_child_nodes: () -> Array



11942
11943
11944
# File 'lib/prism/node.rb', line 11942

def compact_child_nodes
  [value]
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, name_loc: self.name_loc, operator_loc: self.operator_loc, value: self.value, name: self.name, depth: self.depth) ⇒ Object

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?name_loc: Location, ?operator_loc: Location, ?value: Prism::node, ?name: Symbol, ?depth: Integer) -> LocalVariableOrWriteNode



11952
11953
11954
# File 'lib/prism/node.rb', line 11952

def copy(node_id: self.node_id, location: self.location, flags: self.flags, name_loc: self.name_loc, operator_loc: self.operator_loc, value: self.value, name: self.name, depth: self.depth)
  LocalVariableOrWriteNode.new(source, node_id, location, flags, name_loc, operator_loc, value, name, depth)
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, name_loc: Location, operator_loc: Location, value: Prism::node, name: Symbol, depth: Integer }



11960
11961
11962
# File 'lib/prism/node.rb', line 11960

def deconstruct_keys(keys)
  { node_id: node_id, location: location, name_loc: name_loc, operator_loc: operator_loc, value: value, name: name, depth: depth }
end

#desugarObject

:nodoc:



242
243
244
# File 'lib/prism/desugar_compiler.rb', line 242

def desugar # :nodoc:
  DesugarOrWriteNode.new(self, source, :local_variable_read_node, :local_variable_write_node, name: name, depth: depth).compile
end

#inspectObject

def inspect -> String



12005
12006
12007
# File 'lib/prism/node.rb', line 12005

def inspect
  InspectVisitor.compose(self)
end

#name_locObject

attr_reader name_loc: Location



11965
11966
11967
11968
11969
# File 'lib/prism/node.rb', line 11965

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



12000
12001
12002
# File 'lib/prism/node.rb', line 12000

def operator
  operator_loc.slice
end

#operator_locObject

attr_reader operator_loc: Location



11978
11979
11980
11981
11982
# File 'lib/prism/node.rb', line 11978

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.



11973
11974
11975
# File 'lib/prism/node.rb', line 11973

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.



11986
11987
11988
# File 'lib/prism/node.rb', line 11986

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

#typeObject

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



12010
12011
12012
# File 'lib/prism/node.rb', line 12010

def type
  :local_variable_or_write_node
end