Skip to content

Commit f22dc9d

Browse files
committed
Only remove parens for identifiers and call expressions
1 parent e27d260 commit f22dc9d

File tree

3 files changed

+8
-45
lines changed

3 files changed

+8
-45
lines changed

src/services/codefixes/removeUnnecessaryAwait.ts

+2-29
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,7 @@ namespace ts.codefix {
2727
}
2828

2929
const parenthesizedExpression = tryCast(awaitExpression.parent, isParenthesizedExpression);
30-
const preserveParens = parenthesizedExpression && (
31-
// (await 0).toFixed() should keep its parens (or add an extra dot for 0..toFixed())
32-
isPropertyAccessExpression(parenthesizedExpression.parent) && isDecimalIntegerLiteral(awaitExpression.expression, sourceFile) ||
33-
// new (await c).Class()
34-
isPropertyAccessExpressionInNewExpression(parenthesizedExpression.parent) ||
35-
// (await new C).foo
36-
isNewExpressionWithoutParens(awaitExpression.expression)
37-
);
38-
39-
if (preserveParens) {
40-
changeTracker.replaceNode(sourceFile, awaitExpression, awaitExpression.expression);
41-
}
42-
else {
43-
changeTracker.replaceNode(sourceFile, parenthesizedExpression || awaitExpression, awaitExpression.expression);
44-
}
45-
}
46-
47-
function isPropertyAccessExpressionInNewExpression(expression: Node) {
48-
return isPropertyAccessExpression(expression) && !!findAncestor(expression, ancestor => {
49-
return isPropertyAccessExpression(ancestor)
50-
? false
51-
: isNewExpression(ancestor)
52-
? true
53-
: "quit";
54-
});
55-
}
56-
57-
function isNewExpressionWithoutParens(expression: Node) {
58-
return isNewExpression(expression) && expression.getLastToken() === expression.expression;
30+
const removeParens = parenthesizedExpression && (isIdentifier(awaitExpression.expression) || isCallExpression(awaitExpression.expression));
31+
changeTracker.replaceNode(sourceFile, removeParens ? parenthesizedExpression || awaitExpression : awaitExpression, awaitExpression.expression);
5932
}
6033
}

src/services/utilities.ts

-4
Original file line numberDiff line numberDiff line change
@@ -1992,10 +1992,6 @@ namespace ts {
19921992
return typeIsAccessible ? res : undefined;
19931993
}
19941994

1995-
export function isDecimalIntegerLiteral(node: Node, sourceFile: SourceFile): node is NumericLiteral {
1996-
return isNumericLiteral(node) && /^\d+$/.test(node.getText(sourceFile));
1997-
}
1998-
19991995
export function syntaxUsuallyHasTrailingSemicolon(kind: SyntaxKind) {
20001996
return kind === SyntaxKind.VariableStatement
20011997
|| kind === SyntaxKind.ExpressionStatement
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
11
/// <reference path="fourslash.ts" />
22
////declare class C { foo(): void }
3-
////declare function getC(): { foo: { Class: C } }
3+
////declare function foo(): string;
44
////async function f() {
55
//// await "";
66
//// await 0;
7-
//// (await "").toLowerCase();
7+
//// (await foo()).toLowerCase();
88
//// (await 0).toFixed();
9-
//// (await 3.2).toFixed();
109
//// (await new C).foo();
11-
//// new (await getC()).foo.Class();
1210
////}
1311

1412
verify.codeFix({
1513
description: ts.Diagnostics.Remove_unnecessary_await.message,
1614
index: 0,
1715
newFileContent:
1816
`declare class C { foo(): void }
19-
declare function getC(): { foo: { Class: C } }
17+
declare function foo(): string;
2018
async function f() {
2119
"";
2220
await 0;
23-
(await "").toLowerCase();
21+
(await foo()).toLowerCase();
2422
(await 0).toFixed();
25-
(await 3.2).toFixed();
2623
(await new C).foo();
27-
new (await getC()).foo.Class();
2824
}`
2925
});
3026

@@ -33,14 +29,12 @@ verify.codeFixAll({
3329
fixId: "removeUnnecessaryAwait",
3430
newFileContent:
3531
`declare class C { foo(): void }
36-
declare function getC(): { foo: { Class: C } }
32+
declare function foo(): string;
3733
async function f() {
3834
"";
3935
0;
40-
"".toLowerCase();
36+
foo().toLowerCase();
4137
(0).toFixed();
42-
3.2.toFixed();
4338
(new C).foo();
44-
new (getC()).foo.Class();
4539
}`
4640
});

0 commit comments

Comments
 (0)