mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-05 16:38:05 -06:00
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:
parent
241cee0879
commit
c76f71cfae
@ -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) {
|
||||
|
||||
@ -0,0 +1,5 @@
|
||||
//// [arrowFunctionWithObjectLiteralBody1.ts]
|
||||
var v = a => <any>{}
|
||||
|
||||
//// [arrowFunctionWithObjectLiteralBody1.js]
|
||||
var v = function (a) { return {}; };
|
||||
@ -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
|
||||
>{} : {}
|
||||
|
||||
@ -0,0 +1,5 @@
|
||||
//// [arrowFunctionWithObjectLiteralBody2.ts]
|
||||
var v = a => <any><any>{}
|
||||
|
||||
//// [arrowFunctionWithObjectLiteralBody2.js]
|
||||
var v = function (a) { return {}; };
|
||||
@ -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
|
||||
>{} : {}
|
||||
|
||||
@ -0,0 +1,5 @@
|
||||
//// [arrowFunctionWithObjectLiteralBody3.ts]
|
||||
var v = a => <any>{}
|
||||
|
||||
//// [arrowFunctionWithObjectLiteralBody3.js]
|
||||
var v = a => ({});
|
||||
@ -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
|
||||
>{} : {}
|
||||
|
||||
@ -0,0 +1,5 @@
|
||||
//// [arrowFunctionWithObjectLiteralBody4.ts]
|
||||
var v = a => <any><any>{}
|
||||
|
||||
//// [arrowFunctionWithObjectLiteralBody4.js]
|
||||
var v = a => ({});
|
||||
@ -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
|
||||
>{} : {}
|
||||
|
||||
@ -0,0 +1 @@
|
||||
var v = a => <any>{}
|
||||
@ -0,0 +1 @@
|
||||
var v = a => <any><any>{}
|
||||
@ -0,0 +1,2 @@
|
||||
// @target: es6
|
||||
var v = a => <any>{}
|
||||
@ -0,0 +1,2 @@
|
||||
// @target: es6
|
||||
var v = a => <any><any>{}
|
||||
Loading…
x
Reference in New Issue
Block a user