Skip to content

Commit

Permalink
Improve asccuracy of remove unnecessary await fix
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbranch committed Jul 12, 2019
1 parent 37f2e59 commit 3209373
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/compiler/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4701,7 +4701,7 @@ namespace ts {
}
}

function getLeftmostExpression(node: Expression, stopAtCallExpressions: boolean) {
export function getLeftmostExpression(node: Expression, stopAtCallExpressions: boolean) {
while (true) {
switch (node.kind) {
case SyntaxKind.PostfixUnaryExpression:
Expand Down
16 changes: 13 additions & 3 deletions src/services/codefixes/removeUnnecessaryAwait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,18 @@ namespace ts.codefix {
return;
}

const parenthesizedExpression = tryCast(awaitExpression.parent, isParenthesizedExpression);
const removeParens = parenthesizedExpression && (isIdentifier(awaitExpression.expression) || isCallExpression(awaitExpression.expression));
changeTracker.replaceNode(sourceFile, removeParens ? parenthesizedExpression || awaitExpression : awaitExpression, awaitExpression.expression);
let expressionToReplace: Node = awaitExpression;
const hasSurroundingParens = isParenthesizedExpression(awaitExpression.parent);
if (hasSurroundingParens) {
const leftMostExpression = getLeftmostExpression(awaitExpression.expression, /*stopAtCallExpressions*/ false);
if (isIdentifier(leftMostExpression)) {
const precedingToken = findPrecedingToken(awaitExpression.parent.pos, sourceFile);
if (precedingToken && precedingToken.kind !== SyntaxKind.NewKeyword) {
expressionToReplace = awaitExpression.parent;
}
}
}

changeTracker.replaceNode(sourceFile, expressionToReplace, awaitExpression.expression);
}
}
9 changes: 9 additions & 0 deletions tests/cases/fourslash/codeFixRemoveUnnecessaryAwait.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
/// <reference path="fourslash.ts" />
////declare class C { foo(): void }
////declare function getC(): { Class: C };
////declare function foo(): string;
////async function f() {
//// await "";
//// await 0;
//// (await foo()).toLowerCase();
//// (await 0).toFixed();
//// (await new C).foo();
//// (await function() { }());
//// new (await getC()).Class();
////}

verify.codeFix({
description: ts.Diagnostics.Remove_unnecessary_await.message,
index: 0,
newFileContent:
`declare class C { foo(): void }
declare function getC(): { Class: C };
declare function foo(): string;
async function f() {
"";
await 0;
(await foo()).toLowerCase();
(await 0).toFixed();
(await new C).foo();
(await function() { }());
new (await getC()).Class();
}`
});

Expand All @@ -29,12 +35,15 @@ verify.codeFixAll({
fixId: "removeUnnecessaryAwait",
newFileContent:
`declare class C { foo(): void }
declare function getC(): { Class: C };
declare function foo(): string;
async function f() {
"";
0;
foo().toLowerCase();
(0).toFixed();
(new C).foo();
(function() { } ());
new (getC()).Class();
}`
});

0 comments on commit 3209373

Please sign in to comment.