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
));
},
},
};

View File

@@ -1,4 +1,4 @@
var _foo, _foo2, _foo$bar, _foo3, _foo4, _foo4$bar, _foo5;
var _foo, _foo2, _foo$bar, _foo3, _foo$bar2, _foo4, _foo5;
(_foo = foo) == null ? void 0 : _foo(foo);
@@ -6,4 +6,4 @@ var _foo, _foo2, _foo$bar, _foo3, _foo4, _foo4$bar, _foo5;
(_foo$bar = (_foo3 = foo).bar) == null ? void 0 : _foo$bar.call(_foo3, foo.bar, false);
(_foo4 = foo) == null ? void 0 : (_foo4$bar = (_foo5 = _foo4).bar) == null ? void 0 : _foo4$bar.call(_foo5, foo.bar, true);
(_foo5 = _foo4 = foo) == null ? void 0 : (_foo$bar2 = _foo5.bar) == null ? void 0 : _foo$bar2.call(_foo4, foo.bar, true);

View File

@@ -1,4 +1,4 @@
var _a, _a$b$c, _a$b, _a2, _a3, _a$b$c2, _a$b2, _a4, _b, _a5, _a5$b;
var _a, _a$b$c, _a$b, _a2, _a3, _a$b$c2, _a$b2, _a4, _b, _a$b3, _a5;
(_a = a) == null ? void 0 : new _a.b();
(_a2 = a) == null ? void 0 : (_a$b = _a2.b) == null ? void 0 : (_a$b$c = _a$b.c) == null ? void 0 : new _a$b$c.d();
@@ -7,4 +7,4 @@ var _a, _a$b$c, _a$b, _a2, _a3, _a$b$c2, _a$b2, _a4, _b, _a5, _a5$b;
(_a4 = a) == null ? void 0 : (_a$b2 = _a4.b) == null ? void 0 : (_a$b$c2 = _a$b2.c) == null ? void 0 : new _a$b$c2.d();
(_b = b) == null ? void 0 : new _b(b);
(_a5 = a) == null ? void 0 : (_a5$b = _a5.b) == null ? void 0 : new _a5$b(a.b, true);
(_a5 = a) == null ? void 0 : (_a$b3 = _a5.b) == null ? void 0 : new _a$b3(a.b, true);