@@ -1644,6 +1644,13 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
1644
1644
}
1645
1645
1646
1646
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
+
1647
1654
// isLeftHandSideExpression is almost the correct criterion for when it is not necessary
1648
1655
// to parenthesize the expression before a dot. The known exceptions are:
1649
1656
//
@@ -1652,7 +1659,10 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
1652
1659
// NumberLiteral
1653
1660
// 1.x -> not the same as (1).x
1654
1661
//
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
+
1656
1666
return < LeftHandSideExpression > expr ;
1657
1667
}
1658
1668
let node = < ParenthesizedExpression > createSynthesizedNode ( SyntaxKind . ParenthesizedExpression ) ;
@@ -1906,7 +1916,10 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
1906
1916
}
1907
1917
1908
1918
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 ) {
1910
1923
if ( node . expression . kind === SyntaxKind . TypeAssertionExpression ) {
1911
1924
let operand = ( < TypeAssertion > node . expression ) . expression ;
1912
1925
0 commit comments