Skip to content

Commit 9cf84de

Browse files
committed
Added simplified version of bugfix for #19220
1 parent 7189a66 commit 9cf84de

9 files changed

+127
-312
lines changed

src/compiler/checker.ts

Lines changed: 21 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -18382,70 +18382,37 @@ namespace ts {
1838218382
argc++;
1838318383
} while (argc <= maxArgs);
1838418384
});
18385-
if (availableArgumentCounts.length === 0) availableArgumentCounts.push(0);
1838618385

18387-
const orderNumerically = (a: number, b: number) => a < b ? 0 : 1;
18388-
const availableArgumentCountsSorted = sort(availableArgumentCounts, orderNumerically);
18389-
18390-
const min = first(availableArgumentCountsSorted);
18391-
const max = last(availableArgumentCountsSorted);
18386+
const availableArgumentCountsAscending = sort(availableArgumentCounts, (a, b) => a - b);
18387+
const min = first(availableArgumentCountsAscending);
18388+
const max = last(availableArgumentCountsAscending);
1839218389

1839318390
const hasRestParameter = some(signatures, sig => sig.hasRestParameter);
1839418391
const hasSpreadArgument = getSpreadArgumentIndex(args) > -1;
18395-
18392+
const paramCount = hasRestParameter ?
18393+
min :
18394+
min < max ?
18395+
min + "-" + max :
18396+
min;
1839618397
let argCount = args.length;
1839718398
if (argCount <= max && hasSpreadArgument) argCount--;
1839818399

