Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix singleton indexing under compact namespaces #3062

Merged
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
14 changes: 11 additions & 3 deletions lib/ruby_indexer/lib/ruby_indexer/declaration_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def on_singleton_class_node_enter(node)

if current_owner
expression = node.expression
name = (expression.is_a?(Prism::SelfNode) ? "<Class:#{@stack.last}>" : "<Class:#{expression.slice}>")
name = (expression.is_a?(Prism::SelfNode) ? "<Class:#{last_name_in_stack}>" : "<Class:#{expression.slice}>")
real_nesting = actual_nesting(name)

existing_entries = T.cast(@index[real_nesting.join("::")], T.nilable(T::Array[Entry::SingletonClass]))
Expand Down Expand Up @@ -376,7 +376,6 @@ def on_def_node_enter(node)
))

@owner_stack << singleton
@stack << "<Class:#{@stack.last}>"
end
end

Expand All @@ -386,7 +385,6 @@ def on_def_node_leave(node)

if node.receiver.is_a?(Prism::SelfNode)
@owner_stack.pop
@stack.pop
end
end

Expand Down Expand Up @@ -1127,5 +1125,15 @@ def advance_namespace_stack(short_name, entry)
@index.add(entry)
@stack << short_name
end

# Returns the last name in the stack not as we found it, but in terms of declared constants. For example, if the
# last entry in the stack is a compact namespace like `Foo::Bar`, then the last name is `Bar`
sig { returns(T.nilable(String)) }
def last_name_in_stack
name = @stack.last
return unless name

name.split("::").last
end
end
end
19 changes: 19 additions & 0 deletions lib/ruby_indexer/test/classes_and_modules_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -647,5 +647,24 @@ class Foo
entry = @index["Foo"].first
assert_empty(entry.comments)
end

def test_singleton_inside_compact_namespace
index(<<~RUBY)
module Foo::Bar
class << self
def baz; end
end
end
RUBY

# Verify we didn't index the incorrect name
assert_nil(@index["Foo::Bar::<Class:Foo::Bar>"])

# Verify we indexed the correct name
assert_entry("Foo::Bar::<Class:Bar>", Entry::SingletonClass, "/fake/path/foo.rb:1-2:3-5")

method = @index["baz"]&.first
assert_equal("Foo::Bar::<Class:Bar>", method.owner.name)
end
end
end
Loading