Skip to content

Commit

Permalink
Search aliased namespaces in the top level too (#2208)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock authored Jun 19, 2024
1 parent b3c3668 commit 54b7e4a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
7 changes: 1 addition & 6 deletions lib/ruby_indexer/lib/ruby_indexer/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def resolve(name, nesting, seen_names = [])
return entries if entries

# Finally, as a fallback, Ruby will search for the constant in the top level namespace
search_top_level(name, seen_names)
direct_or_aliased_constant(name, seen_names)
rescue UnresolvableAliasError
nil
end
Expand Down Expand Up @@ -604,11 +604,6 @@ def direct_or_aliased_constant(full_name, seen_names)
entries&.map { |e| e.is_a?(Entry::UnresolvedAlias) ? resolve_alias(e, seen_names) : e }
end

sig { params(name: String, seen_names: T::Array[String]).returns(T.nilable(T::Array[Entry])) }
def search_top_level(name, seen_names)
@entries[name]&.map { |e| e.is_a?(Entry::UnresolvedAlias) ? resolve_alias(e, seen_names) : e }
end

# Attempt to resolve a given unresolved method alias. This method returns the resolved alias if we managed to
# identify the target or the same unresolved alias entry if we couldn't
sig do
Expand Down
37 changes: 37 additions & 0 deletions lib/ruby_indexer/test/index_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,43 @@ module Namespace
assert_instance_of(Entry::Alias, baz_entry)
end

def test_resolving_constants_in_aliased_namespace
index(<<~RUBY)
module Original
module Something
CONST = 123
end
end
module Other
ALIAS = Original::Something
end
module Third
Other::ALIAS::CONST
end
RUBY

entry = T.must(@index.resolve("Other::ALIAS::CONST", ["Third"])&.first)
assert_kind_of(Entry::Constant, entry)
assert_equal("Original::Something::CONST", entry.name)
end

def test_resolving_top_level_aliases
index(<<~RUBY)
class Foo
CONST = 123
end
FOO = Foo
FOO::CONST
RUBY

entry = T.must(@index.resolve("FOO::CONST", [])&.first)
assert_kind_of(Entry::Constant, entry)
assert_equal("Foo::CONST", entry.name)
end

def test_resolving_top_level_compact_reference
index(<<~RUBY)
class Foo::Bar
Expand Down

0 comments on commit 54b7e4a

Please sign in to comment.