Class: Prism::AliasMethodNode

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

Overview

Represents the use of the ‘alias` keyword to alias a method.

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(new_name, old_name, keyword_loc, location) ⇒ AliasMethodNode

def initialize: (new_name: Node, old_name: Node, keyword_loc: Location, location: Location) -> void



150
151
152
153
154
155
# File 'lib/prism/node.rb', line 150

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

Instance Attribute Details

#keyword_locObject (readonly)

attr_reader keyword_loc: Location



147
148
149
# File 'lib/prism/node.rb', line 147

def keyword_loc
  @keyword_loc
end

#new_nameObject (readonly)

attr_reader new_name: Node



141
142
143
# File 'lib/prism/node.rb', line 141

def new_name
  @new_name
end

#old_nameObject (readonly)

attr_reader old_name: Node



144
145
146
# File 'lib/prism/node.rb', line 144

def old_name
  @old_name
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



158
159
160
# File 'lib/prism/node.rb', line 158

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

#child_nodesObject Also known as: deconstruct

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



163
164
165
# File 'lib/prism/node.rb', line 163

def child_nodes
  [new_name, old_name]
end

#comment_targetsObject

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



173
174
175
# File 'lib/prism/node.rb', line 173

def comment_targets
  [new_name, old_name, keyword_loc]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



168
169
170
# File 'lib/prism/node.rb', line 168

def compact_child_nodes
  [new_name, old_name]
end

#copy(**params) ⇒ Object

def copy: (**params) -> AliasMethodNode



178
179
180
181
182
183
184
185
# File 'lib/prism/node.rb', line 178

def copy(**params)
  AliasMethodNode.new(
    params.fetch(:new_name) { new_name },
    params.fetch(:old_name) { old_name },
    params.fetch(:keyword_loc) { keyword_loc },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (keys: Array) -> Hash[Symbol, nil | Node | Array | String | Token | Array | Location]



191
192
193
# File 'lib/prism/node.rb', line 191

def deconstruct_keys(keys)
  { new_name: new_name, old_name: old_name, keyword_loc: keyword_loc, location: location }
end

#inspect(inspector = NodeInspector.new) ⇒ Object



200
201
202
203
204
205
206
207
208
# File 'lib/prism/node.rb', line 200

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  inspector << "├── new_name:\n"
  inspector << inspector.child_node(new_name, "")
  inspector << "├── old_name:\n"
  inspector << inspector.child_node(old_name, "")
  inspector << "└── keyword_loc: #{inspector.location(keyword_loc)}\n"
  inspector.to_str
end

#keywordObject

def keyword: () -> String



196
197
198
# File 'lib/prism/node.rb', line 196

def keyword
  keyword_loc.slice
end

#typeObject

Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform. Usually this is done by calling ‘[cls1, cls2].include?(node.class)` or putting the node into a case statement and doing `case node; when cls1; when cls2; end`. Both of these approaches are relatively slow because of the constant lookups, method calls, and/or array allocations.

Instead, you can call #type, which will return to you a symbol that you can use for comparison. This is faster than the other approaches because it uses a single integer comparison, but also because if you’re on CRuby you can take advantage of the fact that case statements with all symbol keys will use a jump table.

def type: () -> Symbol



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

def type
  :alias_method_node
end