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

Index forwarded parameters #2540

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion lib/ruby_indexer/lib/ruby_indexer/declaration_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
11 changes: 11 additions & 0 deletions lib/ruby_indexer/lib/ruby_indexer/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions lib/ruby_indexer/test/method_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading