extract-hoc
Version:
A Babel plugin that enables react-hot-loader to work on HoCs.
95 lines (76 loc) • 2.04 kB
JavaScript
function extractArguments(_ref, node) {
var types = _ref.types,
path = _ref.path,
forDefaultExport = _ref.forDefaultExport;
if (node.type !== 'CallExpression') {
return;
}
node.arguments = node.arguments.map(function (node) {
if (types.isCallExpression(node)) {
extractArguments({ types: types, path: path, forDefaultExport: forDefaultExport }, node);
}
if (types.isIdentifier(node)) {
return node;
}
if (types.isLiteral(node)) {
return node;
}
if (types.isSpreadElement(node)) {
return node;
}
var idName = forDefaultExport ? 'default' : path.node.id.name;
var id = path.scope.generateUidIdentifier(idName + '_arg');
path.insertBefore(types.assignmentExpression('=', id, node));
path.scope.push({ id: id });
return id;
});
}
function pathToInsertAssignments(path) {
while (path.parent.type !== 'Program') {
path = path.parentPath;
}
return path;
}
module.exports = function (_ref2) {
var types = _ref2.types;
// No-op in production.
if (process.env.NODE_ENV === 'production') {
return {
visitor: {}
};
}
var Visitor = {
VariableDeclarator: function VariableDeclarator(path) {
if (path.parentPath.parent.type !== 'Program' && path.parentPath.parent.type !== 'ExportNamedDeclaration') {
return;
}
if (!path.node.init) {
return;
}
if (path.node.init.type !== 'CallExpression') {
return;
}
extractArguments({ types: types, path: path }, path.node.init);
},
ExportDefaultDeclaration: function ExportDefaultDeclaration(path) {
if (path.node.declaration.type !== 'CallExpression') {
return;
}
extractArguments({
types: types,
path: path,
forDefaultExport: true
}, path.node.declaration);
}
};
return {
visitor: {
Program: {
enter: function enter(path) {
path.traverse(Visitor);
}
}
}
};
};
;