Skip to content

Commit 1cda3dc

Browse files
committed
Fix parenthesizeForAccess to always parenthesize NewExpressions and NumberLiterals
1 parent 2cadea2 commit 1cda3dc

File tree

7 files changed

+54
-1
lines changed

7 files changed

+54
-1
lines changed

src/compiler/emitter.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1607,7 +1607,15 @@ var __param = this.__param || function(index, decorator) { return function (targ
16071607
}
16081608

16091609
function parenthesizeForAccess(expr: Expression): LeftHandSideExpression {
1610-
if (isLeftHandSideExpression(expr)) {
1610+
// isLeftHandSideExpression is almost the correct criterion for when it is not necessary
1611+
// to parenthesize the expression before a dot. The known exceptions are:
1612+
//
1613+
// NewExpression:
1614+
// new C.x -> not the same as (new C).x
1615+
// NumberLiteral
1616+
// 1.x -> not the same as (1).x
1617+
//
1618+
if (isLeftHandSideExpression(expr) && expr.kind !== SyntaxKind.NewExpression && expr.kind !== SyntaxKind.NumericLiteral) {
16111619
return <LeftHandSideExpression>expr;
16121620
}
16131621
let node = <ParenthesizedExpression>createSynthesizedNode(SyntaxKind.ParenthesizedExpression);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [destructuringWithNewExpression.ts]
2+
class C {
3+
x = 0;
4+
}
5+
6+
var { x } = new C;
7+
8+
//// [destructuringWithNewExpression.js]
9+
var C = (function () {
10+
function C() {
11+
this.x = 0;
12+
}
13+
return C;
14+
})();
15+
var x = (new C).x;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
=== tests/cases/compiler/destructuringWithNewExpression.ts ===
2+
class C {
3+
>C : C, Symbol(C, Decl(destructuringWithNewExpression.ts, 0, 0))
4+
5+
x = 0;
6+
>x : number, Symbol(x, Decl(destructuringWithNewExpression.ts, 0, 9))
7+
>0 : number
8+
}
9+
10+
var { x } = new C;
11+
>x : number, Symbol(x, Decl(destructuringWithNewExpression.ts, 4, 5))
12+
>new C : C
13+
>C : typeof C, Symbol(C, Decl(destructuringWithNewExpression.ts, 0, 0))
14+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//// [destructuringWithNumberLiteral.ts]
2+
var { toExponential } = 0;
3+
4+
//// [destructuringWithNumberLiteral.js]
5+
var toExponential = (0).toExponential;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=== tests/cases/compiler/destructuringWithNumberLiteral.ts ===
2+
var { toExponential } = 0;
3+
>toExponential : (fractionDigits?: number) => string, Symbol(toExponential, Decl(destructuringWithNumberLiteral.ts, 0, 5))
4+
>0 : number
5+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class C {
2+
x = 0;
3+
}
4+
5+
var { x } = new C;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var { toExponential } = 0;

0 commit comments

Comments
 (0)