Skip to content

Commit

Permalink
Index forwarded parameters
Browse files Browse the repository at this point in the history
Co-authored-by: Andy Waite <[email protected]>
  • Loading branch information
vinistock and andyw8 committed Sep 11, 2024
1 parent 44f85d0 commit 22a9dfa
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
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

0 comments on commit 22a9dfa

Please sign in to comment.