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

Implement @babel/plugin-bugfix-v8-spread-parameters-in-optional-chaining #13009

Merged
merged 7 commits into from Mar 19, 2021

Conversation

JLHwung
Copy link
Contributor

@JLHwung JLHwung commented Mar 16, 2021

Q                       A
Fixed Issues? Fixes #13001
Patch: Bug Fix? Y
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

This PR transpiles the following optional chaining usage when targeted to latest V8 derived browsers

fn ?. (x, ...y, z)
a?.b (...y, z)

see also code examples in the upstream V8 bug: https://bugs.chromium.org/p/v8/issues/detail?id=11558.

The other unmentioned optional chaining usage will not be transpiled if your targets already have optional chaining support.

In this PR we export the optional chaining transform from proposal-optional-chaining and apply the transform in bugfix-v8-spread-parameters-after-optional-chaining when we see a specific affected code patterns.

Note that this may be sub-optimal for nested optional chains, i.e. in fn ?. (...y, z) ?. x, both optional chaining ? will be transpiled even though we could have only transpiled the first one. However I think since 1) it is an edge case and 2) we have another optional transform in class properties, the maintainability should win here.

For reviewers: This PR depends on #13008, please review that one first.

@JLHwung JLHwung added PR: Bug Fix 🐛 A type of pull request used for our changelog categories i: browser bug labels Mar 16, 2021
@babel-bot
Copy link
Collaborator

babel-bot commented Mar 16, 2021

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/44467/

@codesandbox-ci
Copy link

codesandbox-ci bot commented Mar 16, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 687ddb5:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

@JLHwung JLHwung changed the title Implement @babel/plugin-bugfix-v8-spread-parameters-after-optional-chaining Implement @babel/plugin-bugfix-v8-spread-parameters-in-optional-chaining Mar 16, 2021
"a?.[fn?.(...[], 0)]", // optional chain in property will be handled when traversed
"a?.(fn?.(...[], 0))", // optional chain in arguments will be handled when traversed
"class C { #c; p = obj?.#c(...[]) }",
];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here are the positive / negative cases on detecting the affected code patterns.

* @param {(NodePath<t.OptionalMemberExpression | t.OptionalCallExpression>)} path
* @returns {boolean}
*/
export function shouldTransform(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This routine determine whether we should transpile the affected optional chain.


a?.b(...c, 1);

a?.b?.(...c, 1);
Copy link
Member

Choose a reason for hiding this comment

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

It would be enough to transform this to

(_a2$b = (_a2 = a)?.b) === null || _a2$b === void 0 ? void 0 : _a2$b.call(_a2, ...c, 1);

instead of

(_a2 = a) === null || _a2 === void 0 ? void 0 : (_a2$b = _a2.b) === null || _a2$b === void 0 ? void 0 : _a2$b.call(_a2, ...c, 1);

We don't need to transform inner optional checks, only the one that directly affects the call expression.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I mentioned that this PR is sub-optimal for nested chains. But because it's an edge case and we already have to maintain two optional chain transform, I prefer to leave it as-is.

@JLHwung JLHwung merged commit c2a4249 into babel:main Mar 19, 2021
@JLHwung JLHwung deleted the fix-13001 branch March 19, 2021 17:26
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jun 19, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i: browser bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Bug Fix 🐛 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bugfix transform needed for optional chain expressions with spread arguments
4 participants