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 2 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
37 changes: 37 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,8 @@ def on_call_node_enter(node)
@visibility_stack.push(Entry::Visibility::PROTECTED)
when :private
@visibility_stack.push(Entry::Visibility::PRIVATE)
when :module_function
handle_module_function(node)
end

@enhancements.each do |enhancement|
Expand Down Expand Up @@ -751,6 +753,41 @@ def handle_module_operation(node, operation)
end
end

sig { params(node: Prism::CallNode).void }
def handle_module_function(node)
arguments_node = node.arguments
return unless arguments_node

name_argument = arguments_node.arguments.first
return unless name_argument.is_a?(Prism::SymbolNode)
IgnacioFan marked this conversation as resolved.
Show resolved Hide resolved

method_name = name_argument.value.to_s
owner_name = @owner_stack.last&.name
return unless owner_name

entries = @index.resolve_method(method_name, owner_name)
return unless entries

entries.each do |entry|
entry_owner_name = entry.owner&.name
next unless entry_owner_name

entry.visibility = Entry::Visibility::PRIVATE

singleton = @index.existing_or_new_singleton_class(entry_owner_name)
@index.add(Entry::Method.new(
vinistock marked this conversation as resolved.
Show resolved Hide resolved
method_name,
@file_path,
Location.from_prism_location(node.location, @code_units_cache),
Location.from_prism_location(T.must(node.message_loc), @code_units_cache),
collect_comments(node),
[],
vinistock marked this conversation as resolved.
Show resolved Hide resolved
Entry::Visibility::PUBLIC,
singleton,
))
end
end

sig { returns(Entry::Visibility) }
def current_visibility
T.must(@visibility_stack.last)
Expand Down
27 changes: 27 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,33 @@ 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)
first_entry, second_entry = *entries
# The first entry points to the location of the module_function call
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
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