Skip to content

Commit

Permalink
add handle_module_function and fix typecheck failures
Browse files Browse the repository at this point in the history
  • Loading branch information
IgnacioFan committed Oct 18, 2024
1 parent 045ec92 commit 16d87f1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
58 changes: 36 additions & 22 deletions lib/ruby_indexer/lib/ruby_indexer/declaration_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -313,28 +313,7 @@ def on_call_node_enter(node)
when :private
@visibility_stack.push(Entry::Visibility::PRIVATE)
when :module_function
name_argument = node.arguments.arguments.first
return unless name_argument

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

entries.each do |e|
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
handle_module_function(node)
end

@enhancements.each do |enhancement|
Expand Down Expand Up @@ -774,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)

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(
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),
[],
Entry::Visibility::PUBLIC,
singleton,
))
end
end

sig { returns(Entry::Visibility) }
def current_visibility
T.must(@visibility_stack.last)
Expand Down
3 changes: 1 addition & 2 deletions lib/ruby_indexer/test/method_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,12 @@ class Bar
# 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
first_entry = entries.first
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)
Expand Down

0 comments on commit 16d87f1

Please sign in to comment.