Skip to content

Commit

Permalink
Support private_class_method in the indexer
Browse files Browse the repository at this point in the history
  • Loading branch information
kcdragon committed Nov 14, 2024
1 parent e6a3703 commit fac03eb
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
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)
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)
end
end

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

0 comments on commit fac03eb

Please sign in to comment.