18399-
const availableBelowCurrentArgCount = filter(availableArgumentCountsSorted, c => c < argCount);
18400-
const availableAboveCurrentArgCount = filter(availableArgumentCountsSorted, c => c > argCount);
18401-
18402-
const minAboveArgCount = firstOrUndefined(availableAboveCurrentArgCount);
18403-
const maxBelowArgCount = lastOrUndefined(availableBelowCurrentArgCount);
18404-
18405-
const sortedArrayIsContiguous = (arr: number[]) => arr.length === 0 ? true : last(arr) - first(arr) === arr.length - 1;
18406-
const contiguousBelow = sortedArrayIsContiguous(availableBelowCurrentArgCount);
18407-
const contiguousAbove = sortedArrayIsContiguous(availableAboveCurrentArgCount);
18408-
18409-
const rangeErrorBelow = maxBelowArgCount && min !== maxBelowArgCount ?
18410-
min + "-" + maxBelowArgCount :
18411-
min;
18412-
const rangeErrorAbove = minAboveArgCount && minAboveArgCount !== max ?
18413-
minAboveArgCount + "-" + max :
18414-
max;
18415-
18416-
if (hasSpreadArgument) {
18417-
if (!hasRestParameter && contiguousAbove && argCount < min) {
18418-
diagnostics.add(createDiagnosticForNode(node, Diagnostics.Expected_0_arguments_but_got_1_or_more, rangeErrorAbove, argCount));
18419-
}
18420-
else if (contiguousBelow && argCount > max) {
18421-
diagnostics.add(createDiagnosticForNode(node, Diagnostics.Expected_0_arguments_but_got_1_or_more, rangeErrorBelow, argCount));
18422-
}
18423-
else diagnostics.add(createDiagnosticForNode(node, Diagnostics.Expected_at_least_0_arguments_but_got_1_or_more, min, argCount));
18400+
if (hasRestParameter || hasSpreadArgument) {
18401+
const error = hasRestParameter && hasSpreadArgument ? Diagnostics.Expected_at_least_0_arguments_but_got_1_or_more :
18402+
hasRestParameter ? Diagnostics.Expected_at_least_0_arguments_but_got_1 :
18403+
hasSpreadArgument ? Diagnostics.Expected_0_arguments_but_got_1_or_more :
18404+
undefined;
18405+
diagnostics.add(createDiagnosticForNode(node, error, paramCount, argCount));
1842418406
}
18425-
else if (argCount > max) {
18426-
if (contiguousBelow && !hasRestParameter) {
18427-
diagnostics.add(createDiagnosticForNode(node, Diagnostics.Expected_0_arguments_but_got_1, rangeErrorBelow, argCount));
18428-
}
18429-
else diagnostics.add(createDiagnosticForNode(node, Diagnostics.Expected_no_more_than_0_arguments_but_got_1, max, argCount));
18430-
}
18431-
else if (argCount < min) {
18432-
if (contiguousAbove && !hasRestParameter) {
18433-
diagnostics.add(createDiagnosticForNode(node, Diagnostics.Expected_0_arguments_but_got_1, rangeErrorAbove, argCount));
18434-
}
18435-
else diagnostics.add(createDiagnosticForNode(node, Diagnostics.Expected_at_least_0_arguments_but_got_1, min, argCount));
18436-
}
18437-
else { // there are both signatures taking less and more arguments
18438-
if (contiguousBelow) {
18439-
if (contiguousAbove && !hasRestParameter) {
18440-
diagnostics.add(createDiagnosticForNode(node, Diagnostics.Expected_0_or_1_arguments_but_got_2, rangeErrorBelow, rangeErrorAbove, argCount));
18441-
}
18442-
else diagnostics.add(createDiagnosticForNode(node, Diagnostics.No_overload_expects_0_arguments_The_most_likely_overloads_that_match_expect_either_1_arguments_or_at_least_2_arguments, argCount, rangeErrorBelow, minAboveArgCount));
18407+
else {
18408+
if (min < argCount && argCount < max) {
18409+
const maxBelowArgCount = lastOrUndefined(filter(availableArgumentCountsAscending, c => c < argCount));
18410+
const minAboveArgCount = firstOrUndefined(filter(availableArgumentCountsAscending, c => c > argCount));
18411+
18412+
diagnostics.add(createDiagnosticForNode(node, Diagnostics.No_overload_expects_0_arguments_The_most_likely_overloads_that_match_expect_either_1_arguments_or_at_least_2_arguments, argCount, maxBelowArgCount, minAboveArgCount));
1844318413
}
1844418414
else {
18445-
if (contiguousAbove && !hasRestParameter) {
18446-
diagnostics.add(createDiagnosticForNode(node, Diagnostics.No_overload_expects_0_arguments_The_most_likely_overloads_that_match_expect_either_up_to_1_arguments_or_2_arguments, argCount, maxBelowArgCount, rangeErrorAbove));
18447-
}
18448-
else diagnostics.add(createDiagnosticForNode(node, Diagnostics.No_overload_expects_0_arguments_The_most_likely_overloads_that_match_expect_either_up_to_1_arguments_or_at_least_2_arguments, argCount, maxBelowArgCount, minAboveArgCount));
18415+
diagnostics.add(createDiagnosticForNode(node, Diagnostics.Expected_0_arguments_but_got_1, paramCount, argCount));
1844918416
}
1845018417
}
1845118418
}

src/compiler/diagnosticMessages.json

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2024,25 +2024,9 @@
20242024
"category": "Error",
20252025
"code": 2570
20262026
},
2027-
"Expected no more than {0} arguments but got {1}.": {
2028-
"category": "Error",
2029-
"code": 2571
2030-
},
2031-
"Expected {0} or {1} arguments but got {2}.": {
2032-
"category": "Error",
2033-
"code": 2572
2034-
},
20352027
"No overload expects {0} arguments. The most likely overloads that match expect either {1} arguments or at least {2} arguments.": {
20362028
"category": "Error",
2037-
"code": 2573
2038-
},
2039-
"No overload expects {0} arguments. The most likely overloads that match expect either up to {1} arguments or {2} arguments.": {
2040-
"category": "Error",
2041-
"code": 2574
2042-
},
2043-
"No overload expects {0} arguments. The most likely overloads that match expect either up to {1} arguments or at least {2} arguments.": {
2044-
"category": "Error",
2045-
"code": 2575
2029+
"code": 2571
20462030
},
20472031
"JSX element attributes type '{0}' may not be a union type.": {
20482032
"category": "Error",
Lines changed: 25 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,39 @@
1-
tests/cases/compiler/functionParameterArityMismatch.ts(3,1): error TS2555: Expected at least 1 arguments, but got 0.
2-
tests/cases/compiler/functionParameterArityMismatch.ts(4,1): error TS2572: Expected 1 or 3 arguments but got 2.
3-
tests/cases/compiler/functionParameterArityMismatch.ts(5,1): error TS2571: Expected no more than 3 arguments but got 4.
4-
tests/cases/compiler/functionParameterArityMismatch.ts(9,1): error TS2573: No overload expects 2 arguments. The most likely overloads that match expect either 0-1 arguments or at least 3 arguments.
5-
tests/cases/compiler/functionParameterArityMismatch.ts(14,1): error TS2573: No overload expects 1 arguments. The most likely overloads that match expect either 0 arguments or at least 2 arguments.
6-
tests/cases/compiler/functionParameterArityMismatch.ts(15,1): error TS2574: No overload expects 3 arguments. The most likely overloads that match expect either up to 2 arguments or 4-5 arguments.
7-
tests/cases/compiler/functionParameterArityMismatch.ts(20,1): error TS2573: No overload expects 1 arguments. The most likely overloads that match expect either 0 arguments or at least 2 arguments.
8-
tests/cases/compiler/functionParameterArityMismatch.ts(21,1): error TS2575: No overload expects 3 arguments. The most likely overloads that match expect either up to 2 arguments or at least 4 arguments.
9-
tests/cases/compiler/functionParameterArityMismatch.ts(25,1): error TS2572: Expected 0-2 or 4-5 arguments but got 3.
10-
tests/cases/compiler/functionParameterArityMismatch.ts(26,1): error TS2571: Expected no more than 5 arguments but got 6.
1+
tests/cases/compiler/functionParameterArityMismatch.ts(3,1): error TS2554: Expected 1-3 arguments, but got 0.
2+
tests/cases/compiler/functionParameterArityMismatch.ts(4,1): error TS2571: No overload expects 2 arguments. The most likely overloads that match expect either 1 arguments or at least 3 arguments.
3+
tests/cases/compiler/functionParameterArityMismatch.ts(5,1): error TS2554: Expected 1-3 arguments, but got 4.
4+
tests/cases/compiler/functionParameterArityMismatch.ts(11,1): error TS2571: No overload expects 1 arguments. The most likely overloads that match expect either 0 arguments or at least 2 arguments.
5+
tests/cases/compiler/functionParameterArityMismatch.ts(12,1): error TS2571: No overload expects 3 arguments. The most likely overloads that match expect either 2 arguments or at least 4 arguments.
6+
tests/cases/compiler/functionParameterArityMismatch.ts(13,1): error TS2571: No overload expects 5 arguments. The most likely overloads that match expect either 4 arguments or at least 6 arguments.
7+
tests/cases/compiler/functionParameterArityMismatch.ts(14,1): error TS2554: Expected 0-6 arguments, but got 7.
118

129

13-
==== tests/cases/compiler/functionParameterArityMismatch.ts (10 errors) ====
10+
==== tests/cases/compiler/functionParameterArityMismatch.ts (7 errors) ====
1411
declare function f1(a: number);
1512
declare function f1(a: number, b: number, c: number);
1613
f1();
1714
~~~~
18-
!!! error TS2555: Expected at least 1 arguments, but got 0.
15+
!!! error TS2554: Expected 1-3 arguments, but got 0.
1916
f1(1, 2);
2017
~~~~~~~~
21-
!!! error TS2572: Expected 1 or 3 arguments but got 2.
18+
!!! error TS2571: No overload expects 2 arguments. The most likely overloads that match expect either 1 arguments or at least 3 arguments.
2219
f1(1, 2, 3, 4);
2320
~~~~~~~~~~~~~~
24-
!!! error TS2571: Expected no more than 3 arguments but got 4.
21+
!!! error TS2554: Expected 1-3 arguments, but got 4.
2522

26-
declare function f2(a?: number);
27-
declare function f2(a: number, b: number, c: number, ...d: number[]);
28-
f2(1, 2);
29-
~~~~~~~~
30-
!!! error TS2573: No overload expects 2 arguments. The most likely overloads that match expect either 0-1 arguments or at least 3 arguments.
31-
32-
declare function f3();
33-
declare function f3(a: number, b: number);
34-
declare function f3(a: number, b: number, c: number, d: number, e?: number);
35-
f3(1);
23+
declare function f2();
24+
declare function f2(a: number, b: number);
25+
declare function f2(a: number, b: number, c: number, d: number);
26+
declare function f2(a: number, b: number, c: number, d: number, e: number, f: number);
27+
f2(1);
3628
~~~~~
37-
!!! error TS2573: No overload expects 1 arguments. The most likely overloads that match expect either 0 arguments or at least 2 arguments.
38-
f3(1, 2, 3);
39-
~~~~~~~~~~~
40-
!!! error TS2574: No overload expects 3 arguments. The most likely overloads that match expect either up to 2 arguments or 4-5 arguments.
41-
42-
declare function f4();
43-
declare function f4(a: number, b: number);
44-
declare function f4(a: number, b: number, c: number, d: number, ...e: number[]);
45-
f4(1);
46-
~~~~~
47-
!!! error TS2573: No overload expects 1 arguments. The most likely overloads that match expect either 0 arguments or at least 2 arguments.
48-
f4(1, 2, 3);
49-
~~~~~~~~~~~
50-
!!! error TS2575: No overload expects 3 arguments. The most likely overloads that match expect either up to 2 arguments or at least 4 arguments.
51-
52-
declare function f5(a?: number, b?: number);
53-
declare function f5(a: number, b: number, c: number, d: number, e?: number);
54-
f5(1, 2, 3);
29+
!!! error TS2571: No overload expects 1 arguments. The most likely overloads that match expect either 0 arguments or at least 2 arguments.
30+
f2(1, 2, 3);
5531
~~~~~~~~~~~
56-
!!! error TS2572: Expected 0-2 or 4-5 arguments but got 3.
57-
f5(1, 2, 3, 4, 5, 6);
58-
~~~~~~~~~~~~~~~~~~~~
59-
!!! error TS2571: Expected no more than 5 arguments but got 6.
32+
!!! error TS2571: No overload expects 3 arguments. The most likely overloads that match expect either 2 arguments or at least 4 arguments.
33+
f2(1, 2, 3, 4, 5);
34+
~~~~~~~~~~~~~~~~~
35+
!!! error TS2571: No overload expects 5 arguments. The most likely overloads that match expect either 4 arguments or at least 6 arguments.
36+
f2(1, 2, 3, 4, 5, 6, 7);
37+
~~~~~~~~~~~~~~~~~~~~~~~
38+
!!! error TS2554: Expected 0-6 arguments, but got 7.
6039

tests/baselines/reference/functionParameterArityMismatch.js

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,21 @@ f1();
55
f1(1, 2);
66
f1(1, 2, 3, 4);
77

8-
declare function f2(a?: number);
9-
declare function f2(a: number, b: number, c: number, ...d: number[]);
10-
f2(1, 2);
11-
12-
declare function f3();
13-
declare function f3(a: number, b: number);
14-
declare function f3(a: number, b: number, c: number, d: number, e?: number);
15-
f3(1);
16-
f3(1, 2, 3);
17-
18-
declare function f4();
19-
declare function f4(a: number, b: number);
20-
declare function f4(a: number, b: number, c: number, d: number, ...e: number[]);
21-
f4(1);
22-
f4(1, 2, 3);
23-
24-
declare function f5(a?: number, b?: number);
25-
declare function f5(a: number, b: number, c: number, d: number, e?: number);
26-
f5(1, 2, 3);
27-
f5(1, 2, 3, 4, 5, 6);
8+
declare function f2();
9+
declare function f2(a: number, b: number);
10+
declare function f2(a: number, b: number, c: number, d: number);
11+
declare function f2(a: number, b: number, c: number, d: number, e: number, f: number);
12+
f2(1);
13+
f2(1, 2, 3);
14+
f2(1, 2, 3, 4, 5);
15+
f2(1, 2, 3, 4, 5, 6, 7);
2816

2917

3018
//// [functionParameterArityMismatch.js]
3119
f1();
3220
f1(1, 2);
3321
f1(1, 2, 3, 4);
34-
f2(1, 2);
35-
f3(1);
36-
f3(1, 2, 3);
37-
f4(1);
38-
f4(1, 2, 3);
39-
f5(1, 2, 3);
40-
f5(1, 2, 3, 4, 5, 6);
22+
f2(1);
23+
f2(1, 2, 3);
24+
f2(1, 2, 3, 4, 5);
25+
f2(1, 2, 3, 4, 5, 6, 7);

0 commit comments

Comments
 (0)