Skip to content

Commit 81b8818

Browse files
committed
Fix stack StackLevelError during self referential constant resolution
1 parent 5802a0b commit 81b8818

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/ruby_indexer/lib/ruby_indexer/index.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,10 +903,12 @@ def resolve_alias(entry, seen_names)
903903
return entry if seen_names.include?(alias_name)
904904

905905
seen_names << alias_name
906-
907906
target = resolve(entry.target, entry.nesting, seen_names)
908907
return entry unless target
909908

909+
# Self referential alias can be unresolved we should bail out from resolving
910+
return entry if target.first == entry
911+
910912
target_name = target.first #: as !nil
911913
.name
912914
resolved_alias = Entry::ConstantAlias.new(target_name, entry)

lib/ruby_indexer/test/constant_test.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,28 @@ module A
318318
assert_instance_of(Entry::Constant, constant)
319319
end
320320

321+
def test_self_referential_constant_alias
322+
index(<<~RUBY)
323+
module RealClass
324+
CONSTANT = {}
325+
end
326+
327+
module Foo
328+
SomeClass = ::SomeClass
329+
RealClass = ::RealClass
330+
331+
UNRESOLVED = SomeClass::CONSTANT
332+
CONSTANT = RealClass::CONSTANT
333+
end
334+
RUBY
335+
336+
assert_entry("RealClass", Entry::Module, "/fake/path/foo.rb:0-0:2-3")
337+
assert_no_entry("SomeClass")
338+
assert_entry("Foo", Entry::Module, "/fake/path/foo.rb:4-0:10-3")
339+
assert_entry("RealClass::CONSTANT", Entry::Constant, "/fake/path/foo.rb:1-2:1-15")
340+
assert_entry("Foo::UNRESOLVED", Entry::UnresolvedConstantAlias, "/fake/path/foo.rb:8-2:8-34")
341+
end
342+
321343
def test_indexing_or_and_operator_nodes
322344
index(<<~RUBY)
323345
A ||= 1

0 commit comments

Comments
 (0)