Merge pull request #3374 from Microsoft/emitParenthesizedTypeAssertion

Emit parenthesized type assertion
This commit is contained in:
Jason Freeman
2015-06-03 17:55:45 -07:00
23 changed files with 115 additions and 2 deletions

View File

@@ -1644,6 +1644,12 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
}
function parenthesizeForAccess(expr: Expression): LeftHandSideExpression {
// When diagnosing whether the expression needs parentheses, the decision should be based
// on the innermost expression in a chain of nested type assertions.
while (expr.kind === SyntaxKind.TypeAssertionExpression) {
expr = (<TypeAssertion>expr).expression;
}
// isLeftHandSideExpression is almost the correct criterion for when it is not necessary
// to parenthesize the expression before a dot. The known exceptions are:
//
@@ -1652,7 +1658,10 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
// NumberLiteral
// 1.x -> not the same as (1).x
//
if (isLeftHandSideExpression(expr) && expr.kind !== SyntaxKind.NewExpression && expr.kind !== SyntaxKind.NumericLiteral) {
if (isLeftHandSideExpression(expr) &&
expr.kind !== SyntaxKind.NewExpression &&
expr.kind !== SyntaxKind.NumericLiteral) {
return <LeftHandSideExpression>expr;
}
let node = <ParenthesizedExpression>createSynthesizedNode(SyntaxKind.ParenthesizedExpression);
@@ -1906,7 +1915,10 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
}
function emitParenExpression(node: ParenthesizedExpression) {
if (!node.parent || node.parent.kind !== SyntaxKind.ArrowFunction) {
// If the node is synthesized, it means the emitter put the parentheses there,
// not the user. If we didn't want them, the emitter would not have put them
// there.
if (!nodeIsSynthesized(node) && node.parent.kind !== SyntaxKind.ArrowFunction) {
if (node.expression.kind === SyntaxKind.TypeAssertionExpression) {
let operand = (<TypeAssertion>node.expression).expression;

View File

@@ -0,0 +1,7 @@
tests/cases/conformance/es6/destructuring/destructuringTypeAssertionsES5_1.ts(1,18): error TS2304: Cannot find name 'foo'.
==== tests/cases/conformance/es6/destructuring/destructuringTypeAssertionsES5_1.ts (1 errors) ====
var { x } = <any>foo();
~~~
!!! error TS2304: Cannot find name 'foo'.

View File

@@ -0,0 +1,5 @@
//// [destructuringTypeAssertionsES5_1.ts]
var { x } = <any>foo();
//// [destructuringTypeAssertionsES5_1.js]
var x = foo().x;

View File

@@ -0,0 +1,7 @@
tests/cases/conformance/es6/destructuring/destructuringTypeAssertionsES5_2.ts(1,19): error TS2304: Cannot find name 'foo'.
==== tests/cases/conformance/es6/destructuring/destructuringTypeAssertionsES5_2.ts (1 errors) ====
var { x } = (<any>foo());
~~~
!!! error TS2304: Cannot find name 'foo'.

View File

@@ -0,0 +1,5 @@
//// [destructuringTypeAssertionsES5_2.ts]
var { x } = (<any>foo());
//// [destructuringTypeAssertionsES5_2.js]
var x = foo().x;

View File

@@ -0,0 +1,7 @@
tests/cases/conformance/es6/destructuring/destructuringTypeAssertionsES5_3.ts(1,19): error TS2304: Cannot find name 'foo'.
==== tests/cases/conformance/es6/destructuring/destructuringTypeAssertionsES5_3.ts (1 errors) ====
var { x } = <any>(foo());
~~~
!!! error TS2304: Cannot find name 'foo'.

View File

@@ -0,0 +1,5 @@
//// [destructuringTypeAssertionsES5_3.ts]
var { x } = <any>(foo());
//// [destructuringTypeAssertionsES5_3.js]
var x = (foo()).x;

View File

@@ -0,0 +1,7 @@
tests/cases/conformance/es6/destructuring/destructuringTypeAssertionsES5_4.ts(1,23): error TS2304: Cannot find name 'foo'.
==== tests/cases/conformance/es6/destructuring/destructuringTypeAssertionsES5_4.ts (1 errors) ====
var { x } = <any><any>foo();
~~~
!!! error TS2304: Cannot find name 'foo'.

View File

@@ -0,0 +1,5 @@
//// [destructuringTypeAssertionsES5_4.ts]
var { x } = <any><any>foo();
//// [destructuringTypeAssertionsES5_4.js]
var x = foo().x;

View File

@@ -0,0 +1,5 @@
//// [destructuringTypeAssertionsES5_5.ts]
var { x } = <any>0;
//// [destructuringTypeAssertionsES5_5.js]
var x = (0).x;

View File

@@ -0,0 +1,4 @@
=== tests/cases/conformance/es6/destructuring/destructuringTypeAssertionsES5_5.ts ===
var { x } = <any>0;
>x : Symbol(x, Decl(destructuringTypeAssertionsES5_5.ts, 0, 5))

View File

@@ -0,0 +1,6 @@
=== tests/cases/conformance/es6/destructuring/destructuringTypeAssertionsES5_5.ts ===
var { x } = <any>0;
>x : any
><any>0 : any
>0 : number

View File

@@ -0,0 +1,7 @@
tests/cases/conformance/es6/destructuring/destructuringTypeAssertionsES5_6.ts(1,22): error TS2304: Cannot find name 'Foo'.
==== tests/cases/conformance/es6/destructuring/destructuringTypeAssertionsES5_6.ts (1 errors) ====
var { x } = <any>new Foo;
~~~
!!! error TS2304: Cannot find name 'Foo'.

View File

@@ -0,0 +1,5 @@
//// [destructuringTypeAssertionsES5_6.ts]
var { x } = <any>new Foo;
//// [destructuringTypeAssertionsES5_6.js]
var x = (new Foo).x;

View File

@@ -0,0 +1,7 @@
tests/cases/conformance/es6/destructuring/destructuringTypeAssertionsES5_7.ts(1,27): error TS2304: Cannot find name 'Foo'.
==== tests/cases/conformance/es6/destructuring/destructuringTypeAssertionsES5_7.ts (1 errors) ====
var { x } = <any><any>new Foo;
~~~
!!! error TS2304: Cannot find name 'Foo'.

View File

@@ -0,0 +1,5 @@
//// [destructuringTypeAssertionsES5_7.ts]
var { x } = <any><any>new Foo;
//// [destructuringTypeAssertionsES5_7.js]
var x = (new Foo).x;

View File

@@ -0,0 +1,2 @@
//@target: ES5
var { x } = <any>foo();

View File

@@ -0,0 +1,2 @@
//@target: ES5
var { x } = (<any>foo());

View File

@@ -0,0 +1,2 @@
//@target: ES5
var { x } = <any>(foo());

View File

@@ -0,0 +1,2 @@
//@target: ES5
var { x } = <any><any>foo();

View File

@@ -0,0 +1,2 @@
//@target: ES5
var { x } = <any>0;

View File

@@ -0,0 +1,2 @@
//@target: ES5
var { x } = <any>new Foo;

View File

@@ -0,0 +1,2 @@
//@target: ES5
var { x } = <any><any>new Foo;