Skip to content

Add JS-specific diagnostic message for resolve() in Promise where type argument can't be inferred #48533

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

Merged
merged 3 commits into from
Apr 27, 2022
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
12 changes: 9 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30413,9 +30413,15 @@ namespace ts {
const parameterRange = hasRestParameter ? min
: min < max ? min + "-" + max
: min;
const error = hasRestParameter ? Diagnostics.Expected_at_least_0_arguments_but_got_1
: parameterRange === 1 && args.length === 0 && isPromiseResolveArityError(node) ? Diagnostics.Expected_0_arguments_but_got_1_Did_you_forget_to_include_void_in_your_type_argument_to_Promise
: Diagnostics.Expected_0_arguments_but_got_1;
const isVoidPromiseError = !hasRestParameter && parameterRange === 1 && args.length === 0 && isPromiseResolveArityError(node);
if (isVoidPromiseError && isInJSFile(node)) {
return getDiagnosticForCallNode(node, Diagnostics.Expected_1_argument_but_got_0_new_Promise_needs_a_JSDoc_hint_to_produce_a_resolve_that_can_be_called_without_arguments);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any convenient way to get the name of the function? I tried symbolName(node.symbol) without success

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolveName(node.expression, node.expression.escapedText, SymbolFlags.Value, undefined, undefined, false)?.name

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may also be reasonable just to call it resolve since that’s the parameter name in the library file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will change it to a straightforward string

}
const error = hasRestParameter
? Diagnostics.Expected_at_least_0_arguments_but_got_1
: isVoidPromiseError
? Diagnostics.Expected_0_arguments_but_got_1_Did_you_forget_to_include_void_in_your_type_argument_to_Promise
: Diagnostics.Expected_0_arguments_but_got_1;
if (min < args.length && args.length < max) {
// between min and max, but with no matching overload
return getDiagnosticForCallNode(node, Diagnostics.No_overload_expects_0_arguments_but_overloads_do_exist_that_expect_either_1_or_2_arguments, args.length, maxBelow, minAbove);
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3369,6 +3369,10 @@
"category": "Error",
"code": 2809
},
"Expected 1 argument, but got 0. 'new Promise()' needs a JSDoc hint to produce a 'resolve' that can be called without arguments.": {
"category": "Error",
"code": 2810
},
"Initializer for property '{0}'": {
"category": "Error",
"code": 2811
Expand Down
1 change: 1 addition & 0 deletions src/services/codefixes/fixAddVoidToPromise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace ts.codefix {
const fixName = "addVoidToPromise";
const fixId = "addVoidToPromise";
const errorCodes = [
Diagnostics.Expected_1_argument_but_got_0_new_Promise_needs_a_JSDoc_hint_to_produce_a_resolve_that_can_be_called_without_arguments.code,
Diagnostics.Expected_0_arguments_but_got_1_Did_you_forget_to_include_void_in_your_type_argument_to_Promise.code
];
registerCodeFix({
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/fourslash/codeFixAddVoidToPromiseJS.1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
////const p1 = new Promise(resolve => resolve());

verify.codeFix({
errorCode: 2794,
errorCode: 2810,
description: "Add 'void' to Promise resolved without a value",
index: 2,
newFileContent: `const p1 = /** @type {Promise<void>} */(new Promise(resolve => resolve()));`
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/fourslash/codeFixAddVoidToPromiseJS.2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
////const p2 = /** @type {Promise<number>} */(new Promise(resolve => resolve()));

verify.codeFix({
errorCode: 2794,
errorCode: 2810,
description: "Add 'void' to Promise resolved without a value",
index: 2,
newFileContent: `const p2 = /** @type {Promise<number | void>} */(new Promise(resolve => resolve()));`
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/fourslash/codeFixAddVoidToPromiseJS.3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
////const p3 = /** @type {Promise<number | string>} */(new Promise(resolve => resolve()));

verify.codeFix({
errorCode: 2794,
errorCode: 2810,
description: "Add 'void' to Promise resolved without a value",
index: 2,
newFileContent: `const p3 = /** @type {Promise<number | string | void>} */(new Promise(resolve => resolve()));`
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/fourslash/codeFixAddVoidToPromiseJS.4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
////const p4 = /** @type {Promise<{ x: number } & { y: string }>} */(new Promise(resolve => resolve()));

verify.codeFix({
errorCode: 2794,
errorCode: 2810,
description: "Add 'void' to Promise resolved without a value",
index: 2,
newFileContent: `const p4 = /** @type {Promise<({ x: number } & { y: string }) | void>} */(new Promise(resolve => resolve()));`
Expand Down