Fix top level namespace declaration indexing #2297
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
Closes #2287
The problem surfaced in the issue is actually unrelated to linearizing ancestors. We were incorrectly handling top level namespace declarations and accidentally inserting entries in the index that included a leading
::
.To match Ruby's behaviour, we need to account that a top level reference may be found at any level of nesting and it fully removes all of the preceding namespaces.
Implementation
Created a method to compute the correct nesting. The method searches for the first top level reference in reverse. If a constant with the
::
prefix is found, we break out of the loop and return the nesting only up to that point.Otherwise, we just keep prepending the names into the array and it just results in the same nesting.
Automated Tests
Added two examples that demonstrate the scenarios we did not support.