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

Support module_function in the indexer #2733

Merged
merged 5 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 23 additions & 0 deletions lib/ruby_indexer/lib/ruby_indexer/declaration_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,29 @@ def on_call_node_enter(node)
@visibility_stack.push(Entry::Visibility::PROTECTED)
when :private
@visibility_stack.push(Entry::Visibility::PRIVATE)
when :module_function
name_argument = node.arguments.arguments.first
IgnacioFan marked this conversation as resolved.
Show resolved Hide resolved
return unless name_argument

entries = @index.resolve_method(name_argument.value, @owner_stack.last.name)
return unless entries

entries.each do |e|
IgnacioFan marked this conversation as resolved.
Show resolved Hide resolved
e.visibility = Entry::Visibility::PRIVATE

singleton = @index.existing_or_new_singleton_class(e.owner.name)

@index.add(Entry::Method.new(
name_argument.value,
@file_path,
Location.from_prism_location(node.location, @code_units_cache),
Location.from_prism_location(node.message_loc, @code_units_cache),
collect_comments(node),
[],
Entry::Visibility::PUBLIC,
singleton,
))
end
end

@enhancements.each do |enhancement|
Expand Down
28 changes: 28 additions & 0 deletions lib/ruby_indexer/test/method_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,34 @@ def baz; end
assert_entry("baz", Entry::Method, "/fake/path/foo.rb:9-2:9-14", visibility: Entry::Visibility::PRIVATE)
end

def test_visibility_tracking_with_module_function
index(<<~RUBY)
module Foo
def foo; end
module_function :foo
end

class Bar
include Foo
end
IgnacioFan marked this conversation as resolved.
Show resolved Hide resolved
RUBY

entries = T.must(@index["foo"])
# receive two entries because module_function creates a singleton method
# for the Foo module and a private method for classes include Foo module
assert_equal(entries.size, 2)
# The first entry points to the location of the module_function call
first_entry = entries.first
IgnacioFan marked this conversation as resolved.
Show resolved Hide resolved
assert_equal("Foo", first_entry.owner.name)
assert_instance_of(Entry::Module, first_entry.owner)
assert_equal(Entry::Visibility::PRIVATE, first_entry.visibility)
# The second entry points to the public singleton method
second_entry = entries.last
assert_equal("Foo::<Class:Foo>", second_entry.owner.name)
assert_instance_of(Entry::SingletonClass, second_entry.owner)
assert_equal(Entry::Visibility::PUBLIC, second_entry.visibility)
end

def test_method_with_parameters
index(<<~RUBY)
class Foo
Expand Down
Loading