From 9ce797dd34ac16bd5b29efbedae2751a177e0859 Mon Sep 17 00:00:00 2001 From: Justin Ridgewell Date: Sat, 3 Jun 2017 23:52:04 -0400 Subject: [PATCH] Reduce context memoization when possible --- .../src/index.js | 16 ++++++++-------- .../fixtures/general/function-call/expected.js | 4 ++-- .../test/fixtures/general/memoize/expected.js | 10 +++++----- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/packages/babel-plugin-transform-optional-chaining/src/index.js b/packages/babel-plugin-transform-optional-chaining/src/index.js index 9c4b7805a5..826a5b8a4c 100644 --- a/packages/babel-plugin-transform-optional-chaining/src/index.js +++ b/packages/babel-plugin-transform-optional-chaining/src/index.js @@ -29,16 +29,14 @@ export default function ({ types: t }) { if (loose && atCall) { // If we are using a loose transform (avoiding a Function#call) and we are at the call, // we can avoid a needless memoize. - ref = chain; - check = ref; + check = ref = chain; } else { ref = scope.maybeGenerateMemoised(chain); if (ref) { check = t.assignmentExpression("=", ref, chain); node[replaceKey] = ref; } else { - ref = chain; - check = chain; + check = ref = chain; } } @@ -53,10 +51,12 @@ export default function ({ types: t }) { // Otherwise, we need to memoize the context object, and change the call into a Function#call. // `a.?b.?()` translates roughly to `(_b = _a.b) != null && _b.call(_a)` const { object } = chain; - const context = scope.generateUidIdentifierBasedOnNode(object); - - scope.push({ id: context }); - chain.object = t.assignmentExpression("=", context, object); + let context = scope.maybeGenerateMemoised(object); + if (context) { + chain.object = t.assignmentExpression("=", context, object); + } else { + context = object; + } node.arguments.unshift(context); node.callee = t.memberExpression(node.callee, t.identifier("call")); 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..ca85c85dac 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, _foo4, _foo4$bar; (_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 +(_foo4 = foo) == null ? void 0 : (_foo4$bar = _foo4.bar) == null ? void 0 : _foo4$bar.call(_foo4, foo.bar, true); \ No newline at end of file diff --git a/packages/babel-plugin-transform-optional-chaining/test/fixtures/general/memoize/expected.js b/packages/babel-plugin-transform-optional-chaining/test/fixtures/general/memoize/expected.js index d02f52f934..2e24fb6f41 100644 --- a/packages/babel-plugin-transform-optional-chaining/test/fixtures/general/memoize/expected.js +++ b/packages/babel-plugin-transform-optional-chaining/test/fixtures/general/memoize/expected.js @@ -1,5 +1,5 @@ function test(foo) { - var _foo$bar, _foo$bar2, _foo, _foo$bar3, _foo2, _foo$bar4, _foo$bar5, _foo$bar6, _foo$bar6$baz, _foo$bar7, _foo$bar8, _foo$bar8$baz, _foo$bar9; + var _foo$bar, _foo$bar2, _foo$bar3, _foo$bar4, _foo$bar5, _foo$bar6, _foo$bar6$baz, _foo$bar7, _foo$bar7$baz; foo == null ? void 0 : foo.bar; @@ -9,15 +9,15 @@ function test(foo) { foo == null ? void 0 : foo.bar(); - (_foo$bar2 = (_foo = foo).bar) == null ? void 0 : _foo$bar2.call(_foo, foo.bar, false); + (_foo$bar2 = foo.bar) == null ? void 0 : _foo$bar2.call(foo, foo.bar, false); - foo == null ? void 0 : (_foo$bar3 = (_foo2 = foo).bar) == null ? void 0 : _foo$bar3.call(_foo2, foo.bar, true); + foo == null ? void 0 : (_foo$bar3 = foo.bar) == null ? void 0 : _foo$bar3.call(foo, foo.bar, true); (_foo$bar4 = foo.bar) == null ? void 0 : _foo$bar4.baz(foo.bar, false); foo == null ? void 0 : (_foo$bar5 = foo.bar) == null ? void 0 : _foo$bar5.baz(foo.bar, true); - (_foo$bar6 = foo.bar) == null ? void 0 : (_foo$bar6$baz = (_foo$bar7 = _foo$bar6).baz) == null ? void 0 : _foo$bar6$baz.call(_foo$bar7, foo.bar, false); + (_foo$bar6 = foo.bar) == null ? void 0 : (_foo$bar6$baz = _foo$bar6.baz) == null ? void 0 : _foo$bar6$baz.call(_foo$bar6, foo.bar, false); - foo == null ? void 0 : (_foo$bar8 = foo.bar) == null ? void 0 : (_foo$bar8$baz = (_foo$bar9 = _foo$bar8).baz) == null ? void 0 : _foo$bar8$baz.call(_foo$bar9, foo.bar, true); + foo == null ? void 0 : (_foo$bar7 = foo.bar) == null ? void 0 : (_foo$bar7$baz = _foo$bar7.baz) == null ? void 0 : _foo$bar7$baz.call(_foo$bar7, foo.bar, true); } \ No newline at end of file