Issue 19220 function parameter arity (#24031)

* Added reference test case and diagnostics message

* Adjusted arity checks to account for non-contiguous overloads

* Code cleanup, baseline not yet commited

* Accepted test baselines and minor implementation changes

* Cleaned up baseline tracking the now renamed arity check test

* Add range response when range contains only 2 values

* Added recent baseline

* Refined arity error messages when available overloads can be grouped

* Rolled back code formatting

* WIP cleanup needed in a few edge cases

* Finished adding new more descriptive error messages

* Code cleanup

* Added simplified version of bugfix for #19220

* Rebased onto master

* Removed whitespace after type assertion

* Code review simplifications

* Use correct diagnostic name

* Code review changes and simplification of diagnostic message

* Revert formatting changes
This commit is contained in:
rflorian
2018-07-12 02:19:56 +02:00
committed by Nathan Shively-Sanders
parent 5f4a03c408
commit 66e9aaac18
7 changed files with 249 additions and 10 deletions

View File

@@ -19052,24 +19052,40 @@ namespace ts {
else if (args) {
let min = Number.POSITIVE_INFINITY;
let max = Number.NEGATIVE_INFINITY;
let belowArgCount = Number.NEGATIVE_INFINITY;
let aboveArgCount = Number.POSITIVE_INFINITY;
let argCount = args.length;
for (const sig of signatures) {
min = Math.min(min, getMinArgumentCount(sig));
max = Math.max(max, getParameterCount(sig));
const minCount = getMinArgumentCount(sig);
const maxCount = getParameterCount(sig);
if (minCount < argCount && minCount > belowArgCount) belowArgCount = minCount;
if (argCount < maxCount && maxCount < aboveArgCount) aboveArgCount = maxCount;
min = Math.min(min, minCount);
max = Math.max(max, maxCount);
}
const hasRestParameter = some(signatures, hasEffectiveRestParameter);
const hasSpreadArgument = getSpreadArgumentIndex(args) > -1;
const paramCount = hasRestParameter ? min :
const paramRange = hasRestParameter ? min :
min < max ? min + "-" + max :
min;
let argCount = args.length;
const hasSpreadArgument = getSpreadArgumentIndex(args) > -1;
if (argCount <= max && hasSpreadArgument) {
argCount--;
}
const error = hasRestParameter && hasSpreadArgument ? Diagnostics.Expected_at_least_0_arguments_but_got_1_or_more :
hasRestParameter ? Diagnostics.Expected_at_least_0_arguments_but_got_1 :
hasSpreadArgument ? Diagnostics.Expected_0_arguments_but_got_1_or_more :
Diagnostics.Expected_0_arguments_but_got_1;
diagnostics.add(createDiagnosticForNode(node, error, paramCount, argCount));
if (hasRestParameter || hasSpreadArgument) {
const error = hasRestParameter && hasSpreadArgument ? Diagnostics.Expected_at_least_0_arguments_but_got_1_or_more :
hasRestParameter ? Diagnostics.Expected_at_least_0_arguments_but_got_1 :
Diagnostics.Expected_0_arguments_but_got_1_or_more;
diagnostics.add(createDiagnosticForNode(node, error, paramRange, argCount));
}
else if (min < argCount && argCount < max) {
diagnostics.add(createDiagnosticForNode(node, Diagnostics.No_overload_expects_0_arguments_but_overloads_do_exist_that_expect_either_1_or_2_arguments, argCount, belowArgCount, aboveArgCount));
}
else {
diagnostics.add(createDiagnosticForNode(node, Diagnostics.Expected_0_arguments_but_got_1, paramRange, argCount));
}
}
else if (fallbackError) {
diagnostics.add(createDiagnosticForNode(node, fallbackError));

View File

@@ -2056,6 +2056,10 @@
"category": "Error",
"code": 2574
},
"No overload expects {0} arguments, but overloads do exist that expect either {1} or {2} arguments.": {
"category": "Error",
"code": 2575
},
"JSX element attributes type '{0}' may not be a union type.": {
"category": "Error",
"code": 2600