Skip to content

[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

Merged
merged 4 commits into from
Apr 5, 2017

Conversation

yuit
Copy link
Contributor

@yuit yuit commented Apr 4, 2017

Fix #14895

// 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);
Copy link
Member

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.

Copy link
Member

@rbuckton rbuckton left a 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.

@@ -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;
Copy link
Member

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.

@yuit yuit merged commit 8941e5f into master Apr 5, 2017
@yuit yuit deleted the master-14895 branch April 5, 2017 20:03
@microsoft microsoft locked and limited conversation to collaborators Jun 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants