From 953dd1df48b3009e3ff63bdddcb049a51f81457c Mon Sep 17 00:00:00 2001 From: Boopathi Rajaa Date: Wed, 31 Aug 2016 23:01:59 +0200 Subject: [PATCH] Fix toExpression converting arrow functions to function expressions without block body (#3687) --- packages/babel-types/src/converters.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/packages/babel-types/src/converters.js b/packages/babel-types/src/converters.js index f3ce679d84..dcfc14d9f9 100644 --- a/packages/babel-types/src/converters.js +++ b/packages/babel-types/src/converters.js @@ -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 {