Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
enter(path) {
depth++;
const {node} = path;
if (node.async) {
const decls = [];
const addVarDecl = id => decls.push(variableDeclarator(id));
// hoist variables
hoistVariables(path, addVarDecl);
// info gathering for this/arguments during the refactoring
const thisID = identifier(path.scope.generateUid('this'));
const argumentsID = identifier(path.scope.generateUid('arguments'));
const used = {thisID: false, argumentsID: false};
const newBody = [];
const addFunctionDecl = func => newBody.push(func);
// refactor code
const args = {thisID, argumentsID, used, addVarDecl, addFunctionDecl, respID, errID};
path.traverse(RefactorVisitor, args);
// add this/arguments vars if necessary
if (used.thisID) {
decls.push(variableDeclarator(thisID, thisExpression()));
}
export default function (path, scope = path.scope) {
let { node } = path;
let 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 }));
let state = {
foundThis: false,
foundArguments: false
};
path.traverse(visitor, state);
if (state.foundArguments) {
callee = t.memberExpression(container, t.identifier("apply"));
args = [];
if (state.foundThis) {
args.push(t.thisExpression());
}