Skip to content

Commit

Permalink
Memoize call expressions in optional chains in loose mode (#11261)
Browse files Browse the repository at this point in the history
* Memoize in loose mode when callee is a CallExpression

* Handle more complex OptionalCallExpressions where memoization is needed

* Convert calls to Function#call when member needs memoization

* Only update call context for member expressions
  • Loading branch information
oliverdunk committed Mar 20, 2020
1 parent 2e6f958 commit 693a5df
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
17 changes: 14 additions & 3 deletions packages/babel-plugin-proposal-optional-chaining/src/index.js
Expand Up @@ -7,6 +7,16 @@ export default declare((api, options) => {

const { loose = false } = options;

function isSimpleMemberExpression(expression) {
return (
t.isIdentifier(expression) ||
t.isSuper(expression) ||
(t.isMemberExpression(expression) &&
!expression.computed &&
isSimpleMemberExpression(expression.object))
);
}

return {
name: "proposal-optional-chaining",
inherits: syntaxOptionalChaining,
Expand Down Expand Up @@ -50,9 +60,10 @@ export default declare((api, options) => {

let ref;
let check;
if (loose && isCall) {
if (loose && isCall && isSimpleMemberExpression(chain)) {
// If we are using a loose transform (avoiding a Function#call) and we are at the call,
// we can avoid a needless memoize.
// we can avoid a needless memoize. We only do this if the callee is a simple member
// expression, to avoid multiple calls to nested call expressions.
check = ref = chain;
} else {
ref = scope.maybeGenerateMemoised(chain);
Expand All @@ -73,7 +84,7 @@ export default declare((api, options) => {
// Ensure call expressions have the proper `this`
// `foo.bar()` has context `foo`.
if (isCall && t.isMemberExpression(chain)) {
if (loose) {
if (loose && isSimpleMemberExpression(chain)) {
// 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()`
node.callee = chain;
Expand Down
Expand Up @@ -7,6 +7,14 @@ function test(foo) {

foo?.bar()

foo.get(bar)?.()

foo.bar()?.()
foo[bar]()?.()

foo.bar().baz?.()
foo[bar]().baz?.()

foo.bar?.(foo.bar, false)

foo?.bar?.(foo.bar, true)
Expand Down
@@ -1,14 +1,19 @@
function test(foo) {
var _foo$bar, _foo$bar2, _foo$bar3, _foo$bar4, _foo$bar5;
var _foo$bar, _foo$get, _foo$bar2, _foo$bar3, _foo$bar$baz, _foo$bar4, _foo$bar$baz2, _foo$bar5, _foo$bar6, _foo$bar7, _foo$bar8, _foo$bar9;

foo == null ? void 0 : foo.bar;
foo == null ? void 0 : (_foo$bar = foo.bar) == null ? void 0 : _foo$bar.baz;
foo == null ? void 0 : foo(foo);
foo == null ? void 0 : foo.bar();
(_foo$get = foo.get(bar)) == null ? void 0 : _foo$get();
(_foo$bar2 = foo.bar()) == null ? void 0 : _foo$bar2();
(_foo$bar3 = foo[bar]()) == null ? void 0 : _foo$bar3();
(_foo$bar$baz = (_foo$bar4 = foo.bar()).baz) == null ? void 0 : _foo$bar$baz.call(_foo$bar4);
(_foo$bar$baz2 = (_foo$bar5 = foo[bar]()).baz) == null ? void 0 : _foo$bar$baz2.call(_foo$bar5);
foo.bar == null ? void 0 : foo.bar(foo.bar, false);
foo == null ? void 0 : foo.bar == null ? void 0 : foo.bar(foo.bar, true);
(_foo$bar2 = foo.bar) == null ? void 0 : _foo$bar2.baz(foo.bar, false);
foo == null ? void 0 : (_foo$bar3 = foo.bar) == null ? void 0 : _foo$bar3.baz(foo.bar, true);
(_foo$bar4 = foo.bar) == null ? void 0 : _foo$bar4.baz == null ? void 0 : _foo$bar4.baz(foo.bar, false);
foo == null ? void 0 : (_foo$bar5 = foo.bar) == null ? void 0 : _foo$bar5.baz == null ? void 0 : _foo$bar5.baz(foo.bar, true);
(_foo$bar6 = foo.bar) == null ? void 0 : _foo$bar6.baz(foo.bar, false);
foo == null ? void 0 : (_foo$bar7 = foo.bar) == null ? void 0 : _foo$bar7.baz(foo.bar, true);
(_foo$bar8 = foo.bar) == null ? void 0 : _foo$bar8.baz == null ? void 0 : _foo$bar8.baz(foo.bar, false);
foo == null ? void 0 : (_foo$bar9 = foo.bar) == null ? void 0 : _foo$bar9.baz == null ? void 0 : _foo$bar9.baz(foo.bar, true);
}

0 comments on commit 693a5df

Please sign in to comment.