Skip to content

Commit

Permalink
Resolve inherited methods
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock committed May 9, 2024
1 parent cac4824 commit 367a84d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/ruby_indexer/lib/ruby_indexer/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,14 @@ def follow_aliased_namespace(name)
sig { params(method_name: String, receiver_name: String).returns(T.nilable(T::Array[Entry::Member])) }
def resolve_method(method_name, receiver_name)
method_entries = self[method_name]
owner_entries = self[receiver_name]
return unless owner_entries && method_entries
ancestors = linearized_ancestors_of(receiver_name.delete_prefix("::"))
return unless ancestors && method_entries

owner_name = T.must(owner_entries.first).name
T.cast(
method_entries.grep(Entry::Member).select do |entry|
T.cast(entry, Entry::Member).owner&.name == owner_name
method_entries.select do |entry|
next unless entry.is_a?(Entry::Member)

ancestors.any?(entry.owner&.name)
end,
T::Array[Entry::Member],
)
Expand Down
24 changes: 24 additions & 0 deletions lib/ruby_indexer/test/index_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -611,5 +611,29 @@ class Bar < Something
@index.linearized_ancestors_of("Foo::Bar"),
)
end

def test_resolving_an_inherited_method
index(<<~RUBY)
module Foo
def baz; end
end
class Bar
def qux; end
end
class Wow < Bar
include Foo
end
RUBY

entry = T.must(@index.resolve_method("baz", "Wow")&.first)
assert_equal("baz", entry.name)
assert_equal("Foo", T.must(entry.owner).name)

entry = T.must(@index.resolve_method("qux", "Wow")&.first)
assert_equal("qux", entry.name)
assert_equal("Bar", T.must(entry.owner).name)
end
end
end

0 comments on commit 367a84d

Please sign in to comment.