mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-24 04:30:53 -06:00
Fix parenthesizeForAccess to always parenthesize NewExpressions and NumberLiterals
This commit is contained in:
parent
2cadea2137
commit
1cda3dc45d
@ -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);
|
||||
|
||||
15
tests/baselines/reference/destructuringWithNewExpression.js
Normal file
15
tests/baselines/reference/destructuringWithNewExpression.js
Normal 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;
|
||||
@ -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))
|
||||
|
||||
@ -0,0 +1,5 @@
|
||||
//// [destructuringWithNumberLiteral.ts]
|
||||
var { toExponential } = 0;
|
||||
|
||||
//// [destructuringWithNumberLiteral.js]
|
||||
var toExponential = (0).toExponential;
|
||||
@ -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
|
||||
|
||||
5
tests/cases/compiler/destructuringWithNewExpression.ts
Normal file
5
tests/cases/compiler/destructuringWithNewExpression.ts
Normal file
@ -0,0 +1,5 @@
|
||||
class C {
|
||||
x = 0;
|
||||
}
|
||||
|
||||
var { x } = new C;
|
||||
1
tests/cases/compiler/destructuringWithNumberLiteral.ts
Normal file
1
tests/cases/compiler/destructuringWithNumberLiteral.ts
Normal file
@ -0,0 +1 @@
|
||||
var { toExponential } = 0;
|
||||
Loading…
x
Reference in New Issue
Block a user