From f22dc9d6c23fab2c190e23c27f81ab4efee4163b Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Fri, 12 Jul 2019 09:15:56 -0700 Subject: [PATCH] Only remove parens for identifiers and call expressions --- .../codefixes/removeUnnecessaryAwait.ts | 31 ++----------------- src/services/utilities.ts | 4 --- .../codeFixRemoveUnnecessaryAwait.ts | 18 ++++------- 3 files changed, 8 insertions(+), 45 deletions(-) diff --git a/src/services/codefixes/removeUnnecessaryAwait.ts b/src/services/codefixes/removeUnnecessaryAwait.ts index 93e76198a5341..c235d028efbce 100644 --- a/src/services/codefixes/removeUnnecessaryAwait.ts +++ b/src/services/codefixes/removeUnnecessaryAwait.ts @@ -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); } } diff --git a/src/services/utilities.ts b/src/services/utilities.ts index 0dd15bcbab581..cbadfbc9b53a8 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -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 diff --git a/tests/cases/fourslash/codeFixRemoveUnnecessaryAwait.ts b/tests/cases/fourslash/codeFixRemoveUnnecessaryAwait.ts index 021a8f2a67608..b8e1de4605e27 100644 --- a/tests/cases/fourslash/codeFixRemoveUnnecessaryAwait.ts +++ b/tests/cases/fourslash/codeFixRemoveUnnecessaryAwait.ts @@ -1,14 +1,12 @@ /// ////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({ @@ -16,15 +14,13 @@ verify.codeFix({ 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(); }` }); @@ -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(); }` });