Class: Prism::LocalVariableWriteNode

Inherits:
PrismNode
  • Object
show all
Defined in:
lib/prism/node.rb,
lib/prism/parse_result/newlines.rb,
sig/generated/prism/node.rbs,
sig/generated/prism/parse_result/newlines.rbs,
ext/prism/api_node.c

Overview

Represents writing to a local variable.

foo = 1
^^^^^^^

Explore code sample

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initialize a new LocalVariableWriteNode node.

: (Source source, Integer node_id, Location location, Integer flags, Symbol name, Integer depth, Location name_loc, Prism::node value, Location operator_loc) -> void

Parameters:

  • source (Source)
  • node_id (Integer)
  • location (Location)
  • flags (Integer)
  • name (Symbol)
  • depth (Integer)
  • name_loc (Location)
  • value (Prism::node)
  • operator_loc (Location)


22378
22379
22380
22381
22382
22383
22384
22385
22386
22387
22388
# File 'lib/prism/node.rb', line 22378

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

Class Method Details

.type ⇒ :local_variable_write_node

See Node.type.

: () -> :local_variable_write_node

Returns:

  • (:local_variable_write_node)


22472
22473
22474
# File 'lib/prism/node.rb', line 22472

def self.type
  :local_variable_write_node
end

Instance Method Details

#===(other) ⇒ boolish

: (untyped other) -> boolish

Parameters:

  • other (Object)

Returns:

  • (boolish)


22622
22623
22624
22625
22626
22627
22628
22629
# File 'lib/prism/node.rb', line 22622

def ===(other) # :nodoc:
  other.is_a?(LocalVariableWriteNode) &&
    (name === other.name) &&
    (depth === other.depth) &&
    (name_loc.nil? == other.name_loc.nil?) &&
    (value === other.value) &&
    (operator_loc.nil? == other.operator_loc.nil?)
end

#accept(visitor) ⇒ Object

See Node.accept.

: (_Visitor visitor) -> untyped

Parameters:

Returns:

  • (Object)


22404
22405
22406
# File 'lib/prism/node.rb', line 22404

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

#child_nodes ⇒ Array[node?] Also known as: deconstruct

See Node.child_nodes.

: () -> Array

Returns:

  • (Array[node?])


22411
22412
22413
# File 'lib/prism/node.rb', line 22411

def child_nodes
  [value]
end

#compact_child_nodes ⇒ Array[node]

See Node.compact_child_nodes.

: () -> Array

Returns:

  • (Array[node])


22428
22429
22430
# File 'lib/prism/node.rb', line 22428

def compact_child_nodes
  [value]
end

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

:call-seq:

copy(**fields) -> LocalVariableWriteNode

Creates a copy of self with the given fields, using self as the template.

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

Parameters:

  • node_id: (Integer) (defaults to: self.node_id)
  • location: (Location) (defaults to: self.location)
  • flags: (Integer) (defaults to: self.flags)
  • name: (Symbol) (defaults to: self.name)
  • depth: (Integer) (defaults to: self.depth)
  • name_loc: (Location) (defaults to: self.name_loc)
  • value: (Prism::node) (defaults to: self.value)
  • operator_loc: (Location) (defaults to: self.operator_loc)

Returns:



22438
22439
22440
# File 'lib/prism/node.rb', line 22438

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

#deconstruct_keys(keys) ⇒ Hash[Symbol, untyped]

: (Array? keys) -> Hash[Symbol, untyped]

Parameters:

  • keys (Array[Symbol], nil)

Returns:

  • (Hash[Symbol, untyped])


22445
22446
22447
22448
22449
22450
22451
22452
22453
22454
22455
22456
22457
22458
22459
22460
# File 'lib/prism/node.rb', line 22445

def deconstruct_keys(keys) # :nodoc:
  (keys || i[node_id location name depth name_loc value operator_loc operator]).each_with_object(
    {} #: Hash[Symbol, untyped]
  ) do |key, deconstructed|
    case key
    when :node_id                 then deconstructed[:node_id] = self.node_id
    when :location                then deconstructed[:location] = self.location
    when :name                    then deconstructed[:name] = self.name
    when :depth                   then deconstructed[:depth] = self.depth
    when :name_loc                then deconstructed[:name_loc] = self.name_loc
    when :value                   then deconstructed[:value] = self.value
    when :operator_loc            then deconstructed[:operator_loc] = self.operator_loc
    when :operator                then deconstructed[:operator] = self.operator
    end
  end
