Skip to content

Commit 689822c

Browse files
authored
convertToAsyncFunction: Disable for .then with both fulfillment and rejection handlers (#38152)
* Disable convert to async for `.then` with both fulfillment and rejection handlers * Delete baselines * Also disable refactor for 3+ arguments
1 parent a7d6825 commit 689822c

File tree

7 files changed

+21
-102
lines changed

7 files changed

+21
-102
lines changed

src/services/suggestionDiagnostics.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,18 @@ namespace ts {
156156
}
157157

158158
function isPromiseHandler(node: Node): node is CallExpression {
159-
return isCallExpression(node) && (hasPropertyAccessExpressionWithName(node, "then") || hasPropertyAccessExpressionWithName(node, "catch"));
159+
return isCallExpression(node) && (
160+
hasPropertyAccessExpressionWithName(node, "then") && hasSupportedNumberOfArguments(node) ||
161+
hasPropertyAccessExpressionWithName(node, "catch"));
162+
}
163+
164+
function hasSupportedNumberOfArguments(node: CallExpression) {
165+
if (node.arguments.length > 2) return false;
166+
if (node.arguments.length < 2) return true;
167+
return some(node.arguments, arg => {
168+
return arg.kind === SyntaxKind.NullKeyword ||
169+
isIdentifier(arg) && arg.text === "undefined";
170+
});
160171
}
161172

162173
// should be kept up to date with getTransformationBody in convertToAsyncFunction.ts

src/testRunner/unittests/services/convertToAsyncFunction.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -558,13 +558,13 @@ function [#|f|]():void{
558558
}
559559
`
560560
);
561-
_testConvertToAsyncFunction("convertToAsyncFunction_Rej", `
561+
_testConvertToAsyncFunctionFailed("convertToAsyncFunction_Rej", `
562562
function [#|f|]():Promise<void> {
563563
return fetch('https://typescriptlang.org').then(result => { console.log(result); }, rejection => { console.log("rejected:", rejection); });
564564
}
565565
`
566566
);
567-
_testConvertToAsyncFunction("convertToAsyncFunction_RejRef", `
567+
_testConvertToAsyncFunctionFailed("convertToAsyncFunction_RejRef", `
568568
function [#|f|]():Promise<void> {
569569
return fetch('https://typescriptlang.org').then(res, rej);
570570
}
@@ -576,7 +576,7 @@ function rej(err){
576576
}
577577
`
578578
);
579-
_testConvertToAsyncFunction("convertToAsyncFunction_RejNoBrackets", `
579+
_testConvertToAsyncFunctionFailed("convertToAsyncFunction_RejNoBrackets", `
580580
function [#|f|]():Promise<void> {
581581
return fetch('https://typescriptlang.org').then(result => console.log(result), rejection => console.log("rejected:", rejection));
582582
}
@@ -1238,7 +1238,7 @@ function [#|f|]() {
12381238
}
12391239
`);
12401240

1241-
_testConvertToAsyncFunction("convertToAsyncFunction_ResRejNoArgsArrow", `
1241+
_testConvertToAsyncFunctionFailed("convertToAsyncFunction_ResRejNoArgsArrow", `
12421242
function [#|f|]() {
12431243
return Promise.resolve().then(() => 1, () => "a");
12441244
}
@@ -1436,5 +1436,10 @@ function [#|get|]() {
14361436
.catch<APIResponse<{ email: string }>>(() => ({ success: false }));
14371437
}
14381438
`);
1439+
1440+
_testConvertToAsyncFunctionFailed("convertToAsyncFunction_threeArguments", `
1441+
function [#|f|]() {
1442+
return Promise.resolve().then(undefined, undefined, () => 1);
1443+
}`);
14391444
});
14401445
}

tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_Rej.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_RejNoBrackets.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_RejRef.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_ResRejNoArgsArrow.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_ResRejNoArgsArrow.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)