From 22a9dfa8c9b60c874d4a42501a90a1eafe9c4d23 Mon Sep 17 00:00:00 2001 From: Vinicius Stock Date: Wed, 11 Sep 2024 14:26:43 -0400 Subject: [PATCH] Index forwarded parameters Co-authored-by: Andy Waite --- .../lib/ruby_indexer/declaration_listener.rb | 5 +++- lib/ruby_indexer/lib/ruby_indexer/entry.rb | 11 ++++++++ lib/ruby_indexer/test/method_test.rb | 27 +++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/lib/ruby_indexer/lib/ruby_indexer/declaration_listener.rb b/lib/ruby_indexer/lib/ruby_indexer/declaration_listener.rb index c9a0f309d..1fc625568 100644 --- a/lib/ruby_indexer/lib/ruby_indexer/declaration_listener.rb +++ b/lib/ruby_indexer/lib/ruby_indexer/declaration_listener.rb @@ -685,9 +685,12 @@ def list_params(parameters_node) keyword_rest = parameters_node.keyword_rest - if keyword_rest.is_a?(Prism::KeywordRestParameterNode) + case keyword_rest + when Prism::KeywordRestParameterNode keyword_rest_name = parameter_name(keyword_rest) || Entry::KeywordRestParameter::DEFAULT_NAME parameters << Entry::KeywordRestParameter.new(name: keyword_rest_name) + when Prism::ForwardingParameterNode + parameters << Entry::ForwardingParameter.new end parameters_node.posts.each do |post| diff --git a/lib/ruby_indexer/lib/ruby_indexer/entry.rb b/lib/ruby_indexer/lib/ruby_indexer/entry.rb index 5bbf9e2b0..4841a9f62 100644 --- a/lib/ruby_indexer/lib/ruby_indexer/entry.rb +++ b/lib/ruby_indexer/lib/ruby_indexer/entry.rb @@ -302,6 +302,17 @@ def decorated_name end end + # A forwarding method parameter, e.g. `def foo(...)` + class ForwardingParameter < Parameter + extend T::Sig + + sig { void } + def initialize + # You can't name a forwarding parameter, it's always called `...` + super(name: :"...") + end + end + class Member < Entry extend T::Sig extend T::Helpers diff --git a/lib/ruby_indexer/test/method_test.rb b/lib/ruby_indexer/test/method_test.rb index 82ef2c5ca..266ceb35e 100644 --- a/lib/ruby_indexer/test/method_test.rb +++ b/lib/ruby_indexer/test/method_test.rb @@ -330,6 +330,33 @@ def bar(**nil) assert_empty(parameters) end + def test_methods_with_argument_forwarding + index(<<~RUBY) + class Foo + def bar(...) + end + + def baz(a, ...) + end + end + RUBY + + entry = T.must(@index["bar"].first) + assert_instance_of(Entry::Method, entry, "Expected `bar` to be indexed") + + parameters = entry.signatures.first.parameters + assert_equal(1, parameters.length) + assert_instance_of(Entry::ForwardingParameter, parameters.first) + + entry = T.must(@index["baz"].first) + assert_instance_of(Entry::Method, entry, "Expected `baz` to be indexed") + + parameters = entry.signatures.first.parameters + assert_equal(2, parameters.length) + assert_instance_of(Entry::RequiredParameter, parameters[0]) + assert_instance_of(Entry::ForwardingParameter, parameters[1]) + end + def test_keeps_track_of_method_owner index(<<~RUBY) class Foo