Skip to content

Commit

Permalink
refactor: simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed May 27, 2020
1 parent 4f3369f commit 3672f7f
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions packages/babel-plugin-proposal-optional-chaining/src/index.js
Expand Up @@ -121,26 +121,23 @@ export default declare((api, options) => {
replacementPath.parentPath.isCallExpression()
) {
// `(a?.b)()` to `(a == null ? undefined : a.b.bind(a))()`
const { object, property, computed } = replacement;
let memoisedObject;
const { object } = replacement;
let baseRef;
if (!loose) {
// memoize the context object in non-loose mode
// `(a?.b.c)()` to `(a == null ? undefined : (_a$b = a.b).c.bind(_a$b))()`
const context = scope.maybeGenerateMemoised(object);
if (context) {
memoisedObject = t.assignmentExpression("=", context, object);
baseRef = scope.maybeGenerateMemoised(object);
if (baseRef) {
replacement.object = t.assignmentExpression(
"=",
baseRef,
object,
);
}
}
replacement = t.callExpression(
t.memberExpression(
t.memberExpression(
memoisedObject ?? object,
property,
computed,
),
t.identifier("bind"),
),
[t.cloneNode(memoisedObject?.left ?? object)],
t.memberExpression(replacement, t.identifier("bind")),
[t.cloneNode(baseRef ?? object)],
);
}
replacementPath.replaceWith(
Expand Down

0 comments on commit 3672f7f

Please sign in to comment.