Fix parenthesizeForAccess to always parenthesize NewExpressions and NumberLiterals

This commit is contained in:
Jason Freeman 2015-04-15 17:22:19 -07:00
parent 2cadea2137
commit 1cda3dc45d
7 changed files with 54 additions and 1 deletions

View File

@ -1607,7 +1607,15 @@ var __param = this.__param || function(index, decorator) { return function (targ
}
function parenthesizeForAccess(expr: Expression): LeftHandSideExpression {
if (isLeftHandSideExpression(expr)) {
// isLeftHandSideExpression is almost the correct criterion for when it is not necessary
// to parenthesize the expression before a dot. The known exceptions are:
//
// NewExpression:
// new C.x -> not the same as (new C).x
// NumberLiteral
// 1.x -> not the same as (1).x
//
if (isLeftHandSideExpression(expr) && expr.kind !== SyntaxKind.NewExpression && expr.kind !== SyntaxKind.NumericLiteral) {
return <LeftHandSideExpression>expr;
}
let node = <ParenthesizedExpression>createSynthesizedNode(SyntaxKind.ParenthesizedExpression);

View File

@ -0,0 +1,15 @@
//// [destructuringWithNewExpression.ts]
class C {
x = 0;
}
var { x } = new C;
//// [destructuringWithNewExpression.js]
var C = (function () {
function C() {
this.x = 0;
}
return C;
})();
var x = (new C).x;

View File

@ -0,0 +1,14 @@
=== tests/cases/compiler/destructuringWithNewExpression.ts ===
class C {
>C : C, Symbol(C, Decl(destructuringWithNewExpression.ts, 0, 0))
x = 0;
>x : number, Symbol(x, Decl(destructuringWithNewExpression.ts, 0, 9))
>0 : number
}
var { x } = new C;
>x : number, Symbol(x, Decl(destructuringWithNewExpression.ts, 4, 5))
>new C : C
>C : typeof C, Symbol(C, Decl(destructuringWithNewExpression.ts, 0, 0))

View File

@ -0,0 +1,5 @@
//// [destructuringWithNumberLiteral.ts]
var { toExponential } = 0;
//// [destructuringWithNumberLiteral.js]
var toExponential = (0).toExponential;

View File

@ -0,0 +1,5 @@
=== tests/cases/compiler/destructuringWithNumberLiteral.ts ===
var { toExponential } = 0;
>toExponential : (fractionDigits?: number) => string, Symbol(toExponential, Decl(destructuringWithNumberLiteral.ts, 0, 5))
>0 : number

View File

@ -0,0 +1,5 @@
class C {
x = 0;
}
var { x } = new C;

View File

@ -0,0 +1 @@
var { toExponential } = 0;