Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix optional chain optimization in sequence expression #15888

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import {
} from "@babel/helper-skip-transparent-expression-wrappers";
import { willPathCastToBoolean, findOutermostTransparentParent } from "./util";

// TODO(Babel 9): Use .at(-1)
const last = <T>(arr: T[]) => arr[arr.length - 1];

function isSimpleMemberExpression(
expression: t.Expression | t.Super,
): expression is t.Identifier | t.Super | t.MemberExpression {
Expand Down Expand Up @@ -209,9 +212,10 @@ export function transformOptionalChain(
!ifNullishBoolean && t.isUnaryExpression(ifNullish, { operator: "void" });

const isEvaluationValueIgnored =
(t.isExpressionStatement(replacementPath.parent) ||
t.isSequenceExpression(replacementPath.parent)) &&
!replacementPath.isCompletionRecord();
(t.isExpressionStatement(replacementPath.parent) &&
!replacementPath.isCompletionRecord()) ||
(t.isSequenceExpression(replacementPath.parent) &&
last(replacementPath.parent.expressions) !== replacementPath.node);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might make sense to have a path.isValueObservable() helper eventually.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It certainly reminds me of the messy path._getCompletionRecords. Thankfully the switch statement is not an expression.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar usage:

const shouldPreserveCompletion =
(!isExpressionStatement(parent) && !isSequenceExpression(parent)) ||
path.isCompletionRecord();


// prettier-ignore
const tpl = ifNullishFalse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ var _foo, _a, _a$b, _a$b$c, _orders, _orders2, _client, _orders$client$key, _a2,
(_orders2 = orders) === null || _orders2 === void 0 || (_orders2 = _orders2[0]) === null || _orders2 === void 0 || _orders2.price;
orders[(_client = client) === null || _client === void 0 ? void 0 : _client.key].price;
(_orders$client$key = orders[client.key]) === null || _orders$client$key === void 0 || _orders$client$key.price;
(0, (_a2 = a) === null || _a2 === void 0 || _a2.b).c;
(0, (_a2 = a) === null || _a2 === void 0 ? void 0 : _a2.b).c;
(0, (_c = (0, (_a3 = a) === null || _a3 === void 0 ? void 0 : _a3.b).c) === null || _c === void 0 ? void 0 : _c.d).e;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
let a = (0, null?.prop);

expect(a).toBe(undefined);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
let a = (null?.prop1, null?.prop2);

expect(a).toBe(undefined);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var _ref, _ref2;
let a = ((_ref = null) !== null && _ref !== void 0 && _ref.prop1, (_ref2 = null) === null || _ref2 === void 0 ? void 0 : _ref2.prop2);
expect(a).toBe(undefined);