Skip to content

Commit 3209373

Browse files
committed
Improve asccuracy of remove unnecessary await fix
1 parent 37f2e59 commit 3209373

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

src/compiler/factory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4701,7 +4701,7 @@ namespace ts {
47014701
}
47024702
}
47034703

4704-
function getLeftmostExpression(node: Expression, stopAtCallExpressions: boolean) {
4704+
export function getLeftmostExpression(node: Expression, stopAtCallExpressions: boolean) {
47054705
while (true) {
47064706
switch (node.kind) {
47074707
case SyntaxKind.PostfixUnaryExpression:

src/services/codefixes/removeUnnecessaryAwait.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,18 @@ namespace ts.codefix {
2626
return;
2727
}
2828

29-
const parenthesizedExpression = tryCast(awaitExpression.parent, isParenthesizedExpression);
30-
const removeParens = parenthesizedExpression && (isIdentifier(awaitExpression.expression) || isCallExpression(awaitExpression.expression));
31-
changeTracker.replaceNode(sourceFile, removeParens ? parenthesizedExpression || awaitExpression : awaitExpression, awaitExpression.expression);
29+
let expressionToReplace: Node = awaitExpression;
30+
const hasSurroundingParens = isParenthesizedExpression(awaitExpression.parent);
31+
if (hasSurroundingParens) {
32+
const leftMostExpression = getLeftmostExpression(awaitExpression.expression, /*stopAtCallExpressions*/ false);
33+
if (isIdentifier(leftMostExpression)) {
34+
const precedingToken = findPrecedingToken(awaitExpression.parent.pos, sourceFile);
35+
if (precedingToken && precedingToken.kind !== SyntaxKind.NewKeyword) {
36+
expressionToReplace = awaitExpression.parent;
37+
}
38+
}
39+
}
40+
41+
changeTracker.replaceNode(sourceFile, expressionToReplace, awaitExpression.expression);
3242
}
3343
}

tests/cases/fourslash/codeFixRemoveUnnecessaryAwait.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
/// <reference path="fourslash.ts" />
22
////declare class C { foo(): void }
3+
////declare function getC(): { Class: C };
34
////declare function foo(): string;
45
////async function f() {
56
//// await "";
67
//// await 0;
78
//// (await foo()).toLowerCase();
89
//// (await 0).toFixed();
910
//// (await new C).foo();
11+
//// (await function() { }());
12+
//// new (await getC()).Class();
1013
////}
1114

1215
verify.codeFix({
1316
description: ts.Diagnostics.Remove_unnecessary_await.message,
1417
index: 0,
1518
newFileContent:
1619
`declare class C { foo(): void }
20+
declare function getC(): { Class: C };
1721
declare function foo(): string;
1822
async function f() {
1923
"";
2024
await 0;
2125
(await foo()).toLowerCase();
2226
(await 0).toFixed();
2327
(await new C).foo();
28+
(await function() { }());
29+
new (await getC()).Class();
2430
}`
2531
});
2632

@@ -29,12 +35,15 @@ verify.codeFixAll({
2935
fixId: "removeUnnecessaryAwait",
3036
newFileContent:
3137
`declare class C { foo(): void }
38+
declare function getC(): { Class: C };
3239
declare function foo(): string;
3340
async function f() {
3441
"";
3542
0;
3643
foo().toLowerCase();
3744
(0).toFixed();
3845
(new C).foo();
46+
(function() { } ());
47+
new (getC()).Class();
3948
}`
4049
});

0 commit comments

Comments
 (0)