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 private_class_method in the indexer #2858

Merged
merged 6 commits into from
Nov 21, 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
39 changes: 39 additions & 0 deletions lib/ruby_indexer/lib/ruby_indexer/declaration_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ def on_call_node_enter(node)
@visibility_stack.push(Entry::Visibility::PRIVATE)
when :module_function
handle_module_function(node)
when :private_class_method
handle_private_class_method(node)
end

@enhancements.each do |enhancement|
Expand Down Expand Up @@ -805,6 +807,43 @@ def handle_module_function(node)
end
end

sig { params(node: Prism::CallNode).void }
def handle_private_class_method(node)
node.arguments&.arguments&.each do |argument|
string_or_symbol_nodes = case argument
when Prism::StringNode, Prism::SymbolNode
[argument]
when Prism::ArrayNode
argument.elements
else
[]
end

string_or_symbol_nodes.each do |string_or_symbol_node|
method_name = case string_or_symbol_node
when Prism::StringNode
string_or_symbol_node.content
when Prism::SymbolNode
string_or_symbol_node.value
end
next unless method_name

owner_name = @owner_stack.last&.name
next unless owner_name

entries = @index.resolve_method(method_name, @index.existing_or_new_singleton_class(owner_name).name)
next unless entries

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

entry.visibility = Entry::Visibility::PRIVATE
end
end
end
end

sig { returns(Entry::Visibility) }
def current_visibility
T.must(@visibility_stack.last)
Expand Down
42 changes: 42 additions & 0 deletions lib/ruby_indexer/test/method_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,48 @@ def bar; end
end
end

def test_private_class_method_visibility_tracking_string_symbol_arguments
index(<<~RUBY)
class Test
def self.foo
end

def self.bar
end

private_class_method("foo", :bar)
end
RUBY

["foo", "bar"].each do |keyword|
entries = T.must(@index[keyword])
assert_equal(1, entries.size)
entry = entries.first
assert_equal(Entry::Visibility::PRIVATE, entry.visibility)
kcdragon marked this conversation as resolved.
Show resolved Hide resolved
end
end

def test_private_class_method_visibility_tracking_array_argument
index(<<~RUBY)
class Test
def self.foo
end

def self.bar
end

private_class_method(["foo", :bar])
end
RUBY

["foo", "bar"].each do |keyword|
entries = T.must(@index[keyword])
assert_equal(1, entries.size)
entry = entries.first
assert_equal(Entry::Visibility::PRIVATE, entry.visibility)
kcdragon marked this conversation as resolved.
Show resolved Hide resolved
end
end

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