Skip to content

Commit 4ccf088

Browse files
committed
Don't try to strip parentheses when emitting synthesized parenthesized expressions
1 parent 1bb46e3 commit 4ccf088

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/compiler/emitter.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,6 +1644,13 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
16441644
}
16451645

16461646
function parenthesizeForAccess(expr: Expression): LeftHandSideExpression {
1647+
// When diagnosing whether the expression needs parentheses, the decision should be based
1648+
// on the innermost expression in a chain of nested type assertions.
1649+
let innerExpression = expr;
1650+
while (innerExpression.kind === SyntaxKind.TypeAssertionExpression) {
1651+
innerExpression = (<TypeAssertion>innerExpression).expression;
1652+
}
1653+
16471654
// isLeftHandSideExpression is almost the correct criterion for when it is not necessary
16481655
// to parenthesize the expression before a dot. The known exceptions are:
16491656
//
@@ -1652,7 +1659,10 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
16521659
// NumberLiteral
16531660
// 1.x -> not the same as (1).x
16541661
//
1655-
if (isLeftHandSideExpression(expr) && expr.kind !== SyntaxKind.NewExpression && expr.kind !== SyntaxKind.NumericLiteral) {
1662+
if (isLeftHandSideExpression(innerExpression) &&
1663+
innerExpression.kind !== SyntaxKind.NewExpression &&
1664+
innerExpression.kind !== SyntaxKind.NumericLiteral) {
1665+
16561666
return <LeftHandSideExpression>expr;
16571667
}
16581668
let node = <ParenthesizedExpression>createSynthesizedNode(SyntaxKind.ParenthesizedExpression);
@@ -1906,7 +1916,10 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
19061916
}
19071917

19081918
function emitParenExpression(node: ParenthesizedExpression) {
1909-
if (!node.parent || node.parent.kind !== SyntaxKind.ArrowFunction) {
1919+
// If the node is synthesized, it means the emitter put the parentheses there,
1920+
// not the user. If we didn't want them, the emitter would not have put them
1921+
// there.
1922+
if (!nodeIsSynthesized(node) && node.parent.kind !== SyntaxKind.ArrowFunction) {
19101923
if (node.expression.kind === SyntaxKind.TypeAssertionExpression) {
19111924
let operand = (<TypeAssertion>node.expression).expression;
19121925

0 commit comments

Comments
 (0)