Class: Prism::AliasMethodNode

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 method.

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) ⇒ AliasMethodNode

Initialize a new AliasMethodNode node.

: (Source source, Integer node_id, Location location, Integer flags, (SymbolNode | InterpolatedSymbolNode) new_name, (SymbolNode | InterpolatedSymbolNode) old_name, Location keyword_loc) -> void

Parameters:



637
638
639
640
641
642
643
644
645
# File 'lib/prism/node.rb', line 637

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_method_node

See Node.type.

: () -> :alias_method_node

Returns:

  • (:alias_method_node)


728
729
730
# File 'lib/prism/node.rb', line 728

def self.type
  :alias_method_node
end

Instance Method Details

#===(other) ⇒ boolish

: (untyped other) -> boolish

Parameters:

  • other (Object)

Returns:

  • (boolish)


829
830
831
832
833
834
# File 'lib/prism/node.rb', line 829

def ===(other) # :nodoc:
  other.is_a?(AliasMethodNode) &&
    (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)


661
662
663
# File 'lib/prism/node.rb', line 661

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

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

See Node.child_nodes.

: () -> Array

Returns:

  • (Array[node?])


668
669
670
# File 'lib/prism/node.rb', line 668

def child_nodes
  [new_name, old_name]
end

#compact_child_nodes ⇒ Array[node]

See Node.compact_child_nodes.

: () -> Array

Returns:

  • (Array[node])


686
687
688
# File 'lib/prism/node.rb', line 686

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) ⇒ AliasMethodNode

:call-seq:

copy(**fields) -> AliasMethodNode

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

: (?node_id: Integer, ?location: Location, ?flags: Integer, ?new_name: (SymbolNode | InterpolatedSymbolNode), ?old_name: (SymbolNode | InterpolatedSymbolNode), ?keyword_loc: Location) -> AliasMethodNode

Parameters:

Returns:



696
697
698
# File 'lib/prism/node.rb', line 696

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)
  AliasMethodNode.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])


703
704
705
706
707
708
709
710
711
712
713
714
715
716
# File 'lib/prism/node.rb', line 703

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)


676
677
678
679
680
681
# File 'lib/prism/node.rb', line 676

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)


733
734
735
# File 'lib/prism/node.rb', line 733

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)


822
823
824
# File 'lib/prism/node.rb', line 822

def keyword
  keyword_loc.slice
end

#keyword_loc ⇒ Location

:category: Locations :call-seq:

keyword_loc -> Location

Represents the Location of the alias keyword.

alias foo bar
^^^^^

Explore code sample

: () -> Location

Returns:



799
800
801
802
803
# File 'lib/prism/node.rb', line 799

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 ⇒ SymbolNode, InterpolatedSymbolNode

:call-seq:

new_name -> SymbolNode | InterpolatedSymbolNode

Represents the new name of the method that will be aliased.

alias foo bar
      ^^^

alias :foo :bar
      ^^^^

alias :"#{foo}" :"#{bar}"
      ^^^^^^^^^

Explore code sample

: () -> (SymbolNode | InterpolatedSymbolNode)



758
759
760
# File 'lib/prism/node.rb', line 758

def new_name
  @new_name
end

#old_name ⇒ SymbolNode, InterpolatedSymbolNode

:call-seq:

old_name -> SymbolNode | InterpolatedSymbolNode

Represents the old name of the method that will be aliased.

alias foo bar
          ^^^

alias :foo :bar
           ^^^^

alias :"#{foo}" :"#{bar}"
                ^^^^^^^^^

Explore code sample

: () -> (SymbolNode | InterpolatedSymbolNode)



781
782
783
# File 'lib/prism/node.rb', line 781

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:



810
811
812
# File 'lib/prism/node.rb', line 810

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

#type ⇒ :alias_method_node

See Node#type.

: () -> :alias_method_node

Returns:

  • (:alias_method_node)


721
722
723
# File 'lib/prism/node.rb', line 721

def type
  :alias_method_node
end