Skip to content

Commit

Permalink
add #m?.() support
Browse files Browse the repository at this point in the history
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
  • Loading branch information
JLHwung and nicolo-ribaudo committed May 25, 2020
1 parent 55bdc00 commit 12eb77b
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 161 deletions.
12 changes: 11 additions & 1 deletion packages/babel-helper-create-class-features-plugin/src/fields.js
Expand Up @@ -300,7 +300,13 @@ const privateNameHandlerSpec = {
// The first access (the get) should do the memo assignment.
this.memoise(member, 1);

return optimiseCall(this.get(member), this.receiver(member), args);
return optimiseCall(this.get(member), this.receiver(member), args, false);
},

optionalCall(member, args) {
this.memoise(member, 1);

return optimiseCall(this.get(member), this.receiver(member), args, true);
},
};

Expand Down Expand Up @@ -328,6 +334,10 @@ const privateNameHandlerLoose = {
call(member, args) {
return t.callExpression(this.get(member), args);
},

optionalCall(member, args) {
return t.optionalCallExpression(this.get(member), args, true);
},
};

export function transformPrivateNamesUsage(
Expand Down
27 changes: 24 additions & 3 deletions packages/babel-helper-member-expression-to-functions/src/index.js
Expand Up @@ -109,7 +109,11 @@ const handle = {
if (parentPath.isOptionalCallExpression()) {
// Checking `parent.callee` since we could be in the arguments, eg
// `bad?.(FOO?.BAR)`.
return parent.optional || parent.callee !== node;
// Also skip `FOO?.BAR` in `FOO?.BAR?.()` since we need to transform the optional call to ensure proper this
return (
// In FOO?.#BAR?.(), endPath points the optional call expression so we skip FOO?.#BAR
(node !== member.node && parent.optional) || parent.callee !== node
);
}
return true;
});
Expand Down Expand Up @@ -145,7 +149,9 @@ const handle = {
continue;
}
// prevent infinite loop: unreachable if the AST is well-formed
throw new Error("Internal error");
throw new Error(
`Internal error: unexpected ${startingOptional.node.type}`,
);
}

const { scope } = member;
Expand All @@ -163,14 +169,23 @@ const handle = {
});
startingOptional.replaceWith(toNonOptional(startingOptional, baseRef));
if (parentIsOptionalCall) {
parentPath.replaceWith(this.call(member, parent.arguments));
if (parent.optional) {
parentPath.replaceWith(this.optionalCall(member, parent.arguments));
} else {
parentPath.replaceWith(this.call(member, parent.arguments));
}
} else {
member.replaceWith(this.get(member));
}

let regular = member.node;
for (let current = member; current !== endPath; ) {
const { parentPath } = current;
// skip transforming `Foo.#BAR?.call(FOO)`
if (parentPath === endPath && parentIsOptionalCall && parent.optional) {
regular = parentPath.node;
break;
}
regular = toNonOptional(parentPath, regular);
current = parentPath;
}
Expand Down Expand Up @@ -299,6 +314,12 @@ const handle = {
return;
}

// MEMBER?.(ARGS) -> _optionalCall(MEMBER, ARGS)
if (parentPath.isOptionalCallExpression({ callee: node })) {
parentPath.replaceWith(this.optionalCall(member, parent.arguments));
return;
}

// { KEY: MEMBER } = OBJ -> { KEY: _destructureSet(MEMBER) } = OBJ
// { KEY: MEMBER = _VALUE } = OBJ -> { KEY: _destructureSet(MEMBER) = _VALUE } = OBJ
// {...MEMBER} -> {..._destructureSet(MEMBER)}
Expand Down
9 changes: 8 additions & 1 deletion packages/babel-helper-optimise-call-expression/src/index.js
@@ -1,6 +1,6 @@
import * as t from "@babel/types";

export default function(callee, thisNode, args) {
export default function(callee, thisNode, args, optional) {
if (
args.length === 1 &&
t.isSpreadElement(args[0]) &&
Expand All @@ -12,6 +12,13 @@ export default function(callee, thisNode, args) {
args[0].argument,
]);
} else {
if (optional) {
return t.optionalCallExpression(
t.optionalMemberExpression(callee, t.identifier("call"), false, true),
[thisNode, ...args],
false,
);
}
return t.callExpression(t.memberExpression(callee, t.identifier("call")), [
thisNode,
...args,
Expand Down
3 changes: 2 additions & 1 deletion packages/babel-helper-replace-supers/src/index.js
Expand Up @@ -158,6 +158,7 @@ const specHandlers = {
this._get(superMember, thisRefs),
t.cloneNode(thisRefs.this),
args,
false,
);
},
};
Expand Down Expand Up @@ -215,7 +216,7 @@ const looseHandlers = {
},

call(superMember, args) {
return optimiseCall(this.get(superMember), t.thisExpression(), args);
return optimiseCall(this.get(superMember), t.thisExpression(), args, false);
},
};

Expand Down
Expand Up @@ -66,9 +66,6 @@ class Foo {
fn?.().Foo.#self?.getSelf?.()?.self.#m?.();
}

static testtest() {
fn?.().Foo.#self.getSelf?.().#m?.();
}
}

Foo.test();

Large diffs are not rendered by default.

Expand Up @@ -66,9 +66,6 @@ class Foo {
fn?.().Foo.#self?.getSelf?.()?.self.#m?.();
}

static testtest() {
fn?.().Foo.#self.getSelf?.().#m?.();
}
}

Foo.test();

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 12eb77b

Please sign in to comment.