Skip to content

Commit

Permalink
Only remove parens for identifiers and call expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbranch committed Jul 12, 2019
1 parent e27d260 commit f22dc9d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 45 deletions.
31 changes: 2 additions & 29 deletions src/services/codefixes/removeUnnecessaryAwait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,7 @@ namespace ts.codefix {
}

const parenthesizedExpression = tryCast(awaitExpression.parent, isParenthesizedExpression);
const preserveParens = parenthesizedExpression && (
// (await 0).toFixed() should keep its parens (or add an extra dot for 0..toFixed())
isPropertyAccessExpression(parenthesizedExpression.parent) && isDecimalIntegerLiteral(awaitExpression.expression, sourceFile) ||
// new (await c).Class()
isPropertyAccessExpressionInNewExpression(parenthesizedExpression.parent) ||
// (await new C).foo
isNewExpressionWithoutParens(awaitExpression.expression)
);

if (preserveParens) {
changeTracker.replaceNode(sourceFile, awaitExpression, awaitExpression.expression);
}
else {
changeTracker.replaceNode(sourceFile, parenthesizedExpression || awaitExpression, awaitExpression.expression);
}
}

function isPropertyAccessExpressionInNewExpression(expression: Node) {
return isPropertyAccessExpression(expression) && !!findAncestor(expression, ancestor => {
return isPropertyAccessExpression(ancestor)
? false
: isNewExpression(ancestor)
? true
: "quit";
});
}

function isNewExpressionWithoutParens(expression: Node) {
return isNewExpression(expression) && expression.getLastToken() === expression.expression;
const removeParens = parenthesizedExpression && (isIdentifier(awaitExpression.expression) || isCallExpression(awaitExpression.expression));
changeTracker.replaceNode(sourceFile, removeParens ? parenthesizedExpression || awaitExpression : awaitExpression, awaitExpression.expression);
}
}
4 changes: 0 additions & 4 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1992,10 +1992,6 @@ namespace ts {
return typeIsAccessible ? res : undefined;
}

export function isDecimalIntegerLiteral(node: Node, sourceFile: SourceFile): node is NumericLiteral {
return isNumericLiteral(node) && /^\d+$/.test(node.getText(sourceFile));
}

export function syntaxUsuallyHasTrailingSemicolon(kind: SyntaxKind) {
return kind === SyntaxKind.VariableStatement
|| kind === SyntaxKind.ExpressionStatement
Expand Down
18 changes: 6 additions & 12 deletions tests/cases/fourslash/codeFixRemoveUnnecessaryAwait.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
/// <reference path="fourslash.ts" />
////declare class C { foo(): void }
////declare function getC(): { foo: { Class: C } }
////declare function foo(): string;
////async function f() {
//// await "";
//// await 0;
//// (await "").toLowerCase();
//// (await foo()).toLowerCase();
//// (await 0).toFixed();
//// (await 3.2).toFixed();
//// (await new C).foo();
//// new (await getC()).foo.Class();
////}

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

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

0 comments on commit f22dc9d

Please sign in to comment.