Use a better nested syntax
This way, it quickly returns from the conditions. The first nil will now exit, instead of checking every nil. This also allows conditionalChaining inside a container to still operate the container.
This commit is contained in:
parent
3fae121460
commit
2a496890ff
@ -47,11 +47,11 @@ export default function ({ types: t }) {
|
|||||||
node.object = ref;
|
node.object = ref;
|
||||||
|
|
||||||
let parent = path;
|
let parent = path;
|
||||||
let expression;
|
let expression = path;
|
||||||
do {
|
while (parent.listKey === undefined) {
|
||||||
expression = parent;
|
expression = parent;
|
||||||
parent = parent.parentPath;
|
parent = parent.parentPath;
|
||||||
} while (!parent.container);
|
}
|
||||||
|
|
||||||
const replace = parent.isExpression() ? parent : expression;
|
const replace = parent.isExpression() ? parent : expression;
|
||||||
replace.replaceWith(t.conditionalExpression(
|
replace.replaceWith(t.conditionalExpression(
|
||||||
|
|||||||
@ -2,4 +2,4 @@ var _a, _a$b$c, _a$b, _a2;
|
|||||||
|
|
||||||
(_a = a) == null ? void 0 : _a.b = 42;
|
(_a = a) == null ? void 0 : _a.b = 42;
|
||||||
|
|
||||||
(((_a2 = a) == null ? void 0 : _a$b = _a2.b) == null ? void 0 : _a$b$c = _a$b.c) == null ? void 0 : _a$b$c.d = 42;
|
(_a2 = a) == null ? void 0 : (_a$b = _a2.b) == null ? void 0 : (_a$b$c = _a$b.c) == null ? void 0 : _a$b$c.d = 42;
|
||||||
6
packages/babel-plugin-transform-optional-chaining/test/fixtures/general/containers/actual.js
vendored
Normal file
6
packages/babel-plugin-transform-optional-chaining/test/fixtures/general/containers/actual.js
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
var street = user.address?.street
|
||||||
|
street = user.address?.street
|
||||||
|
|
||||||
|
test(a?.b, 1);
|
||||||
|
|
||||||
|
(1, a?.b, 2);
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
var _user$address, _user$address2, _a, _a2;
|
||||||
|
|
||||||
|
var street = (_user$address = user.address) == null ? void 0 : _user$address.street;
|
||||||
|
(_user$address2 = user.address) == null ? void 0 : street = _user$address2.street;
|
||||||
|
|
||||||
|
test((_a = a) == null ? void 0 : _a.b, 1);
|
||||||
|
|
||||||
|
1, (_a2 = a) == null ? void 0 : _a2.b, 2;
|
||||||
@ -2,4 +2,4 @@ var _a, _a$b$c, _a$b, _a2;
|
|||||||
|
|
||||||
(_a = a) == null ? void 0 : delete _a.b;
|
(_a = a) == null ? void 0 : delete _a.b;
|
||||||
|
|
||||||
(((_a2 = a) == null ? void 0 : _a$b = _a2.b) == null ? void 0 : _a$b$c = _a$b.c) == null ? void 0 : delete _a$b$c.d;
|
(_a2 = a) == null ? void 0 : (_a$b = _a2.b) == null ? void 0 : (_a$b$c = _a$b.c) == null ? void 0 : delete _a$b$c.d;
|
||||||
@ -2,4 +2,4 @@ var _foo, _a$b$c, _a;
|
|||||||
|
|
||||||
(_foo = foo) == null ? void 0 : _foo.bar;
|
(_foo = foo) == null ? void 0 : _foo.bar;
|
||||||
|
|
||||||
(_a$b$c = (_a = a) == null ? void 0 : _a.b.c) == null ? void 0 : _a$b$c.d.e;
|
(_a = a) == null ? void 0 : (_a$b$c = _a.b.c) == null ? void 0 : _a$b$c.d.e;
|
||||||
@ -1,10 +1,10 @@
|
|||||||
var _a, _a$b$c, _a$b, _a2, _a3, _a$b$c2, _a$b2, _a4, _b, _a5, _a5$b;
|
var _a, _a$b$c, _a$b, _a2, _a3, _a$b$c2, _a$b2, _a4, _b, _a5, _a5$b;
|
||||||
|
|
||||||
(_a = a) == null ? void 0 : new _a.b();
|
(_a = a) == null ? void 0 : new _a.b();
|
||||||
(((_a2 = a) == null ? void 0 : _a$b = _a2.b) == null ? void 0 : _a$b$c = _a$b.c) == null ? void 0 : new _a$b$c.d();
|
(_a2 = a) == null ? void 0 : (_a$b = _a2.b) == null ? void 0 : (_a$b$c = _a$b.c) == null ? void 0 : new _a$b$c.d();
|
||||||
|
|
||||||
(_a3 = a) == null ? void 0 : new _a3.b();
|
(_a3 = a) == null ? void 0 : new _a3.b();
|
||||||
(((_a4 = a) == null ? void 0 : _a$b2 = _a4.b) == null ? void 0 : _a$b$c2 = _a$b2.c) == null ? void 0 : new _a$b$c2.d();
|
(_a4 = a) == null ? void 0 : (_a$b2 = _a4.b) == null ? void 0 : (_a$b$c2 = _a$b2.c) == null ? void 0 : new _a$b$c2.d();
|
||||||
|
|
||||||
(_b = b) == null ? void 0 : new _b(b);
|
(_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);
|
||||||
Loading…
x
Reference in New Issue
Block a user