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