Skip to content
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
13 changes: 12 additions & 1 deletion src/services/suggestionDiagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,18 @@ namespace ts {
}

function isPromiseHandler(node: Node): node is CallExpression {
return isCallExpression(node) && (hasPropertyAccessExpressionWithName(node, "then") || hasPropertyAccessExpressionWithName(node, "catch"));
return isCallExpression(node) && (
hasPropertyAccessExpressionWithName(node, "then") && hasSupportedNumberOfArguments(node) ||
hasPropertyAccessExpressionWithName(node, "catch"));
}

function hasSupportedNumberOfArguments(node: CallExpression) {
if (node.arguments.length > 2) return false;
if (node.arguments.length < 2) return true;
return some(node.arguments, arg => {
return arg.kind === SyntaxKind.NullKeyword ||
isIdentifier(arg) && arg.text === "undefined";
});
}

// should be kept up to date with getTransformationBody in convertToAsyncFunction.ts
Expand Down
13 changes: 9 additions & 4 deletions src/testRunner/unittests/services/convertToAsyncFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,13 +558,13 @@ function [#|f|]():void{
}
`
);
_testConvertToAsyncFunction("convertToAsyncFunction_Rej", `
_testConvertToAsyncFunctionFailed("convertToAsyncFunction_Rej", `
function [#|f|]():Promise<void> {
return fetch('https://typescriptlang.org').then(result => { console.log(result); }, rejection => { console.log("rejected:", rejection); });
}
`
);
_testConvertToAsyncFunction("convertToAsyncFunction_RejRef", `
_testConvertToAsyncFunctionFailed("convertToAsyncFunction_RejRef", `
function [#|f|]():Promise<void> {
return fetch('https://typescriptlang.org').then(res, rej);
}
Expand All @@ -576,7 +576,7 @@ function rej(err){
}
`
);
_testConvertToAsyncFunction("convertToAsyncFunction_RejNoBrackets", `
_testConvertToAsyncFunctionFailed("convertToAsyncFunction_RejNoBrackets", `
function [#|f|]():Promise<void> {
return fetch('https://typescriptlang.org').then(result => console.log(result), rejection => console.log("rejected:", rejection));
}
Expand Down Expand Up @@ -1238,7 +1238,7 @@ function [#|f|]() {
}
`);

_testConvertToAsyncFunction("convertToAsyncFunction_ResRejNoArgsArrow", `
_testConvertToAsyncFunctionFailed("convertToAsyncFunction_ResRejNoArgsArrow", `
function [#|f|]() {
return Promise.resolve().then(() => 1, () => "a");
}
Expand Down Expand Up @@ -1436,5 +1436,10 @@ function [#|get|]() {
.catch<APIResponse<{ email: string }>>(() => ({ success: false }));
}
`);

_testConvertToAsyncFunctionFailed("convertToAsyncFunction_threeArguments", `
function [#|f|]() {
return Promise.resolve().then(undefined, undefined, () => 1);
}`);
});
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.