Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export default function(path: NodePath, scope = path.scope) {
const { node } = path;
const container = t.functionExpression(
null,
[],
node.body,
node.generator,
node.async,
);
let callee = container;
let args = [];
// todo: only hoist if necessary
hoistVariables(path, id => scope.push({ id }));
const state = {
foundThis: false,
foundArguments: false,
};
path.traverse(visitor, state);
if (state.foundArguments || state.foundThis) {
callee = t.memberExpression(container, t.identifier("apply"));
args = [];
if (state.foundThis) {
args.push(t.thisExpression());
}
}
sources.push(t.stringLiteral(specifiers.key));
setters.push(
t.functionExpression(
null,
[t.identifier(target)],
t.blockStatement(setterBody),
),
);
});
let moduleName = this.getModuleName();
if (moduleName) moduleName = t.stringLiteral(moduleName);
hoistVariables(
path,
(id, name, hasInit) => {
variableIds.push(id);
if (!hasInit) {
exportNames.push(name);
exportValues.push(undefinedIdent);
}
},
null,
);
if (variableIds.length) {
beforeBody.unshift(
t.variableDeclaration(
"var",
variableIds.map(id => t.variableDeclarator(id)),
sources.push(t.stringLiteral(specifiers.key));
setters.push(
t.functionExpression(
null,
[target],
t.blockStatement(setterBody),
),
);
});
let moduleName = this.getModuleName();
if (moduleName) moduleName = t.stringLiteral(moduleName);
if (canHoist) {
hoistVariables(path, id => variableIds.push(id));
}
if (variableIds.length) {
beforeBody.unshift(
t.variableDeclaration(
"var",
variableIds.map(id => t.variableDeclarator(id)),
),
);
}
path.traverse(reassignmentVisitor, {
exports: exportNames,
buildCall: buildExportCall,
scope: path.scope,
});