Skip to content

Commit

Permalink
Only pop namespace stack when the class or module is pushed onto it
Browse files Browse the repository at this point in the history
Currently, the indexer doesn't index classes and modules that have dynamic
namespace. Similarly, those classes and modules' names are not pushed onto the
namespace stack. However, the stack is currently popped on every class or module
exit, which could lead to an incorrect stack popping.

This commit fixes the issue by only popping the stack when the class or module
name is considered indexable.
  • Loading branch information
st0012 committed May 7, 2024
1 parent 1f2c028 commit 961ead4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/ruby_indexer/lib/ruby_indexer/declaration_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ def on_class_node_enter(node)

sig { params(node: Prism::ClassNode).void }
def on_class_node_leave(node)
@stack.pop
name = node.constant_path.location.slice
@stack.pop if /^[A-Z:]/.match?(name)
end

sig { params(node: Prism::ModuleNode).void }
Expand All @@ -86,7 +87,8 @@ def on_module_node_enter(node)

sig { params(node: Prism::ModuleNode).void }
def on_module_node_leave(node)
@stack.pop
name = node.constant_path.location.slice
@stack.pop if /^[A-Z:]/.match?(name)
end

sig { params(node: Prism::MultiWriteNode).void }
Expand Down
32 changes: 32 additions & 0 deletions lib/ruby_indexer/test/classes_and_modules_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ class self::Bar
refute_entry("self::Bar")
end

def test_dynamically_namespaced_class_doesnt_affect_other_classes
index(<<~RUBY)
class Foo
class self::Bar
end
class Bar
end
end
RUBY

refute_entry("self::Bar")
assert_entry("Foo", Entry::Class, "/fake/path/foo.rb:0-0:6-3")
assert_entry("Foo::Bar", Entry::Class, "/fake/path/foo.rb:4-2:5-5")
end

def test_empty_statements_module
index(<<~RUBY)
module Foo
Expand Down Expand Up @@ -127,6 +143,22 @@ module self::Bar
refute_entry("self::Bar")
end

def test_dynamically_namespaced_module_doesnt_affect_other_modules
index(<<~RUBY)
module Foo
class self::Bar
end
module Bar
end
end
RUBY

refute_entry("self::Bar")
assert_entry("Foo", Entry::Module, "/fake/path/foo.rb:0-0:6-3")
assert_entry("Foo::Bar", Entry::Module, "/fake/path/foo.rb:4-2:5-5")
end

def test_nested_modules_and_classes
index(<<~RUBY)
module Foo
Expand Down

0 comments on commit 961ead4

Please sign in to comment.