Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve accuracy of remove unnecessary await fix #32384

Merged
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
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();
}`
});