When emitting an arrow function, parenthesize the body if it could be interpreted as a block instead of an object literal.

This commit is contained in:
Cyrus Najmabadi 2015-03-07 01:30:45 -08:00
parent 241cee0879
commit c76f71cfae
13 changed files with 72 additions and 2 deletions

View File

@ -4202,9 +4202,19 @@ module ts {
return;
}
// For es6 and higher we can emit the expression as is.
// For es6 and higher we can emit the expression as is. However, in the case
// where the expression might end up looking like a block when down-leveled, we'll
// also wrap it in parentheses first. For example if you have: a => <foo>{}
// then we need to generate: a => ({})
write(" ");
emit(body);
// Unwrap all type assertions.
var current = body;
while (current.kind === SyntaxKind.TypeAssertionExpression) {
current = (<TypeAssertion>current).expression;
}
emitParenthesizedIf(body, current.kind === SyntaxKind.ObjectLiteralExpression);
}
function emitDownLevelExpressionFunctionBody(node: FunctionLikeDeclaration, body: Expression) {

View File

@ -0,0 +1,5 @@
//// [arrowFunctionWithObjectLiteralBody1.ts]
var v = a => <any>{}
//// [arrowFunctionWithObjectLiteralBody1.js]
var v = function (a) { return {}; };

View File

@ -0,0 +1,8 @@
=== tests/cases/compiler/arrowFunctionWithObjectLiteralBody1.ts ===
var v = a => <any>{}
>v : (a: any) => any
>a => <any>{} : (a: any) => any
>a : any
><any>{} : any
>{} : {}

View File

@ -0,0 +1,5 @@
//// [arrowFunctionWithObjectLiteralBody2.ts]
var v = a => <any><any>{}
//// [arrowFunctionWithObjectLiteralBody2.js]
var v = function (a) { return {}; };

View File

@ -0,0 +1,9 @@
=== tests/cases/compiler/arrowFunctionWithObjectLiteralBody2.ts ===
var v = a => <any><any>{}
>v : (a: any) => any
>a => <any><any>{} : (a: any) => any
>a : any
><any><any>{} : any
><any>{} : any
>{} : {}

View File

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

View File

@ -0,0 +1,8 @@
=== tests/cases/compiler/arrowFunctionWithObjectLiteralBody3.ts ===
var v = a => <any>{}
>v : (a: any) => any
>a => <any>{} : (a: any) => any
>a : any
><any>{} : any
>{} : {}

View File

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

View File

@ -0,0 +1,9 @@
=== tests/cases/compiler/arrowFunctionWithObjectLiteralBody4.ts ===
var v = a => <any><any>{}
>v : (a: any) => any
>a => <any><any>{} : (a: any) => any
>a : any
><any><any>{} : any
><any>{} : any
>{} : {}

View File

@ -0,0 +1 @@
var v = a => <any>{}

View File

@ -0,0 +1 @@
var v = a => <any><any>{}

View File

@ -0,0 +1,2 @@
// @target: es6
var v = a => <any>{}

View File

@ -0,0 +1,2 @@
// @target: es6
var v = a => <any><any>{}