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

Add signature matching for positional parameters #2541

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
30 changes: 30 additions & 0 deletions lib/ruby_indexer/lib/ruby_indexer/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,36 @@ def initialize(parameters)
def format
@parameters.map(&:decorated_name).join(", ")
end

# Returns `true` if the given call node arguments array matches this method signature. This method will prefer
# returning `true` for situations that cannot be analyzed statically, like the presence of splats, keyword splats
# or forwarding arguments
sig { params(arguments: T::Array[Prism::Node]).returns(T::Boolean) }
def matches?(arguments)
min_pos = 0
max_pos = T.let(0, Numeric)

@parameters.each do |param|
case param
when RequiredParameter
min_pos += 1
max_pos += 1
when OptionalParameter
max_pos += 1
when RestParameter
max_pos = Float::INFINITY
when ForwardingParameter
max_pos = Float::INFINITY
end
end

_keyword_hash_node, positional_args = arguments.partition { |arg| arg.is_a?(Prism::KeywordHashNode) }
argument_length_is_unknown = positional_args.any? do |arg|
arg.is_a?(Prism::SplatNode) || arg.is_a?(Prism::ForwardingArgumentsNode)
end

argument_length_is_unknown || (min_pos..max_pos).cover?(positional_args.length)
end
end
end
end
164 changes: 164 additions & 0 deletions lib/ruby_indexer/test/method_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -488,5 +488,169 @@ def bar
assert_equal(6, name_location.start_column)
assert_equal(9, name_location.end_column)
end

def test_signature_matches_for_a_method_with_positional_params
index(<<~RUBY)
class Foo
def bar(a, b = 123)
end
end
RUBY

entry = T.must(@index["bar"].first)

# Matching calls
assert_signature_matches(entry, "bar(1)")
assert_signature_matches(entry, "bar(1, 2)")
assert_signature_matches(entry, "bar(...)")
assert_signature_matches(entry, "bar(1, ...)")
assert_signature_matches(entry, "bar(*a)")
assert_signature_matches(entry, "bar(1, *a)")
assert_signature_matches(entry, "bar(1, *a, 2)")
assert_signature_matches(entry, "bar(*a, 2)")
assert_signature_matches(entry, "bar(1, **a)")
assert_signature_matches(entry, "bar(1) {}")

# Non matching calls

vinistock marked this conversation as resolved.
Show resolved Hide resolved
refute_signature_matches(entry, "bar()")
refute_signature_matches(entry, "bar(1, 2, 3)")

# TODO: uncomment after keyword support
# refute_signature_matches(entry, "bar(1, b: 2)")
# refute_signature_matches(entry, "bar(1, 2, c: 3)")
end

def test_signature_matches_for_a_method_with_argument_forwarding
index(<<~RUBY)
class Foo
def bar(...)
end
end
RUBY

entry = T.must(@index["bar"].first)

# All calls match a forwarding parameter
assert_signature_matches(entry, "bar(1)")
assert_signature_matches(entry, "bar(1, 2)")
assert_signature_matches(entry, "bar(...)")
assert_signature_matches(entry, "bar(1, ...)")
assert_signature_matches(entry, "bar(*a)")
assert_signature_matches(entry, "bar(1, *a)")
assert_signature_matches(entry, "bar(1, *a, 2)")
assert_signature_matches(entry, "bar(*a, 2)")
assert_signature_matches(entry, "bar(1, **a)")
assert_signature_matches(entry, "bar(1) {}")
assert_signature_matches(entry, "bar()")
assert_signature_matches(entry, "bar(1, 2, 3)")
assert_signature_matches(entry, "bar(1, 2, a: 1, b: 5) {}")
end

def test_signature_matches_for_post_forwarding_parameter
index(<<~RUBY)
class Foo
def bar(a, ...)
end
end
RUBY

entry = T.must(@index["bar"].first)

# All calls with at least one positional argument match
assert_signature_matches(entry, "bar(1)")
assert_signature_matches(entry, "bar(1, 2)")
assert_signature_matches(entry, "bar(...)")
assert_signature_matches(entry, "bar(1, ...)")
assert_signature_matches(entry, "bar(*a)")
assert_signature_matches(entry, "bar(1, *a)")
assert_signature_matches(entry, "bar(1, *a, 2)")
assert_signature_matches(entry, "bar(*a, 2)")
assert_signature_matches(entry, "bar(1, **a)")
assert_signature_matches(entry, "bar(1) {}")
assert_signature_matches(entry, "bar(1, 2, 3)")
assert_signature_matches(entry, "bar(1, 2, a: 1, b: 5) {}")

refute_signature_matches(entry, "bar()")
end

def test_signature_matches_for_destructured_parameters
index(<<~RUBY)
class Foo
def bar(a, (b, c))
end
end
RUBY

entry = T.must(@index["bar"].first)

# All calls with at least one positional argument match
assert_signature_matches(entry, "bar(1, 2)")
assert_signature_matches(entry, "bar(...)")
assert_signature_matches(entry, "bar(1, ...)")
assert_signature_matches(entry, "bar(*a)")
assert_signature_matches(entry, "bar(1, *a)")
assert_signature_matches(entry, "bar(*a, 2)")
# This matches because `bar(1, *[], 2)` would result in `bar(1, 2)`, which is a valid call
assert_signature_matches(entry, "bar(1, *a, 2)")

refute_signature_matches(entry, "bar()")
refute_signature_matches(entry, "bar(1)")
refute_signature_matches(entry, "bar(1, **a)")
refute_signature_matches(entry, "bar(1, 2, 3)")
refute_signature_matches(entry, "bar(1) {}")

# TODO: uncomment after keyword support
# refute_signature_matches(entry, "bar(1, 2, a: 1, b: 5) {}")
end

def test_signature_matches_for_post_parameters
index(<<~RUBY)
class Foo
def bar(*splat, a)
end
end
RUBY

entry = T.must(@index["bar"].first)

# All calls with at least one positional argument match
assert_signature_matches(entry, "bar(1)")
assert_signature_matches(entry, "bar(1, 2)")
assert_signature_matches(entry, "bar(...)")
assert_signature_matches(entry, "bar(1, ...)")
assert_signature_matches(entry, "bar(*a)")
assert_signature_matches(entry, "bar(1, *a)")
assert_signature_matches(entry, "bar(*a, 2)")
assert_signature_matches(entry, "bar(1, *a, 2)")
assert_signature_matches(entry, "bar(1, **a)")
assert_signature_matches(entry, "bar(1, 2, 3)")
assert_signature_matches(entry, "bar(1) {}")

refute_signature_matches(entry, "bar()")

# TODO: uncomment after keyword support
# refute_signature_matches(entry, "bar(1, 2, a: 1, b: 5) {}")
end

private

sig { params(entry: Entry::Method, call_string: String).void }
def assert_signature_matches(entry, call_string)
sig = T.must(entry.signatures.first)
arguments = parse_prism_args(call_string)
assert(sig.matches?(arguments), "Expected #{call_string} to match #{entry.name}#{entry.decorated_parameters}")
end

sig { params(entry: Entry::Method, call_string: String).void }
def refute_signature_matches(entry, call_string)
sig = T.must(entry.signatures.first)
arguments = parse_prism_args(call_string)
refute(sig.matches?(arguments), "Expected #{call_string} to not match #{entry.name}#{entry.decorated_parameters}")
end

def parse_prism_args(s)
Array(Prism.parse(s).value.statements.body.first.arguments&.arguments)
end
end
end
Loading