Skip to content

Commit

Permalink
Handle alias_method
Browse files Browse the repository at this point in the history
Co-authored-by: Vinicius Stock <vinistock@users.noreply.github.com>
  • Loading branch information
nikijiandani and vinistock committed May 1, 2024
1 parent aa5a057 commit 0a55ab4
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 3 deletions.
43 changes: 41 additions & 2 deletions lib/ruby_indexer/lib/ruby_indexer/collector.rb
Expand Up @@ -154,6 +154,8 @@ def handle_call_node(node)
handle_attribute(node, reader: false, writer: true)
when :attr_accessor
handle_attribute(node, reader: true, writer: true)
when :alias_method
handle_alias_method(node)
when :include
handle_include(node)
when :prepend
Expand Down Expand Up @@ -191,13 +193,50 @@ def handle_def_node(node)
def handle_alias_node(node)
method_name = node.new_name.slice
comments = collect_comments(node)
@index << Entry::InstanceMethod.new(
@index << Entry::UnresolvedMethodAlias.new(
method_name,
node.old_name.slice,
@current_owner,
@file_path,
node.new_name.location,
comments,
nil,
)
end

sig { params(node: Prism::CallNode).void }
def handle_alias_method(node)
arguments = node.arguments&.arguments
return unless arguments

new_name, old_name = arguments
return unless new_name && old_name

new_name_value = case new_name
when Prism::StringNode
new_name.content
when Prism::SymbolNode
new_name.value
end

return unless new_name_value

old_name_value = case old_name
when Prism::StringNode
old_name.content
when Prism::SymbolNode
old_name.value
end

return unless old_name_value

comments = collect_comments(node)
@index << Entry::UnresolvedMethodAlias.new(
new_name_value,
old_name_value,
@current_owner,
@file_path,
new_name.location,
comments,
)
end

Expand Down
28 changes: 28 additions & 0 deletions lib/ruby_indexer/lib/ruby_indexer/entry.rb
Expand Up @@ -380,5 +380,33 @@ def initialize(target, unresolved_alias)
@target = target
end
end

class UnresolvedMethodAlias < Entry
extend T::Sig

sig { returns(String) }
attr_reader :new_name, :old_name

sig { returns(T.nilable(Entry::Namespace)) }
attr_reader :owner

sig do
params(
new_name: String,
old_name: String,
owner: T.nilable(Entry::Namespace),
file_path: String,
location: T.any(Prism::Location, RubyIndexer::Location),
comments: T::Array[String],
).void
end
def initialize(new_name, old_name, owner, file_path, location, comments) # rubocop:disable Metrics/ParameterLists
super(new_name, file_path, location, comments)

@new_name = new_name
@old_name = old_name
@owner = owner
end
end
end
end
9 changes: 8 additions & 1 deletion lib/ruby_indexer/test/method_test.rb
Expand Up @@ -290,10 +290,17 @@ def test_keeps_track_of_aliases
index(<<~RUBY)
class Foo
alias whatever to_s
alias_method :foo, :to_a
alias_method "bar", "to_a"
alias_method baz, :to_a
alias_method :baz
end
RUBY

assert_entry("whatever", Entry::InstanceMethod, "/fake/path/foo.rb:1-8:1-16")
assert_entry("whatever", Entry::UnresolvedMethodAlias, "/fake/path/foo.rb:1-8:1-16")
assert_entry("foo", Entry::UnresolvedMethodAlias, "/fake/path/foo.rb:2-15:2-19")
assert_entry("bar", Entry::UnresolvedMethodAlias, "/fake/path/foo.rb:3-15:3-20")
assert_equal(4, @index.instance_variable_get(:@entries).length)
end
end
end

0 comments on commit 0a55ab4

Please sign in to comment.