Simplify transform

This also makes the output use less comparisons for `foo?.()?.bar`
cases. 😁
This commit is contained in:
Justin Ridgewell
2017-06-07 02:56:20 -04:00
parent 9e91ac54d3
commit 462825b15a
3 changed files with 24 additions and 26 deletions

View File

@@ -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);
},
},
};

View File

@@ -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;
(_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;

View File

@@ -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;
(_a7 = a) == null ? void 0 : (_a7$b = _a7.b) == null ? void 0 : (_ref3 = new _a7$b()) == null ? void 0 : _ref3.c;