Skip to content

Commit b165b14

Browse files
committed
Merge pull request #3374 from Microsoft/emitParenthesizedTypeAssertion
Emit parenthesized type assertion
2 parents 1bb46e3 + e940fdc commit b165b14

23 files changed

+115
-2
lines changed

src/compiler/emitter.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,6 +1644,12 @@ 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+
while (expr.kind === SyntaxKind.TypeAssertionExpression) {
1650+
expr = (<TypeAssertion>expr).expression;
1651+
}
1652+
16471653
// isLeftHandSideExpression is almost the correct criterion for when it is not necessary
16481654
// to parenthesize the expression before a dot. The known exceptions are:
16491655
//
@@ -1652,7 +1658,10 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
16521658
// NumberLiteral
16531659
// 1.x -> not the same as (1).x
16541660
//
1655-
if (isLeftHandSideExpression(expr) && expr.kind !== SyntaxKind.NewExpression && expr.kind !== SyntaxKind.NumericLiteral) {
1661+
if (isLeftHandSideExpression(expr) &&
1662+
expr.kind !== SyntaxKind.NewExpression &&
1663+
expr.kind !== SyntaxKind.NumericLiteral) {
1664+
16561665
return <LeftHandSideExpression>expr;
16571666
}
16581667
let node = <ParenthesizedExpression>createSynthesizedNode(SyntaxKind.ParenthesizedExpression);
@@ -1906,7 +1915,10 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
19061915
}
19071916

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
tests/cases/conformance/es6/destructuring/destructuringTypeAssertionsES5_1.ts(1,18): error TS2304: Cannot find name 'foo'.
2+
3+
4+
==== tests/cases/conformance/es6/destructuring/destructuringTypeAssertionsES5_1.ts (1 errors) ====
5+
var { x } = <any>foo();
6+
~~~
7+
!!! error TS2304: Cannot find name 'foo'.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//// [destructuringTypeAssertionsES5_1.ts]
2+
var { x } = <any>foo();
3+
4+
//// [destructuringTypeAssertionsES5_1.js]
5+
var x = foo().x;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
tests/cases/conformance/es6/destructuring/destructuringTypeAssertionsES5_2.ts(1,19): error TS2304: Cannot find name 'foo'.
2+
3+
4+
==== tests/cases/conformance/es6/destructuring/destructuringTypeAssertionsES5_2.ts (1 errors) ====
5+
var { x } = (<any>foo());
6+
~~~
7+
!!! error TS2304: Cannot find name 'foo'.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//// [destructuringTypeAssertionsES5_2.ts]
2+
var { x } = (<any>foo());
3+
4+
//// [destructuringTypeAssertionsES5_2.js]
5+
var x = foo().x;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
tests/cases/conformance/es6/destructuring/destructuringTypeAssertionsES5_3.ts(1,19): error TS2304: Cannot find name 'foo'.
2+
3+
4+
==== tests/cases/conformance/es6/destructuring/destructuringTypeAssertionsES5_3.ts (1 errors) ====
5+
var { x } = <any>(foo());
6+
~~~
7+
!!! error TS2304: Cannot find name 'foo'.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//// [destructuringTypeAssertionsES5_3.ts]
2+
var { x } = <any>(foo());
3+
4+
//// [destructuringTypeAssertionsES5_3.js]
5+
var x = (foo()).x;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
tests/cases/conformance/es6/destructuring/destructuringTypeAssertionsES5_4.ts(1,23): error TS2304: Cannot find name 'foo'.
2+
3+
4+
==== tests/cases/conformance/es6/destructuring/destructuringTypeAssertionsES5_4.ts (1 errors) ====
5+
var { x } = <any><any>foo();
6+
~~~
7+
!!! error TS2304: Cannot find name 'foo'.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//// [destructuringTypeAssertionsES5_4.ts]
2+
var { x } = <any><any>foo();
3+
4+
//// [destructuringTypeAssertionsES5_4.js]
5+
var x = foo().x;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//// [destructuringTypeAssertionsES5_5.ts]
2+
var { x } = <any>0;
3+
4+
//// [destructuringTypeAssertionsES5_5.js]
5+
var x = (0).x;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/conformance/es6/destructuring/destructuringTypeAssertionsES5_5.ts ===
2+
var { x } = <any>0;
3+
>x : Symbol(x, Decl(destructuringTypeAssertionsES5_5.ts, 0, 5))
4+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
=== tests/cases/conformance/es6/destructuring/destructuringTypeAssertionsES5_5.ts ===
2+
var { x } = <any>0;
3+
>x : any
4+
><any>0 : any
5+
>0 : number
6+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
tests/cases/conformance/es6/destructuring/destructuringTypeAssertionsES5_6.ts(1,22): error TS2304: Cannot find name 'Foo'.
2+
3+
4+
==== tests/cases/conformance/es6/destructuring/destructuringTypeAssertionsES5_6.ts (1 errors) ====
5+
var { x } = <any>new Foo;
6+
~~~
7+
!!! error TS2304: Cannot find name 'Foo'.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//// [destructuringTypeAssertionsES5_6.ts]
2+
var { x } = <any>new Foo;
3+
4+
//// [destructuringTypeAssertionsES5_6.js]
5+
var x = (new Foo).x;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
tests/cases/conformance/es6/destructuring/destructuringTypeAssertionsES5_7.ts(1,27): error TS2304: Cannot find name 'Foo'.
2+
3+
4+
==== tests/cases/conformance/es6/destructuring/destructuringTypeAssertionsES5_7.ts (1 errors) ====
5+
var { x } = <any><any>new Foo;
6+
~~~
7+
!!! error TS2304: Cannot find name 'Foo'.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//// [destructuringTypeAssertionsES5_7.ts]
2+
var { x } = <any><any>new Foo;
3+
4+
//// [destructuringTypeAssertionsES5_7.js]
5+
var x = (new Foo).x;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
//@target: ES5
2+
var { x } = <any>foo();
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
//@target: ES5
2+
var { x } = (<any>foo());
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
//@target: ES5
2+
var { x } = <any>(foo());
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
//@target: ES5
2+
var { x } = <any><any>foo();
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
//@target: ES5
2+
var { x } = <any>0;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
//@target: ES5
2+
var { x } = <any>new Foo;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
//@target: ES5
2+
var { x } = <any><any>new Foo;

0 commit comments

Comments
 (0)