Class: Prism::AliasGlobalVariableNode

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

Overview

Represents the use of the alias keyword to alias a global variable.

alias $foo $bar
^^^^^^^^^^^^^^^

Explore code sample

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, new_name, old_name, keyword_loc) ⇒ AliasGlobalVariableNode

Initialize a new AliasGlobalVariableNode node.

: (Source source, Integer node_id, Location location, Integer flags, (GlobalVariableReadNode | BackReferenceReadNode | NumberedReferenceReadNode) new_name, (GlobalVariableReadNode | BackReferenceReadNode | NumberedReferenceReadNode) old_name, Location keyword_loc) -> void

Parameters:



433
434
435
436
437
438
439
440
441
# File 'lib/prism/node.rb', line 433

def initialize(source, node_id, location, flags, new_name, old_name, keyword_loc)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @new_name = new_name
  @old_name = old_name
  @keyword_loc = keyword_loc
end

Class Method Details

.type ⇒ :alias_global_variable_node

See Node.type.

: () -> :alias_global_variable_node

Returns:

  • (:alias_global_variable_node)


524
525
526
# File 'lib/prism/node.rb', line 524

def self.type
  :alias_global_variable_node
end

Instance Method Details

#===(other) ⇒ boolish

: (untyped other) -> boolish

Parameters:

  • other (Object)

Returns:

  • (boolish)


613
614
615
616
617
618
# File 'lib/prism/node.rb', line 613

def ===(other) # :nodoc:
  other.is_a?(AliasGlobalVariableNode) &&
    (new_name === other.new_name) &&
    (old_name === other.old_name) &&
    (keyword_loc.nil? == other.keyword_loc.nil?)
end

#accept(visitor) ⇒ Object

See Node.accept.

: (_Visitor visitor) -> untyped

Parameters:

Returns:

  • (Object)


457
458
459
# File 'lib/prism/node.rb', line 457

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

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

See Node.child_nodes.

: () -> Array

Returns:

  • (Array[node?])


464
465
466
# File 'lib/prism/node.rb', line 464

def child_nodes
  [new_name, old_name]
end

#compact_child_nodes ⇒ Array[node]

See Node.compact_child_nodes.

: () -> Array

Returns:

  • (Array[node])


482
483
484
# File 'lib/prism/node.rb', line 482

def compact_child_nodes
  [new_name, old_name]
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, new_name: self.new_name, old_name: self.old_name, keyword_loc: self.keyword_loc) ⇒ AliasGlobalVariableNode

:call-seq:

copy(**fields) -> AliasGlobalVariableNode

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

: (?node_id: Integer, ?location: Location, ?flags: Integer, ?new_name: (GlobalVariableReadNode | BackReferenceReadNode | NumberedReferenceReadNode), ?old_name: (GlobalVariableReadNode | BackReferenceReadNode | NumberedReferenceReadNode), ?keyword_loc: Location) -> AliasGlobalVariableNode

Parameters:

Returns:



492
493
494
# File 'lib/prism/node.rb', line 492

def copy(node_id: self.node_id, location: self.location, flags: self.flags, new_name: self.new_name, old_name: self.old_name, keyword_loc: self.keyword_loc)
  AliasGlobalVariableNode.new(source, node_id, location, flags, new_name, old_name, keyword_loc)
end

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

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

Parameters:

  • keys (Array[Symbol], nil)

Returns:

  • (Hash[Symbol, untyped])


499
500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/prism/node.rb', line 499

def deconstruct_keys(keys) # :nodoc:
  (keys || %i[node_id location new_name old_name keyword_loc keyword]).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 :new_name                then deconstructed[:new_name] = self.new_name
    when :old_name                then deconstructed[:old_name] = self.old_name
    when :keyword_loc             then deconstructed[:keyword_loc] = self.keyword_loc
    when :keyword                 then deconstructed[:keyword] = self.keyword
    end
  end
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)


472
473
474
475
476
477
# File 'lib/prism/node.rb', line 472

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

  yield new_name
  yield old_name
end

#inspect ⇒ String

: () -> String

Returns:

  • (String)


529
530
531
# File 'lib/prism/node.rb', line 529

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

#keyword ⇒ String

:call-seq:

keyword -> String

Slice the location of keyword_loc from the source.

: () -> String

Returns:

  • (String)


606
607
608
# File 'lib/prism/node.rb', line 606

def keyword
  keyword_loc.slice
end

#keyword_loc ⇒ Location

:category: Locations :call-seq:

keyword_loc -> Location

The Location of the alias keyword.

alias $foo $bar
^^^^^

Explore code sample

: () -> Location

Returns:



583
584
585
586
587
# File 'lib/prism/node.rb', line 583

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

#new_name ⇒ GlobalVariableReadNode, ...

:call-seq:

new_name -> GlobalVariableReadNode | BackReferenceReadNode | NumberedReferenceReadNode

Represents the new name of the global variable that can be used after aliasing.

alias $foo $bar
      ^^^^

Explore code sample

: () -> (GlobalVariableReadNode | BackReferenceReadNode | NumberedReferenceReadNode)



548
549
550
# File 'lib/prism/node.rb', line 548

def new_name
  @new_name
end

#old_name ⇒ GlobalVariableReadNode, ...

:call-seq:

old_name -> GlobalVariableReadNode | BackReferenceReadNode | NumberedReferenceReadNode

Represents the old name of the global variable that can be used before aliasing.

alias $foo $bar
           ^^^^

Explore code sample

: () -> (GlobalVariableReadNode | BackReferenceReadNode | NumberedReferenceReadNode)



565
566
567
# File 'lib/prism/node.rb', line 565

def old_name
  @old_name
end

#save_keyword_loc(repository) ⇒ Relocation::Entry

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

: (_Repository repository) -> Relocation::Entry

Parameters:

Returns:



594
595
596
# File 'lib/prism/node.rb', line 594

def save_keyword_loc(repository)
  repository.enter(node_id, :keyword_loc)
end

#type ⇒ :alias_global_variable_node

See Node#type.

: () -> :alias_global_variable_node

Returns:

  • (:alias_global_variable_node)


517
518
519
# File 'lib/prism/node.rb', line 517

def type
  :alias_global_variable_node
end