From 367a84d7d5a721eec149091ca6cab43bf114dbc2 Mon Sep 17 00:00:00 2001 From: Vinicius Stock Date: Thu, 9 May 2024 14:35:24 -0400 Subject: [PATCH] Resolve inherited methods --- lib/ruby_indexer/lib/ruby_indexer/index.rb | 11 +++++----- lib/ruby_indexer/test/index_test.rb | 24 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/lib/ruby_indexer/lib/ruby_indexer/index.rb b/lib/ruby_indexer/lib/ruby_indexer/index.rb index b9f2f96627..9703661432 100644 --- a/lib/ruby_indexer/lib/ruby_indexer/index.rb +++ b/lib/ruby_indexer/lib/ruby_indexer/index.rb @@ -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], ) diff --git a/lib/ruby_indexer/test/index_test.rb b/lib/ruby_indexer/test/index_test.rb index ca79bd33b7..d2fe916727 100644 --- a/lib/ruby_indexer/test/index_test.rb +++ b/lib/ruby_indexer/test/index_test.rb @@ -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