Skip to content

Commit cd28af0

Browse files
Merge pull request #27188 from uniqueiniquity/miscAsyncFixes
Miscellaneous async code fix fixes
2 parents 4009d16 + b850b3b commit cd28af0

File tree

4 files changed

+48
-8
lines changed

4 files changed

+48
-8
lines changed

src/services/codefixes/convertToAsyncFunction.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ namespace ts.codefix {
372372
// the identifier is empty when the handler (.then()) ignores the argument - In this situation we do not need to save the result of the promise returning call
373373
const originalNodeParent = node.original ? node.original.parent : node.parent;
374374
if (prevArgName && !shouldReturn && (!originalNodeParent || isPropertyAccessExpression(originalNodeParent))) {
375-
return createTransformedStatement(prevArgName, createAwait(node), transformer).concat(); // hack to make the types match
375+
return createTransformedStatement(prevArgName, createAwait(node), transformer);
376376
}
377377
else if (!prevArgName && !shouldReturn && (!originalNodeParent || isPropertyAccessExpression(originalNodeParent))) {
378378
return [createStatement(createAwait(node))];
@@ -381,7 +381,7 @@ namespace ts.codefix {
381381
return [createReturn(getSynthesizedDeepClone(node))];
382382
}
383383

384-
function createTransformedStatement(prevArgName: SynthIdentifier | undefined, rightHandSide: Expression, transformer: Transformer): NodeArray<Statement> {
384+
function createTransformedStatement(prevArgName: SynthIdentifier | undefined, rightHandSide: Expression, transformer: Transformer): MutableNodeArray<Statement> {
385385
if (!prevArgName || prevArgName.identifier.text.length === 0) {
386386
// if there's no argName to assign to, there still might be side effects
387387
return createNodeArray([createStatement(rightHandSide)]);
@@ -405,7 +405,10 @@ namespace ts.codefix {
405405
// do not produce a transformed statement for a null argument
406406
break;
407407
case SyntaxKind.Identifier: // identifier includes undefined
408-
if (!argName) break;
408+
if (!argName) {
409+
// undefined was argument passed to promise handler
410+
break;
411+
}
409412

410413
const synthCall = createCall(getSynthesizedDeepClone(func) as Identifier, /*typeArguments*/ undefined, argName ? [argName.identifier] : []);
411414
if (shouldReturn) {
@@ -533,7 +536,7 @@ namespace ts.codefix {
533536
return innerCbBody;
534537
}
535538

536-
function getArgName(funcNode: Node, transformer: Transformer): SynthIdentifier | undefined {
539+
function getArgName(funcNode: Expression, transformer: Transformer): SynthIdentifier | undefined {
537540

538541
const numberOfAssignmentsOriginal = 0;
539542
const types: Type[] = [];
@@ -543,20 +546,21 @@ namespace ts.codefix {
543546
if (isFunctionLikeDeclaration(funcNode)) {
544547
if (funcNode.parameters.length > 0) {
545548
const param = funcNode.parameters[0].name as Identifier;
546-
name = getMapEntryIfExists(param);
549+
name = getMapEntryOrDefault(param);
547550
}
548551
}
549552
else if (isIdentifier(funcNode)) {
550-
name = getMapEntryIfExists(funcNode);
553+
name = getMapEntryOrDefault(funcNode);
551554
}
552555

553-
if (!name || name.identifier === undefined || name.identifier.text === "undefined") {
556+
// return undefined argName when arg is null or undefined
557+
if (!name || name.identifier.text === "undefined") {
554558
return undefined;
555559
}
556560

557561
return name;
558562

559-
function getMapEntryIfExists(identifier: Identifier): SynthIdentifier {
563+
function getMapEntryOrDefault(identifier: Identifier): SynthIdentifier {
560564
const originalNode = getOriginalNode(identifier);
561565
const symbol = getSymbol(originalNode);
562566

src/testRunner/unittests/convertToAsyncFunction.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,12 @@ function [#|f|]():Promise<void | Response> {
487487
function [#|f|]() {
488488
return fetch('https://typescriptlang.org').then(undefined, rejection => console.log("rejected:", rejection));
489489
}
490+
`
491+
);
492+
_testConvertToAsyncFunction("convertToAsyncFunction_NoCatchHandler", `
493+
function [#|f|]() {
494+
return fetch('https://typescriptlang.org').then(x => x.statusText).catch(undefined);
495+
}
490496
`
491497
);
492498
_testConvertToAsyncFunctionFailed("convertToAsyncFunction_NoSuggestion", `
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// ==ORIGINAL==
2+
3+
function /*[#|*/f/*|]*/() {
4+
return fetch('https://typescriptlang.org').then(x => x.statusText).catch(undefined);
5+
}
6+
7+
// ==ASYNC FUNCTION::Convert to async function==
8+
9+
async function f() {
10+
try {
11+
const x = await fetch('https://typescriptlang.org');
12+
return x.statusText;
13+
}
14+
catch (e) { }
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// ==ORIGINAL==
2+
3+
function /*[#|*/f/*|]*/() {
4+
return fetch('https://typescriptlang.org').then(x => x.statusText).catch(undefined);
5+
}
6+
7+
// ==ASYNC FUNCTION::Convert to async function==
8+
9+
async function f() {
10+
try {
11+
const x = await fetch('https://typescriptlang.org');
12+
return x.statusText;
13+
}
14+
catch (e) { }
15+
}

0 commit comments

Comments
 (0)