Amjad Masad 3667527d04 Revert "Remove flow"
This reverts commit 2827ff6b01dcce69e9d3c0402e96b52b3a2a47ee.
2016-03-03 14:49:20 -08:00

40 lines
857 B
JavaScript

export default function ({ types: t }) {
function statementList(key, path) {
let paths: Array = path.get(key);
for (let path of paths) {
let func = path.node;
if (!path.isFunctionDeclaration()) continue;
let declar = t.variableDeclaration("let", [
t.variableDeclarator(func.id, t.toExpression(func))
]);
// hoist it up above everything else
declar._blockHoist = 2;
// todo: name this
func.id = null;
path.replaceWith(declar);
}
}
return {
visitor: {
BlockStatement(path) {
let { node, parent } = path;
if (t.isFunction(parent, { body: node }) || t.isExportDeclaration(parent)) {
return;
}
statementList("body", path);
},
SwitchCase(path) {
statementList("consequent", path);
}
}
};
}