From 4ccf088734ad929f347a29ad0962830116ca575f Mon Sep 17 00:00:00 2001 From: Jason Freeman Date: Wed, 3 Jun 2015 17:22:11 -0700 Subject: [PATCH] Don't try to strip parentheses when emitting synthesized parenthesized expressions --- src/compiler/emitter.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 028a65ddfa1..5a0db87d409 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -1644,6 +1644,13 @@ var __param = (this && this.__param) || function (paramIndex, decorator) { } function parenthesizeForAccess(expr: Expression): LeftHandSideExpression { + // When diagnosing whether the expression needs parentheses, the decision should be based + // on the innermost expression in a chain of nested type assertions. + let innerExpression = expr; + while (innerExpression.kind === SyntaxKind.TypeAssertionExpression) { + innerExpression = (innerExpression).expression; + } + // isLeftHandSideExpression is almost the correct criterion for when it is not necessary // to parenthesize the expression before a dot. The known exceptions are: // @@ -1652,7 +1659,10 @@ var __param = (this && this.__param) || function (paramIndex, decorator) { // NumberLiteral // 1.x -> not the same as (1).x // - if (isLeftHandSideExpression(expr) && expr.kind !== SyntaxKind.NewExpression && expr.kind !== SyntaxKind.NumericLiteral) { + if (isLeftHandSideExpression(innerExpression) && + innerExpression.kind !== SyntaxKind.NewExpression && + innerExpression.kind !== SyntaxKind.NumericLiteral) { + return expr; } let node = createSynthesizedNode(SyntaxKind.ParenthesizedExpression); @@ -1906,7 +1916,10 @@ var __param = (this && this.__param) || function (paramIndex, decorator) { } function emitParenExpression(node: ParenthesizedExpression) { - if (!node.parent || node.parent.kind !== SyntaxKind.ArrowFunction) { + // If the node is synthesized, it means the emitter put the parentheses there, + // not the user. If we didn't want them, the emitter would not have put them + // there. + if (!nodeIsSynthesized(node) && node.parent.kind !== SyntaxKind.ArrowFunction) { if (node.expression.kind === SyntaxKind.TypeAssertionExpression) { let operand = (node.expression).expression;