Skip to content

Commit

Permalink
handle setting multiple methods and support StringNode
Browse files Browse the repository at this point in the history
  • Loading branch information
IgnacioFan committed Oct 23, 2024
1 parent 16d87f1 commit 31b8c90
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 43 deletions.
48 changes: 27 additions & 21 deletions lib/ruby_indexer/lib/ruby_indexer/declaration_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -758,33 +758,39 @@ 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
arguments_node.arguments.each do |argument|
method_name = case argument
when Prism::StringNode
argument.content
when Prism::SymbolNode
argument.value
end
next unless method_name

entries.each do |entry|
entry_owner_name = entry.owner&.name
next unless entry_owner_name
entries = @index.resolve_method(method_name, owner_name)
next unless entries

entry.visibility = Entry::Visibility::PRIVATE
entries.each do |entry|
entry_owner_name = entry.owner&.name
next unless entry_owner_name

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,
))
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.signatures,
Entry::Visibility::PUBLIC,
singleton,
))
end
end
end

Expand Down
43 changes: 21 additions & 22 deletions lib/ruby_indexer/test/method_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,29 +125,28 @@ def baz; end

def test_visibility_tracking_with_module_function
index(<<~RUBY)
module Foo
module Test
def foo; end
module_function :foo
end
class Bar
include Foo
end
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)
def bar; end
module_function :foo, "bar"
end
RUBY

["foo", "bar"].each do |keyword|
entries = T.must(@index[keyword])
# should receive two entries because module_function creates a singleton method
# for the Test module and a private method for classes include the Test 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("Test", 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("Test::<Class:Test>", second_entry.owner.name)
assert_instance_of(Entry::SingletonClass, second_entry.owner)
assert_equal(Entry::Visibility::PUBLIC, second_entry.visibility)
end
end

def test_method_with_parameters
Expand Down

0 comments on commit 31b8c90

Please sign in to comment.