Skip to content

Commit

Permalink
Optional chaining returns undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Sep 20, 2024
1 parent 2d9cd27 commit f2676a8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
26 changes: 8 additions & 18 deletions packages/babel-plugin-polyfill-corejs3/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,33 +357,23 @@ export default defineProvider<Options>(function (
const [thisArg, thisArg2] = maybeMemoizeContext(node, path.scope);

path.replaceWith(
template.expression.ast`
${check} ? null : Function.call.bind(${id}(${thisArg}), ${thisArg2})
`,
check(
template.expression.ast`
Function.call.bind(${id}(${thisArg}), ${thisArg2})
`,
),
);
} else if (t.isOptionalMemberExpression(node)) {
const check = extractOptionalCheck(
path.scope,
node as t.OptionalMemberExpression,
);
callMethod(path, id, true, c =>
t.conditionalExpression(check, t.nullLiteral(), c),
);
const check = extractOptionalCheck(path.scope, node);
callMethod(path, id, true, check);
} else {
callMethod(path, id, true);
}
} else if (t.isCallExpression(parent) && parent.callee === node) {
callMethod(path, id, false);
} else if (t.isOptionalMemberExpression(node)) {
const check = extractOptionalCheck(path.scope, node);

path.replaceWith(
t.conditionalExpression(
check,
t.nullLiteral(),
t.callExpression(id, [node.object]),
),
);
path.replaceWith(check(t.callExpression(id, [node.object])));
if (t.isOptionalMemberExpression(parent)) parent.optional = true;
} else {
path.replaceWith(t.callExpression(id, [node.object]));
Expand Down
7 changes: 6 additions & 1 deletion packages/babel-plugin-polyfill-corejs3/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ export function extractOptionalCheck(
const assign = t.assignmentExpression("=", ctx, optionalNode.object);
optionalNode.object = t.cloneNode(ctx);

return t.binaryExpression("==", assign, t.nullLiteral());
return ifNotNullish =>
t.conditionalExpression(
t.binaryExpression("==", assign, t.nullLiteral()),
t.unaryExpression("void", t.numericLiteral(0)),
ifNotNullish,
);
}

export function isCoreJSSource(source: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ _toSortedInstanceProperty(a)?.call(a, x);
_toSortedInstanceProperty(_context = a.b)?.call(_context, x);
_toSortedInstanceProperty(_context2 = a.b)?.call(_context2, x).c;
_toSortedInstanceProperty(_context3 = a.b)?.call(_context3, x)?.c;
((_context4 = a) == null ? null : Function.call.bind(_toSortedInstanceProperty(_context4), _context4))?.(x);
((_context5 = a) == null ? null : Function.call.bind(_toSortedInstanceProperty(_context6 = _context5.b), _context6))?.(x).c;
((_context7 = a.b) == null ? null : Function.call.bind(_toSortedInstanceProperty(_context7), _context7))?.(x).c;
((_context8 = a) == null ? null : Function.call.bind(_toSortedInstanceProperty(_context9 = _context8.b), _context9))?.(x)?.c;
((_context10 = a.b) == null ? null : Function.call.bind(_toSortedInstanceProperty(_context10), _context10))?.(x)?.c;
((_context11 = a) == null ? null : _toSortedInstanceProperty(_context12 = _context11.b))?.call(_context12, x).c;
((_context4 = a) == null ? void 0 : Function.call.bind(_toSortedInstanceProperty(_context4), _context4))?.(x);
((_context5 = a) == null ? void 0 : Function.call.bind(_toSortedInstanceProperty(_context6 = _context5.b), _context6))?.(x).c;
((_context7 = a.b) == null ? void 0 : Function.call.bind(_toSortedInstanceProperty(_context7), _context7))?.(x).c;
((_context8 = a) == null ? void 0 : Function.call.bind(_toSortedInstanceProperty(_context9 = _context8.b), _context9))?.(x)?.c;
((_context10 = a.b) == null ? void 0 : Function.call.bind(_toSortedInstanceProperty(_context10), _context10))?.(x)?.c;
((_context11 = a) == null ? void 0 : _toSortedInstanceProperty(_context12 = _context11.b))?.call(_context12, x).c;
_toSortedInstanceProperty(a.b.c)?.d.e;
((_context13 = a.b) == null ? null : _toSortedInstanceProperty(_context13.c))?.d.e;
((_context14 = a.b) == null ? null : _toSortedInstanceProperty(_context14.c))?.d.e;
((_context13 = a.b) == null ? void 0 : _toSortedInstanceProperty(_context13.c))?.d.e;
((_context14 = a.b) == null ? void 0 : _toSortedInstanceProperty(_context14.c))?.d.e;
_Array$from(x);
_Array$from?.(x);
_Array$from?.(x).c;
Expand Down

0 comments on commit f2676a8

Please sign in to comment.