mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Improve accuracy of remove unnecessary await fix (#32384)
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user