From 5fe48035620b74db4434a5c5953ea9952c33048c Mon Sep 17 00:00:00 2001 From: Justin Ridgewell Date: Sat, 3 Jun 2017 02:39:38 -0400 Subject: [PATCH] Simplify NewExpression|CallExpression visitor --- .../src/index.js | 46 +++++++++---------- .../general/function-call/expected.js | 4 +- .../test/fixtures/general/new/expected.js | 4 +- 3 files changed, 26 insertions(+), 28 deletions(-) diff --git a/packages/babel-plugin-transform-optional-chaining/src/index.js b/packages/babel-plugin-transform-optional-chaining/src/index.js index 95a565de82..49ff4c81e7 100644 --- a/packages/babel-plugin-transform-optional-chaining/src/index.js +++ b/packages/babel-plugin-transform-optional-chaining/src/index.js @@ -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 + )); }, }, }; diff --git a/packages/babel-plugin-transform-optional-chaining/test/fixtures/general/function-call/expected.js b/packages/babel-plugin-transform-optional-chaining/test/fixtures/general/function-call/expected.js index 1df03edc8c..0b6f985356 100644 --- a/packages/babel-plugin-transform-optional-chaining/test/fixtures/general/function-call/expected.js +++ b/packages/babel-plugin-transform-optional-chaining/test/fixtures/general/function-call/expected.js @@ -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); \ No newline at end of file +(_foo5 = _foo4 = foo) == null ? void 0 : (_foo$bar2 = _foo5.bar) == null ? void 0 : _foo$bar2.call(_foo4, foo.bar, true); \ No newline at end of file diff --git a/packages/babel-plugin-transform-optional-chaining/test/fixtures/general/new/expected.js b/packages/babel-plugin-transform-optional-chaining/test/fixtures/general/new/expected.js index 1d6d1ad2d5..cf631ecfd0 100644 --- a/packages/babel-plugin-transform-optional-chaining/test/fixtures/general/new/expected.js +++ b/packages/babel-plugin-transform-optional-chaining/test/fixtures/general/new/expected.js @@ -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); \ No newline at end of file +(_a5 = a) == null ? void 0 : (_a$b3 = _a5.b) == null ? void 0 : new _a$b3(a.b, true); \ No newline at end of file