Simplify NewExpression|CallExpression visitor

This commit is contained in:
Justin Ridgewell
2017-06-03 02:39:38 -04:00
parent 30ee87159d
commit 5fe4803562
3 changed files with 26 additions and 28 deletions

View File

@@ -34,36 +34,34 @@ export default function ({ types: t }) {
));
},
"NewExpression|CallExpression": {
exit(path) {
if (!path.node.optional) {
return;
}
"NewExpression|CallExpression"(path) {
if (!path.node.optional) {
return;
}
const { scope, node } = path;
const { callee } = node;
const { scope, node } = path;
const { callee } = node;
node.optional = false;
node.optional = false;
const ref = scope.generateUidIdentifierBasedOnNode(callee);
scope.push({ id: ref });
node.callee = ref;
const ref = scope.generateUidIdentifierBasedOnNode(callee);
scope.push({ id: ref });
node.callee = ref;
if (t.isMemberExpression(callee) && !t.isNewExpression(node)) {
const context = scope.generateUidIdentifierBasedOnNode(callee.object);
scope.push({ id: context });
callee.object = t.assignmentExpression("=", context, callee.object);
if (t.isMemberExpression(callee) && !t.isNewExpression(node)) {
const context = scope.generateUidIdentifierBasedOnNode(callee.object);
scope.push({ id: context });
callee.object = t.assignmentExpression("=", context, callee.object);
node.arguments.unshift(context);
node.callee = t.memberExpression(node.callee, t.identifier("call"));
}
node.arguments.unshift(context);
node.callee = t.memberExpression(node.callee, t.identifier("call"));
}
path.replaceWith(t.conditionalExpression(
t.binaryExpression("==", t.assignmentExpression("=", ref, callee), t.nullLiteral()),
scope.buildUndefinedNode(),
node
));
},
path.replaceWith(t.conditionalExpression(
t.binaryExpression("==", t.assignmentExpression("=", ref, callee), t.nullLiteral()),
scope.buildUndefinedNode(),
node
));
},
},
};