end

#depth ⇒ Integer

:call-seq:

depth -> Integer

The number of semantic scopes we have to traverse to find the declaration of this variable.

foo = 1         # depth 0

tap { foo = 1 } # depth 1

Explore code sample The specific rules for calculating the depth may differ from individual Ruby implementations, as they are not specified by the language. For more information, see the Prism documentation.

: () -> Integer

Returns:

  • (Integer)


22517
22518
22519
# File 'lib/prism/node.rb', line 22517

def depth
  @depth
end

#each_child_node ⇒ Enumerator[node, void] #each_child_node ⇒ void

See Node.each_child_node.

: () -> Enumerator[node, void] : () { (node) -> void } -> void

Overloads:

  • #each_child_node ⇒ Enumerator[node, void]

    Returns:

    • (Enumerator[node, void])
  • #each_child_node ⇒ void

    This method returns an undefined value.

Yields:

Yield Parameters:

  • arg0 (node)

Yield Returns:

  • (void)


22419
22420
22421
22422
22423
# File 'lib/prism/node.rb', line 22419

def each_child_node(&blk)
  return to_enum(:each_child_node) unless block_given?

  yield value
end

#inspect ⇒ String

: () -> String

Returns:

  • (String)


22477
22478
22479
# File 'lib/prism/node.rb', line 22477

def inspect # :nodoc:
  InspectVisitor.compose(self)
end

#name ⇒ Symbol

:call-seq:

name -> Symbol

The name of the local variable, which is an identifier.

foo = :bar # name `:foo`

abc = 123  # name `:abc`

Explore code sample

: () -> Symbol

Returns:

  • (Symbol)


22497
22498
22499
# File 'lib/prism/node.rb', line 22497

def name
  @name
end

#name_loc ⇒ Location

:category: Locations :call-seq:

name_loc -> Location

The Location of the variable name.

foo = :bar
^^^

Explore code sample

: () -> Location

Returns:



22535
22536
22537
22538
22539
# File 'lib/prism/node.rb', line 22535

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

#newline_flag!(lines) ⇒ void

This method returns an undefined value.

: (Array lines) -> void

Parameters:

  • lines (Array[bool])


298
299
300
# File 'lib/prism/parse_result/newlines.rb', line 298

def newline_flag!(lines) # :nodoc:
  value.newline_flag!(lines)
end

#operator ⇒ String

:call-seq:

operator -> String

Slice the location of operator_loc from the source.

: () -> String

Returns:

  • (String)


22615
22616
22617
# File 'lib/prism/node.rb', line 22615

def operator
  operator_loc.slice
end

#operator_loc ⇒ Location

:category: Locations :call-seq:

operator_loc -> Location

The Location of the = operator.

x = :y
  ^

Explore code sample

: () -> Location

Returns:



22592
22593
22594
22595
22596
# File 'lib/prism/node.rb', line 22592

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) ⇒ Relocation::Entry

:category: Repository Save the name_loc location using the given saved source so that it can be retrieved later.

: (_Repository repository) -> Relocation::Entry

Parameters:

Returns:



22546
22547
22548
# File 'lib/prism/node.rb', line 22546

def save_name_loc(repository)
  repository.enter(node_id, :name_loc)
end

#save_operator_loc(repository) ⇒ Relocation::Entry

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

: (_Repository repository) -> Relocation::Entry

Parameters:

Returns:



22603
22604
22605
# File 'lib/prism/node.rb', line 22603

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

#type ⇒ :local_variable_write_node

See Node#type.

: () -> :local_variable_write_node

Returns:

  • (:local_variable_write_node)


22465
22466
22467
# File 'lib/prism/node.rb', line 22465

def type
  :local_variable_write_node
end

#value ⇒ Prism::node

:call-seq:

value -> Node

The value to write to the local variable. It can be any non-void expression.

foo = :bar
      ^^^^

abc = 1234
      ^^^^

Explore code sample Note that since the name of a local variable is known before the value is parsed, it is valid for a local variable to appear within the value of its own write.

foo = foo

Explore code sample

: () -> Prism::node

Returns:

  • (Prism::node)


22574
22575
22576
# File 'lib/prism/node.rb', line 22574

def value
  @value
end