Skip to content

Commit a074bf4

Browse files
committed
Code review simplifications
1 parent 95a0baa commit a074bf4

File tree

6 files changed

+36
-43
lines changed

6 files changed

+36
-43
lines changed

src/compiler/checker.ts

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19043,47 +19043,40 @@ namespace ts {
1904319043
diagnostics.add(getTypeArgumentArityError(node, signatures, typeArguments));
1904419044
}
1904519045
else if (args) {
19046-
const availableArgumentCounts: number[] = [];
19047-
forEach(signatures, (sig: Signature) => {
19048-
const maxArgs = sig.parameters.length;
19049-
let argc = sig.minArgumentCount;
19050-
do {
19051-
pushIfUnique(availableArgumentCounts, argc);
19052-
argc++;
19053-
} while (argc <= maxArgs);
19054-
});
19046+
let min = Number.POSITIVE_INFINITY;
19047+
let max = Number.NEGATIVE_INFINITY;
19048+
let belowArgCount = Number.NEGATIVE_INFINITY;
19049+
let aboveArgCount = Number.POSITIVE_INFINITY;
19050+
19051+
let argCount = args.length;
19052+
for (const sig of signatures) {
19053+
const minCount = getMinArgumentCount(sig);
19054+
const maxCount = getParameterCount(sig);
19055+
if (minCount < argCount && minCount > belowArgCount) belowArgCount = minCount;
19056+
if (argCount < maxCount && maxCount < aboveArgCount) aboveArgCount = maxCount;
19057+
min = Math.min(min, minCount);
19058+
max = Math.max(max, maxCount);
19059+
}
1905519060

19056-
const availableArgumentCountsAscending = sort(availableArgumentCounts, (a: number, b: number) => a - b);
19057-
const min = first(availableArgumentCountsAscending);
19058-
const max = last(availableArgumentCountsAscending);
19061+
const hasSpreadArgument = getSpreadArgumentIndex(args) > -1;
19062+
if (argCount <= max && hasSpreadArgument) argCount--;
1905919063

1906019064
const hasRestParameter = some(signatures, (sig: Signature) => sig.hasRestParameter);
19061-
const hasSpreadArgument = getSpreadArgumentIndex(args) > -1;
1906219065
const paramRange: string | number = hasRestParameter ?
1906319066
min :
1906419067
min < max ?
1906519068
min + "-" + max :
1906619069
min;
19067-
let argCount = args.length;
19068-
if (argCount <= max && hasSpreadArgument) argCount--;
19069-
1907019070
if (hasRestParameter || hasSpreadArgument) {
1907119071
const error = hasRestParameter && hasSpreadArgument ? Diagnostics.Expected_at_least_0_arguments_but_got_1_or_more :
19072-
hasRestParameter ? Diagnostics.Expected_at_least_0_arguments_but_got_1 :
19073-
hasSpreadArgument ? Diagnostics.Expected_0_arguments_but_got_1_or_more :
19074-
undefined;
19075-
diagnostics.add(createDiagnosticForNode(node, <DiagnosticMessage>error, paramRange, argCount));
19072+
hasRestParameter ? Diagnostics.Expected_at_least_0_arguments_but_got_1 : Diagnostics.Expected_0_arguments_but_got_1_or_more;
19073+
diagnostics.add(createDiagnosticForNode(node, error, paramRange, argCount));
19074+
}
19075+
else if (min < argCount && argCount < max) {
19076+
diagnostics.add(createDiagnosticForNode(node, Diagnostics.No_overload_expects_0_arguments_but_overloads_do_exist_that_expect_either_1_arguments_or_at_least_2_arguments, argCount, belowArgCount, aboveArgCount));
1907619077
}
1907719078
else {
19078-
if (min < argCount && argCount < max) {
19079-
const maxBelowArgCount = lastOrUndefined(filter(availableArgumentCountsAscending, c => c < argCount));
19080-
const minAboveArgCount = firstOrUndefined(filter(availableArgumentCountsAscending, c => c > argCount));
19081-
19082-
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));
19083-
}
19084-
else {
19085-
diagnostics.add(createDiagnosticForNode(node, Diagnostics.Expected_0_arguments_but_got_1, paramRange, argCount));
19086-
}
19079+
diagnostics.add(createDiagnosticForNode(node, Diagnostics.Expected_0_arguments_but_got_1, paramRange, argCount));
1908719080
}
1908819081
}
1908919082
else if (fallbackError) {

src/compiler/diagnosticMessages.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2056,7 +2056,7 @@
20562056
"category": "Error",
20572057
"code": 2574
20582058
},
2059-
"No overload expects {0} arguments. The most likely overloads that match expect either {1} arguments or at least {2} arguments.": {
2059+
"No overload expects {0} arguments, but overloads do exist that expect either {1} or at least {2} arguments.": {
20602060
"category": "Error",
20612061
"code": 2575
20622062
},

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5344,7 +5344,7 @@ declare namespace ts {
53445344
Rest_signatures_are_incompatible: DiagnosticMessage;
53455345
Property_0_is_incompatible_with_rest_element_type: DiagnosticMessage;
53465346
A_rest_element_type_must_be_an_array_type: DiagnosticMessage;
5347-
No_overload_expects_0_arguments_The_most_likely_overloads_that_match_expect_either_1_arguments_or_at_least_2_arguments: DiagnosticMessage;
5347+
No_overload_expects_0_arguments_but_overloads_do_exist_that_expect_either_1_arguments_or_at_least_2_arguments: DiagnosticMessage;
53485348
JSX_element_attributes_type_0_may_not_be_a_union_type: DiagnosticMessage;
53495349
The_return_type_of_a_JSX_element_constructor_must_return_an_object_type: DiagnosticMessage;
53505350
JSX_element_implicitly_has_type_any_because_the_global_type_JSX_Element_does_not_exist: DiagnosticMessage;

tests/baselines/reference/functionParameterArityMismatch.errors.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
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 TS2575: No overload expects 2 arguments. The most likely overloads that match expect either 1 arguments or at least 3 arguments.
2+
tests/cases/compiler/functionParameterArityMismatch.ts(4,1): error TS2575: No overload expects 2 arguments, but overloads do exist that expect either 1 arguments or at least 3 arguments.
33
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 TS2575: 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 TS2575: 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 TS2575: No overload expects 5 arguments. The most likely overloads that match expect either 4 arguments or at least 6 arguments.
4+
tests/cases/compiler/functionParameterArityMismatch.ts(11,1): error TS2575: No overload expects 1 arguments, but overloads do exist that expect either 0 arguments or at least 2 arguments.
5+
tests/cases/compiler/functionParameterArityMismatch.ts(12,1): error TS2575: No overload expects 3 arguments, but overloads do exist that expect either 2 arguments or at least 4 arguments.
6+
tests/cases/compiler/functionParameterArityMismatch.ts(13,1): error TS2575: No overload expects 5 arguments, but overloads do exist that expect either 4 arguments or at least 6 arguments.
77
tests/cases/compiler/functionParameterArityMismatch.ts(14,1): error TS2554: Expected 0-6 arguments, but got 7.
88

99

@@ -15,7 +15,7 @@ tests/cases/compiler/functionParameterArityMismatch.ts(14,1): error TS2554: Expe
1515
!!! error TS2554: Expected 1-3 arguments, but got 0.
1616
f1(1, 2);
1717
~~~~~~~~
18-
!!! error TS2575: No overload expects 2 arguments. The most likely overloads that match expect either 1 arguments or at least 3 arguments.
18+
!!! error TS2575: No overload expects 2 arguments, but overloads do exist that expect either 1 arguments or at least 3 arguments.
1919
f1(1, 2, 3, 4);
2020
~~~~~~~~~~~~~~
2121
!!! error TS2554: Expected 1-3 arguments, but got 4.
@@ -26,13 +26,13 @@ tests/cases/compiler/functionParameterArityMismatch.ts(14,1): error TS2554: Expe
2626
declare function f2(a: number, b: number, c: number, d: number, e: number, f: number);
2727
f2(1);
2828
~~~~~
29-
!!! error TS2575: No overload expects 1 arguments. The most likely overloads that match expect either 0 arguments or at least 2 arguments.
29+
!!! error TS2575: No overload expects 1 arguments, but overloads do exist that expect either 0 arguments or at least 2 arguments.
3030
f2(1, 2, 3);
3131
~~~~~~~~~~~
32-
!!! error TS2575: No overload expects 3 arguments. The most likely overloads that match expect either 2 arguments or at least 4 arguments.
32+
!!! error TS2575: No overload expects 3 arguments, but overloads do exist that expect either 2 arguments or at least 4 arguments.
3333
f2(1, 2, 3, 4, 5);
3434
~~~~~~~~~~~~~~~~~
35-
!!! error TS2575: No overload expects 5 arguments. The most likely overloads that match expect either 4 arguments or at least 6 arguments.
35+
!!! error TS2575: No overload expects 5 arguments, but overloads do exist that expect either 4 arguments or at least 6 arguments.
3636
f2(1, 2, 3, 4, 5, 6, 7);
3737
~~~~~~~~~~~~~~~~~~~~~~~
3838
!!! error TS2554: Expected 0-6 arguments, but got 7.

tests/baselines/reference/genericRestParameters1.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/conformance/types/rest/genericRestParameters1.ts(22,1): error TS2557: Expected at least 0 arguments, but got 2 or more.
1+
tests/cases/conformance/types/rest/genericRestParameters1.ts(22,1): error TS2557: Expected at least 3 arguments, but got 1 or more.
22
tests/cases/conformance/types/rest/genericRestParameters1.ts(31,1): error TS2556: Expected 3 arguments, but got 1 or more.
33

44

@@ -26,7 +26,7 @@ tests/cases/conformance/types/rest/genericRestParameters1.ts(31,1): error TS2556
2626
f1(ns[0], ns[1], true);
2727
f1(...ns, true); // Error, tuple spread only expanded when last
2828
~~~~~~~~~~~~~~~
29-
!!! error TS2557: Expected at least 0 arguments, but got 2 or more.
29+
!!! error TS2557: Expected at least 3 arguments, but got 1 or more.
3030

3131
f2(42, "hello", true);
3232
f2(t3[0], t3[1], t3[2]);
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
tests/cases/conformance/es6/destructuring/iterableArrayPattern25.ts(1,33): error TS2501: A rest element cannot contain a binding pattern.
2-
tests/cases/conformance/es6/destructuring/iterableArrayPattern25.ts(2,1): error TS2555: Expected at least 0 arguments, but got 1.
2+
tests/cases/conformance/es6/destructuring/iterableArrayPattern25.ts(2,1): error TS2555: Expected at least 2 arguments, but got 1.
33

44

55
==== tests/cases/conformance/es6/destructuring/iterableArrayPattern25.ts (2 errors) ====
@@ -8,4 +8,4 @@ tests/cases/conformance/es6/destructuring/iterableArrayPattern25.ts(2,1): error
88
!!! error TS2501: A rest element cannot contain a binding pattern.
99
takeFirstTwoEntries(new Map([["", 0], ["hello", 1]]));
1010
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11-
!!! error TS2555: Expected at least 0 arguments, but got 1.
11+
!!! error TS2555: Expected at least 2 arguments, but got 1.

0 commit comments

Comments
 (0)