Improve accuracy of remove unnecessary await fix (#32384)
This commit is contained in:
parent
17762c480d
commit
7cdfbceb43
3 changed files with 23 additions and 4 deletions
|
@ -4701,7 +4701,7 @@ namespace ts {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getLeftmostExpression(node: Expression, stopAtCallExpressions: boolean) {
|
export function getLeftmostExpression(node: Expression, stopAtCallExpressions: boolean) {
|
||||||
while (true) {
|
while (true) {
|
||||||
switch (node.kind) {
|
switch (node.kind) {
|
||||||
case SyntaxKind.PostfixUnaryExpression:
|
case SyntaxKind.PostfixUnaryExpression:
|
||||||
|
|
|
@ -26,8 +26,18 @@ namespace ts.codefix {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const parenthesizedExpression = tryCast(awaitExpression.parent, isParenthesizedExpression);
|
let expressionToReplace: Node = awaitExpression;
|
||||||
const removeParens = parenthesizedExpression && (isIdentifier(awaitExpression.expression) || isCallExpression(awaitExpression.expression));
|
const hasSurroundingParens = isParenthesizedExpression(awaitExpression.parent);
|
||||||
changeTracker.replaceNode(sourceFile, removeParens ? parenthesizedExpression || awaitExpression : awaitExpression, awaitExpression.expression);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
/// <reference path="fourslash.ts" />
|
/// <reference path="fourslash.ts" />
|
||||||
////declare class C { foo(): void }
|
////declare class C { foo(): void }
|
||||||
|
////declare function getC(): { Class: C };
|
||||||
////declare function foo(): string;
|
////declare function foo(): string;
|
||||||
////async function f() {
|
////async function f() {
|
||||||
//// await "";
|
//// await "";
|
||||||
|
@ -7,6 +8,8 @@
|
||||||
//// (await foo()).toLowerCase();
|
//// (await foo()).toLowerCase();
|
||||||
//// (await 0).toFixed();
|
//// (await 0).toFixed();
|
||||||
//// (await new C).foo();
|
//// (await new C).foo();
|
||||||
|
//// (await function() { }());
|
||||||
|
//// new (await getC()).Class();
|
||||||
////}
|
////}
|
||||||
|
|
||||||
verify.codeFix({
|
verify.codeFix({
|
||||||
|
@ -14,6 +17,7 @@ verify.codeFix({
|
||||||
index: 0,
|
index: 0,
|
||||||
newFileContent:
|
newFileContent:
|
||||||
`declare class C { foo(): void }
|
`declare class C { foo(): void }
|
||||||
|
declare function getC(): { Class: C };
|
||||||
declare function foo(): string;
|
declare function foo(): string;
|
||||||
async function f() {
|
async function f() {
|
||||||
"";
|
"";
|
||||||
|
@ -21,6 +25,8 @@ async function f() {
|
||||||
(await foo()).toLowerCase();
|
(await foo()).toLowerCase();
|
||||||
(await 0).toFixed();
|
(await 0).toFixed();
|
||||||
(await new C).foo();
|
(await new C).foo();
|
||||||
|
(await function() { }());
|
||||||
|
new (await getC()).Class();
|
||||||
}`
|
}`
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -29,6 +35,7 @@ verify.codeFixAll({
|
||||||
fixId: "removeUnnecessaryAwait",
|
fixId: "removeUnnecessaryAwait",
|
||||||
newFileContent:
|
newFileContent:
|
||||||
`declare class C { foo(): void }
|
`declare class C { foo(): void }
|
||||||
|
declare function getC(): { Class: C };
|
||||||
declare function foo(): string;
|
declare function foo(): string;
|
||||||
async function f() {
|
async function f() {
|
||||||
"";
|
"";
|
||||||
|
@ -36,5 +43,7 @@ async function f() {
|
||||||
foo().toLowerCase();
|
foo().toLowerCase();
|
||||||
(0).toFixed();
|
(0).toFixed();
|
||||||
(new C).foo();
|
(new C).foo();
|
||||||
|
(function() { } ());
|
||||||
|
new (getC()).Class();
|
||||||
}`
|
}`
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue