-
Notifications
You must be signed in to change notification settings - Fork 12.8k
[Master] Emit parenthesis around propert/element access expression of casted object literal expression #15006
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…bject literal expression
src/compiler/transformers/ts.ts
Outdated
// However, auto-parenthesization will not preserve parenthesis for the following case: ({ "1": "one", "2": "two" } as { [key: string]: string })[x]. | ||
// so we have to manually preserve it here. | ||
const shouldPreserveParen = (isPropertyAccessExpression(node.parent) || isElementAccessExpression(node.parent)) && | ||
isObjectLiteralExpression((expression as PartiallyEmittedExpression).expression); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not the correct place to make a change. If we were to take this change, another feature at a later date could run into the same issue. Instead, we should make this change in the parenthesization logic in factory.ts
.
If you look at parenthesizeExpressionForExpressionStatement
, we check the kind of the left-most expression. However, in parenthesizeConciseBody
we are not performing the same check.
I would recommend we modify parenthesizeConciseBody
to call getLeftmostExpression
instead of skipParenthesizedExpressions
. That should correct this issue in a single place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor nit, but this looks good.
src/compiler/factory.ts
Outdated
@@ -3492,7 +3492,8 @@ namespace ts { | |||
|
|||
export function parenthesizeConciseBody(body: ConciseBody): ConciseBody { | |||
const emittedBody = skipPartiallyEmittedExpressions(body); | |||
if (emittedBody.kind === SyntaxKind.ObjectLiteralExpression) { | |||
const leftMostExpression = isExpression(emittedBody) ? getLeftmostExpression(emittedBody) : undefined; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this could just be:
if (!isBlock(body) && getLeftmostExpression(body).kind === SyntaxKind.ObjectLiteralExpresion)
I suggest using !isBlock(body)
here as there are fewer comparisons necessary than a full isExpression
test.
Fix #14895