From 462825b15af0ec844df28cc77b1598591e825661 Mon Sep 17 00:00:00 2001 From: Justin Ridgewell Date: Wed, 7 Jun 2017 02:56:20 -0400 Subject: [PATCH] Simplify transform MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This also makes the output use less comparisons for `foo?.()?.bar` cases. 😁 --- .../src/index.js | 34 +++++++++---------- .../general/function-call/expected.js | 8 ++--- .../test/fixtures/general/new/expected.js | 8 ++--- 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/packages/babel-plugin-transform-optional-chaining/src/index.js b/packages/babel-plugin-transform-optional-chaining/src/index.js index 86b6b0d7ca..a3db104e61 100644 --- a/packages/babel-plugin-transform-optional-chaining/src/index.js +++ b/packages/babel-plugin-transform-optional-chaining/src/index.js @@ -1,30 +1,36 @@ import syntaxOptionalChaining from "babel-plugin-syntax-optional-chaining"; export default function ({ types: t }) { - function optional(path, key, replacementPath, needsContext = false, loose = false) { + function optional(path, replacementPath, loose = false) { const { scope } = path; - const optionals = [path.node]; + const optionals = []; const nil = scope.buildUndefinedNode(); - for (let objectPath = path.get(key); objectPath.isMemberExpression(); objectPath = objectPath.get("object")) { + let objectPath = path; + while (objectPath.isMemberExpression() || objectPath.isCallExpression() || objectPath.isNewExpression()) { const { node } = objectPath; if (node.optional) { optionals.push(node); } + + if (objectPath.isMemberExpression()) { + objectPath = objectPath.get("object"); + } else { + objectPath = objectPath.get("callee"); + } } for (let i = optionals.length - 1; i >= 0; i--) { const node = optionals[i]; node.optional = false; - const replaceKey = i == 0 ? key : "object"; - const atCall = needsContext && i == 0; - + const isCall = t.isCallExpression(node); + const replaceKey = isCall || t.isNewExpression(node) ? "callee" : "object"; const chain = node[replaceKey]; let ref; let check; - if (loose && atCall) { + if (loose && isCall) { // If we are using a loose transform (avoiding a Function#call) and we are at the call, // we can avoid a needless memoize. check = ref = chain; @@ -40,7 +46,7 @@ export default function ({ types: t }) { // Ensure call expressions have the proper `this` // `foo.bar()` has context `foo`. - if (atCall && t.isMemberExpression(chain)) { + if (isCall && t.isMemberExpression(chain)) { if (loose) { // To avoid a Function#call, we can instead re-grab the property from the context object. // `a.?b.?()` translates roughly to `_a.b != null && _a.b()` @@ -99,20 +105,12 @@ export default function ({ types: t }) { inherits: syntaxOptionalChaining, visitor: { - MemberExpression(path) { + "MemberExpression|NewExpression|CallExpression"(path) { if (!path.node.optional) { return; } - optional(path, "object", findReplacementPath(path)); - }, - - "NewExpression|CallExpression"(path) { - if (!path.node.optional) { - return; - } - - optional(path, "callee", findReplacementPath(path), path.isCallExpression(), this.opts.loose); + optional(path, findReplacementPath(path), this.opts.loose); }, }, }; 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 dcf4091343..297241cb9f 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, _foo6, _foo7, _foo$bar2, _foo8, _foo$bar3, _foo$bar4, _foo9, _foo10, _foo10$bar, _foo$bar5, _foo11, _foo11$bar; +var _foo, _foo2, _foo$bar, _foo3, _foo4, _foo4$bar, _foo5, _foo6, _foo7, _foo$bar2, _foo8, _foo$bar3, _foo9, _foo$bar3$call, _foo10, _foo10$bar, _foo11, _foo11$bar, _foo11$bar$call; (_foo = foo) == null ? void 0 : _foo(foo); @@ -10,12 +10,12 @@ var _foo, _foo2, _foo$bar, _foo3, _foo4, _foo4$bar, _foo5, _foo6, _foo7, _foo$ba (_foo5 = foo) == null ? void 0 : _foo5().bar; -(_foo6 = (_foo7 = foo) == null ? void 0 : _foo7()) == null ? void 0 : _foo6.bar; +(_foo6 = foo) == null ? void 0 : (_foo7 = _foo6()) == null ? void 0 : _foo7.bar; (_foo$bar2 = (_foo8 = foo).bar) == null ? void 0 : _foo$bar2.call(_foo8).baz; -(_foo$bar3 = (_foo$bar4 = (_foo9 = foo).bar) == null ? void 0 : _foo$bar4.call(_foo9)) == null ? void 0 : _foo$bar3.baz; +(_foo$bar3 = (_foo9 = foo).bar) == null ? void 0 : (_foo$bar3$call = _foo$bar3.call(_foo9)) == null ? void 0 : _foo$bar3$call.baz; (_foo10 = foo) == null ? void 0 : (_foo10$bar = _foo10.bar) == null ? void 0 : _foo10$bar.call(_foo10).baz; -(_foo$bar5 = (_foo11 = foo) == null ? void 0 : (_foo11$bar = _foo11.bar) == null ? void 0 : _foo11$bar.call(_foo11)) == null ? void 0 : _foo$bar5.baz; \ No newline at end of file +(_foo11 = foo) == null ? void 0 : (_foo11$bar = _foo11.bar) == null ? void 0 : (_foo11$bar$call = _foo11$bar.call(_foo11)) == null ? void 0 : _foo11$bar$call.baz; \ 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 865fc96d63..7c22a8bc0d 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, _a2, _a2$b, _a2$b$c, _a3, _a4, _a4$b, _a4$b$c, _b, _a5, _a5$b, _b2, _ref, _b3, _a$b, _ref2, _a$b2, _a6, _a6$b, _ref3, _a7, _a7$b; +var _a, _a2, _a2$b, _a2$b$c, _a3, _a4, _a4$b, _a4$b$c, _b, _a5, _a5$b, _b2, _b3, _ref, _a$b, _a$b2, _ref2, _a6, _a6$b, _a7, _a7$b, _ref3; (_a = a) == null ? void 0 : new _a.b(); (_a2 = a) == null ? void 0 : (_a2$b = _a2.b) == null ? void 0 : (_a2$b$c = _a2$b.c) == null ? void 0 : new _a2$b$c.d(); @@ -10,9 +10,9 @@ var _a, _a2, _a2$b, _a2$b$c, _a3, _a4, _a4$b, _a4$b$c, _b, _a5, _a5$b, _b2, _ref (_a5 = a) == null ? void 0 : (_a5$b = _a5.b) == null ? void 0 : new _a5$b(a.b, true); (_b2 = b) == null ? void 0 : new _b2().c; -(_ref = (_b3 = b) == null ? void 0 : new _b3()) == null ? void 0 : _ref.c; +(_b3 = b) == null ? void 0 : (_ref = new _b3()) == null ? void 0 : _ref.c; (_a$b = a.b) == null ? void 0 : new _a$b().c; -(_ref2 = (_a$b2 = a.b) == null ? void 0 : new _a$b2()) == null ? void 0 : _ref2.c; +(_a$b2 = a.b) == null ? void 0 : (_ref2 = new _a$b2()) == null ? void 0 : _ref2.c; (_a6 = a) == null ? void 0 : (_a6$b = _a6.b) == null ? void 0 : new _a6$b().c; -(_ref3 = (_a7 = a) == null ? void 0 : (_a7$b = _a7.b) == null ? void 0 : new _a7$b()) == null ? void 0 : _ref3.c; \ No newline at end of file +(_a7 = a) == null ? void 0 : (_a7$b = _a7.b) == null ? void 0 : (_ref3 = new _a7$b()) == null ? void 0 : _ref3.c; \ No newline at end of file