Skip to content

Commit c76f71c

Browse files
When emitting an arrow function, parenthesize the body if it could be interpreted as a block instead of an object literal.
1 parent 241cee0 commit c76f71c

13 files changed

+72
-2
lines changed

src/compiler/emitter.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4202,9 +4202,19 @@ module ts {
42024202
return;
42034203
}
42044204

4205-
// For es6 and higher we can emit the expression as is.
4205+
// For es6 and higher we can emit the expression as is. However, in the case
4206+
// where the expression might end up looking like a block when down-leveled, we'll
4207+
// also wrap it in parentheses first. For example if you have: a => <foo>{}
4208+
// then we need to generate: a => ({})
42064209
write(" ");
4207-
emit(body);
4210+
4211+
// Unwrap all type assertions.
4212+
var current = body;
4213+
while (current.kind === SyntaxKind.TypeAssertionExpression) {
4214+
current = (<TypeAssertion>current).expression;
4215+
}
4216+
4217+
emitParenthesizedIf(body, current.kind === SyntaxKind.ObjectLiteralExpression);
42084218
}
42094219

42104220
function emitDownLevelExpressionFunctionBody(node: FunctionLikeDeclaration, body: Expression) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//// [arrowFunctionWithObjectLiteralBody1.ts]
2+
var v = a => <any>{}
3+
4+
//// [arrowFunctionWithObjectLiteralBody1.js]
5+
var v = function (a) { return {}; };
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/arrowFunctionWithObjectLiteralBody1.ts ===
2+
var v = a => <any>{}
3+
>v : (a: any) => any
4+
>a => <any>{} : (a: any) => any
5+
>a : any
6+
><any>{} : any
7+
>{} : {}
8+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//// [arrowFunctionWithObjectLiteralBody2.ts]
2+
var v = a => <any><any>{}
3+
4+
//// [arrowFunctionWithObjectLiteralBody2.js]
5+
var v = function (a) { return {}; };
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
=== tests/cases/compiler/arrowFunctionWithObjectLiteralBody2.ts ===
2+
var v = a => <any><any>{}
3+
>v : (a: any) => any
4+
>a => <any><any>{} : (a: any) => any
5+
>a : any
6+
><any><any>{} : any
7+
><any>{} : any
8+
>{} : {}
9+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//// [arrowFunctionWithObjectLiteralBody3.ts]
2+
var v = a => <any>{}
3+
4+
//// [arrowFunctionWithObjectLiteralBody3.js]
5+
var v = a => ({});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/arrowFunctionWithObjectLiteralBody3.ts ===
2+
var v = a => <any>{}
3+
>v : (a: any) => any
4+
>a => <any>{} : (a: any) => any
5+
>a : any
6+
><any>{} : any
7+
>{} : {}
8+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//// [arrowFunctionWithObjectLiteralBody4.ts]
2+
var v = a => <any><any>{}
3+
4+
//// [arrowFunctionWithObjectLiteralBody4.js]
5+
var v = a => ({});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
=== tests/cases/compiler/arrowFunctionWithObjectLiteralBody4.ts ===
2+
var v = a => <any><any>{}
3+
>v : (a: any) => any
4+
>a => <any><any>{} : (a: any) => any
5+
>a : any
6+
><any><any>{} : any
7+
><any>{} : any
8+
>{} : {}
9+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var v = a => <any>{}

0 commit comments

Comments
 (0)