Optional call expressions short circuit later member expressions

This commit is contained in:
Justin Ridgewell
2017-06-07 01:58:31 -04:00
parent 54d9732d0b
commit 9e91ac54d3
7 changed files with 88 additions and 38 deletions

View File

@@ -71,6 +71,30 @@ export default function ({ types: t }) {
}
}
function findReplacementPath(path) {
return path.find((path) => {
const { parentPath } = path;
if (path.key == "left" && parentPath.isAssignmentExpression()) {
return false;
}
if (path.key == "object" && parentPath.isMemberExpression()) {
return false;
}
if (path.key == "callee" && (parentPath.isCallExpression() || parentPath.isNewExpression())) {
return false;
}
if (path.key == "argument" && parentPath.isUpdateExpression()) {
return false;
}
if (path.key == "argument" && parentPath.isUnaryExpression({ operator: "delete" })) {
return false;
}
return true;
});
}
return {
inherits: syntaxOptionalChaining,
@@ -80,29 +104,7 @@ export default function ({ types: t }) {
return;
}
const replace = path.find((path) => {
const { parentPath } = path;
if (path.key == "left" && parentPath.isAssignmentExpression()) {
return false;
}
if (path.key == "object" && parentPath.isMemberExpression()) {
return false;
}
if (path.key == "callee" && (parentPath.isCallExpression() || parentPath.isNewExpression())) {
return false;
}
if (path.key == "argument" && parentPath.isUpdateExpression()) {
return false;
}
if (path.key == "argument" && parentPath.isUnaryExpression({ operator: "delete" })) {
return false;
}
return true;
});
optional(path, "object", replace);
optional(path, "object", findReplacementPath(path));
},
"NewExpression|CallExpression"(path) {
@@ -110,7 +112,7 @@ export default function ({ types: t }) {
return;
}
optional(path, "callee", path, path.isCallExpression(), this.opts.loose);
optional(path, "callee", findReplacementPath(path), path.isCallExpression(), this.opts.loose);
},
},
};

View File

@@ -5,3 +5,15 @@ foo?.bar()
foo.bar?.(foo.bar, false)
foo?.bar?.(foo.bar, true)
foo?.().bar
foo?.()?.bar
foo.bar?.().baz
foo.bar?.()?.baz
foo?.bar?.().baz
foo?.bar?.()?.baz

View File

@@ -1,4 +1,4 @@
var _foo, _foo2, _foo$bar, _foo3, _foo4, _foo4$bar;
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;
(_foo = foo) == null ? void 0 : _foo(foo);
@@ -6,4 +6,16 @@ var _foo, _foo2, _foo$bar, _foo3, _foo4, _foo4$bar;
(_foo$bar = (_foo3 = foo).bar) == null ? void 0 : _foo$bar.call(_foo3, foo.bar, false);
(_foo4 = foo) == null ? void 0 : (_foo4$bar = _foo4.bar) == null ? void 0 : _foo4$bar.call(_foo4, foo.bar, true);
(_foo4 = foo) == null ? void 0 : (_foo4$bar = _foo4.bar) == null ? void 0 : _foo4$bar.call(_foo4, foo.bar, true);
(_foo5 = foo) == null ? void 0 : _foo5().bar;
(_foo6 = (_foo7 = foo) == null ? void 0 : _foo7()) == null ? void 0 : _foo6.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;
(_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;

View File

@@ -1,15 +1,19 @@
foo?.bar
foo?.bar;
a?.b.c?.d.e
a?.b.c?.d.e;
a.b?.c.d?.e
a.b?.c.d?.e;
a.b.c?.d?.e
a.b.c?.d?.e;
orders?.[0].price
orders?.[0].price;
orders?.[0]?.price
orders?.[0]?.price;
orders[client?.key].price
orders[client?.key].price;
orders[client.key]?.price
orders[client.key]?.price;
(0, a?.b).c;
(0, (0, a?.b).c?.d).e;

View File

@@ -1,4 +1,4 @@
var _foo, _a, _a$b$c, _a$b, _a$b$c$d, _a$b$c2, _a$b$c2$d, _orders, _orders2, _orders2$, _client, _orders$client$key;
var _foo, _a, _a$b$c, _a$b, _a$b$c$d, _a$b$c2, _a$b$c2$d, _orders, _orders2, _orders2$, _client, _orders$client$key, _a2, _c, _a3;
(_foo = foo) == null ? void 0 : _foo.bar;
@@ -14,4 +14,8 @@ var _foo, _a, _a$b$c, _a$b, _a$b$c$d, _a$b$c2, _a$b$c2$d, _orders, _orders2, _or
orders[(_client = client) == null ? void 0 : _client.key].price;
(_orders$client$key = orders[client.key]) == null ? void 0 : _orders$client$key.price;
(_orders$client$key = orders[client.key]) == null ? void 0 : _orders$client$key.price;
(0, (_a2 = a) == null ? void 0 : _a2.b).c;
(0, (_c = (0, (_a3 = a) == null ? void 0 : _a3.b).c) == null ? void 0 : _c.d).e;

View File

@@ -6,3 +6,11 @@ new a?.b?.c?.d()
new b?.(b)
new a?.b?.(a.b, true)
new b?.().c
new b?.()?.c
new a.b?.().c
new a.b?.()?.c
new a?.b?.().c
new a?.b?.()?.c

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;
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;
(_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();
@@ -7,4 +7,12 @@ var _a, _a2, _a2$b, _a2$b$c, _a3, _a4, _a4$b, _a4$b$c, _b, _a5, _a5$b;
(_a4 = a) == null ? void 0 : (_a4$b = _a4.b) == null ? void 0 : (_a4$b$c = _a4$b.c) == null ? void 0 : new _a4$b$c.d();
(_b = b) == null ? void 0 : new _b(b);
(_a5 = a) == null ? void 0 : (_a5$b = _a5.b) == null ? void 0 : new _a5$b(a.b, true);
(_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;
(_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;
(_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;