Skip to content

Commit

Permalink
fix microsoft#19220: polish arity error message
Browse files Browse the repository at this point in the history
  • Loading branch information
HerringtonDarkholme committed Oct 22, 2017
1 parent ceba507 commit 5e176f3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
35 changes: 20 additions & 15 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16272,23 +16272,28 @@ namespace ts {
diagnostics.add(createDiagnosticForNode(node, Diagnostics.Expected_0_type_arguments_but_got_1, paramCount, typeArguments.length));
}
else if (args) {
let min = Number.POSITIVE_INFINITY;
let max = Number.NEGATIVE_INFINITY;
for (const sig of signatures) {
min = Math.min(min, sig.minArgumentCount);
max = Math.max(max, sig.parameters.length);
}
const hasRestParameter = some(signatures, sig => sig.hasRestParameter);
const hasSpreadArgument = getSpreadArgumentIndex(args) > -1;
const paramCount = hasRestParameter ? min :
min < max ? min + "-" + max :
min;
const argCount = args.length - (hasSpreadArgument ? 1 : 0);
const error = hasRestParameter && hasSpreadArgument ? Diagnostics.Expected_at_least_0_arguments_but_got_a_minimum_of_1 :
hasRestParameter ? Diagnostics.Expected_at_least_0_arguments_but_got_1 :
hasSpreadArgument ? Diagnostics.Expected_0_arguments_but_got_a_minimum_of_1 :
Diagnostics.Expected_0_arguments_but_got_1;
diagnostics.add(createDiagnosticForNode(node, error, paramCount, argCount));
if (hasSpreadArgument) {
const hasRestParameter = some(signatures, sig => sig.hasRestParameter);
const error = hasRestParameter ? Diagnostics.Expected_at_least_0_arguments_but_got_a_minimum_of_1 : Diagnostics.Expected_0_arguments_but_got_1;
const paramCount = reduceLeft(signatures, (min, sig) => Math.min(min, sig.minArgumentCount), Number.POSITIVE_INFINITY);
diagnostics.add(createDiagnosticForNode(node, error, paramCount, argCount));
} else {
let min = Number.POSITIVE_INFINITY;
let max = Number.NEGATIVE_INFINITY;
for (const sig of signatures) {
const minParam = sig.minArgumentCount;
const maxParam = sig.parameters.length;
min = minParam > argCount ? Math.min(min, minParam) : min;
max = maxParam < argCount ? Math.max(max, maxParam) : max;
}
const needMultipleCount = min !== Number.POSITIVE_INFINITY && max !== Number.NEGATIVE_INFINITY && min !== max;
const paramCount = min !== Number.POSITIVE_INFINITY ? min : max;
diagnostics.add(needMultipleCount ?
createDiagnosticForNode(node, Diagnostics.No_overload_expects_0_arguments_The_most_matching_overloads_expect_either_1_or_2_arguments, argCount, max, min) :
createDiagnosticForNode(node, Diagnostics.Expected_0_arguments_but_got_1, paramCount, argCount));
}
}
else if (fallbackError) {
diagnostics.add(createDiagnosticForNode(node, fallbackError));
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -2228,6 +2228,14 @@
"category": "Error",
"code": 2716
},
"Type parameter '{0}' has a circular default.": {
"category": "Error",
"code": 2716
},
"No overload expects {0} arguments. The most matching overloads expect either {1} or {2} arguments.": {
"category": "Error",
"code": 2717
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down

0 comments on commit 5e176f3

Please sign in to comment.