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.

@babel-bot
Copy link
Collaborator

@babel-bot babel-bot commented Mar 16, 2021

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

@codesandbox
Copy link

@codesandbox codesandbox 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(...[]) }",
];

This comment has been minimized.

@JLHwung

JLHwung Mar 16, 2021
Author Contributor

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

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

This comment has been minimized.

@JLHwung

JLHwung Mar 16, 2021
Author Contributor

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


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

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

This comment has been minimized.

@nicolo-ribaudo

nicolo-ribaudo Mar 19, 2021
Member

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.

This comment has been minimized.

@JLHwung

JLHwung Mar 19, 2021
Author Contributor

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 force-pushed the JLHwung:fix-13001 branch from 8f80bfd to 687ddb5 Mar 19, 2021
@JLHwung JLHwung merged commit c2a4249 into babel:main Mar 19, 2021
24 of 26 checks passed
24 of 26 checks passed
@github-actions
Prepare Cache
Details
@github-actions
Test on Node.js Latest
Details
@github-actions
Build Babel Artifacts
Details
@github-actions
Test Babel 8 breaking changes
Details
@github-actions
Publish to local Verdaccio registry
Details
@github-actions
Lint
Details
@github-actions
Test on Node.js (14)
Details
@github-actions
Test on Node.js (12)
Details
@github-actions
Test on Node.js (10)
Details
@github-actions
Test on Node.js (8)
Details
@github-actions
Test on Node.js (6)
Details
@github-actions
Test on Windows
Details
@github-actions
Third-party Parser Tests
Details
@github-actions
Test @babel/runtime integrations
Details
@github-actions
E2E (babel)
Details
@github-actions
E2E (babel-old-version)
Details
@github-actions
E2E (create-react-app)
Details
@github-actions
E2E (vue-cli)
Details
@github-actions
E2E (jest)
Details
@circleci-checks
e2e-breaking-pr Workflow: e2e-breaking-pr
Details
@circleci-checks
test262-pr Workflow: test262-pr
Details
@gitpod-io
Gitpod Open an online workspace in Gitpod
Details
@babel-bot
babel/repl REPL preview is available
Details
@circleci-checks
build-standalone Workflow: build-standalone
Details
@codesandbox
ci/codesandbox Building packages succeeded.
Details
@codecov
codecov/project 91.44% (target 90.00%)
Details
@JLHwung JLHwung deleted the JLHwung:fix-13001 branch Mar 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

4 participants