Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions lib/ruby_indexer/lib/ruby_indexer/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,10 @@ def follow_aliased_namespace(name, seen_names = [])
(parts.length - 1).downto(0) do |i|
current_name = parts[0..i] #: as !nil
.join("::")
entry = @entries[current_name]&.first

entry = unless seen_names.include?(current_name)
@entries[current_name]&.first
end

case entry
when Entry::ConstantAlias
Expand Down Expand Up @@ -1023,11 +1026,19 @@ def build_non_redundant_full_name(name, nesting)
name_parts.join("::")
end

# Tries to return direct entry from index then non seen canonicalized alias or nil
#: (String full_name, Array[String] seen_names) -> Array[Entry::Constant | Entry::ConstantAlias | Entry::Namespace | Entry::UnresolvedConstantAlias]?
def direct_or_aliased_constant(full_name, seen_names)
entries = @entries[full_name] || @entries[follow_aliased_namespace(full_name)]
if (entries = @entries[full_name])
return entries.map do |e|
e.is_a?(Entry::UnresolvedConstantAlias) ? resolve_alias(e, seen_names) : e
end #: as Array[Entry::Constant | Entry::ConstantAlias | Entry::Namespace | Entry::UnresolvedConstantAlias])?
end

aliased = follow_aliased_namespace(full_name, seen_names)
return if full_name == aliased || seen_names.include?(aliased)

entries&.map do |e|
@entries[aliased]&.map do |e|
e.is_a?(Entry::UnresolvedConstantAlias) ? resolve_alias(e, seen_names) : e
end #: as Array[Entry::Constant | Entry::ConstantAlias | Entry::Namespace | Entry::UnresolvedConstantAlias])?
end
Expand Down
23 changes: 23 additions & 0 deletions lib/ruby_indexer/test/index_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,29 @@ class Foo
assert_equal(2, foo_entry.location.start_line)
end

def test_resolving_self_referential_constant_alias
index(<<~RUBY)
module A
module B
class C
end
end
end

module A
module D
B = B::C
end
end
RUBY

entry = @index.resolve("A::D::B", [])&.first #: as Entry::ConstantAlias

assert_kind_of(RubyIndexer::Entry::ConstantAlias, entry)
assert_equal(10, entry.location.start_line)
assert_equal("A::B::C", entry.target)
end

def test_resolving_qualified_references
index(<<~RUBY)
module Namespace
Expand Down