Fix toExpression converting arrow functions to function expressions without block body (#3687)

This commit is contained in:
Boopathi Rajaa 2016-08-31 23:01:59 +02:00 committed by Henry Zhu
parent 174e44f656
commit 953dd1df48

View File

@ -204,17 +204,31 @@ export function toExpression(node: Object): Object {
node = node.expression;
}
// return unmodified node
// important for things like ArrowFunctions where
// type change from ArrowFunction to FunctionExpression
// produces bugs like -> `()=>a` to `function () a`
// without generating a BlockStatement for it
// ref: https://github.com/babel/babili/issues/130
if (t.isExpression(node)) {
return node;
}
// convert all classes and functions
// ClassDeclaration -> ClassExpression
// FunctionDeclaration, ObjectMethod, ClassMethod -> FunctionExpression
if (t.isClass(node)) {
node.type = "ClassExpression";
} else if (t.isFunction(node)) {
node.type = "FunctionExpression";
}
if (t.isExpression(node)) {
return node;
} else {
// if it's still not an expression
if (!t.isExpression(node)) {
throw new Error(`cannot turn ${node.type} to an expression`);
}
return node;
}
export function toBlock(node: Object, parent: Object): Object {