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; 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)) { if (t.isClass(node)) {
node.type = "ClassExpression"; node.type = "ClassExpression";
} else if (t.isFunction(node)) { } else if (t.isFunction(node)) {
node.type = "FunctionExpression"; node.type = "FunctionExpression";
} }
if (t.isExpression(node)) { // if it's still not an expression
return node; if (!t.isExpression(node)) {
} else {
throw new Error(`cannot turn ${node.type} to an expression`); throw new Error(`cannot turn ${node.type} to an expression`);
} }
return node;
} }
export function toBlock(node: Object, parent: Object): Object { export function toBlock(node: Object, parent: Object): Object {