Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7cdfbce

Browse filesBrowse files
authored
Improve accuracy of remove unnecessary await fix (microsoft#32384)
1 parent 17762c4 commit 7cdfbce
Copy full SHA for 7cdfbce

3 files changed

+23-4Lines changed: 23 additions & 4 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/compiler/factory.ts‎

Copy file name to clipboardExpand all lines: src/compiler/factory.ts
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4701,7 +4701,7 @@ namespace ts {
47014701
}
47024702
}
47034703

4704-
function getLeftmostExpression(node: Expression, stopAtCallExpressions: boolean) {
4704+
export function getLeftmostExpression(node: Expression, stopAtCallExpressions: boolean) {
47054705
while (true) {
47064706
switch (node.kind) {
47074707
case SyntaxKind.PostfixUnaryExpression:
Collapse file

‎src/services/codefixes/removeUnnecessaryAwait.ts‎

Copy file name to clipboardExpand all lines: src/services/codefixes/removeUnnecessaryAwait.ts
+13-3Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,18 @@ namespace ts.codefix {
2626
return;
2727
}
2828

29-
const parenthesizedExpression = tryCast(awaitExpression.parent, isParenthesizedExpression);
30-
const removeParens = parenthesizedExpression && (isIdentifier(awaitExpression.expression) || isCallExpression(awaitExpression.expression));
31-
changeTracker.replaceNode(sourceFile, removeParens ? parenthesizedExpression || awaitExpression : awaitExpression, awaitExpression.expression);
29+
let expressionToReplace: Node = awaitExpression;
30+
const hasSurroundingParens = isParenthesizedExpression(awaitExpression.parent);
31+
if (hasSurroundingParens) {
32+
const leftMostExpression = getLeftmostExpression(awaitExpression.expression, /*stopAtCallExpressions*/ false);
33+
if (isIdentifier(leftMostExpression)) {
34+
const precedingToken = findPrecedingToken(awaitExpression.parent.pos, sourceFile);
35+
if (precedingToken && precedingToken.kind !== SyntaxKind.NewKeyword) {
36+
expressionToReplace = awaitExpression.parent;
37+
}
38+
}
39+
}
40+
41+
changeTracker.replaceNode(sourceFile, expressionToReplace, awaitExpression.expression);
3242
}
3343
}
Collapse file

‎tests/cases/fourslash/codeFixRemoveUnnecessaryAwait.ts‎

Copy file name to clipboardExpand all lines: tests/cases/fourslash/codeFixRemoveUnnecessaryAwait.ts
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
/// <reference path="fourslash.ts" />
22
////declare class C { foo(): void }
3+
////declare function getC(): { Class: C };
34
////declare function foo(): string;
45
////async function f() {
56
//// await "";
67
//// await 0;
78
//// (await foo()).toLowerCase();
89
//// (await 0).toFixed();
910
//// (await new C).foo();
11+
//// (await function() { }());
12+
//// new (await getC()).Class();
1013
////}
1114

1215
verify.codeFix({
1316
description: ts.Diagnostics.Remove_unnecessary_await.message,
1417
index: 0,
1518
newFileContent:
1619
`declare class C { foo(): void }
20+
declare function getC(): { Class: C };
1721
declare function foo(): string;
1822
async function f() {
1923
"";
2024
await 0;
2125
(await foo()).toLowerCase();
2226
(await 0).toFixed();
2327
(await new C).foo();
28+
(await function() { }());
29+
new (await getC()).Class();
2430
}`
2531
});
2632

@@ -29,12 +35,15 @@ verify.codeFixAll({
2935
fixId: "removeUnnecessaryAwait",
3036
newFileContent:
3137
`declare class C { foo(): void }
38+
declare function getC(): { Class: C };
3239
declare function foo(): string;
3340
async function f() {
3441
"";
3542
0;
3643
foo().toLowerCase();
3744
(0).toFixed();
3845
(new C).foo();
46+
(function() { } ());
47+
new (getC()).Class();
3948
}`
4049
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.