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

Correctly transform spreads of arrays with holes #13439

Conversation

nicolo-ribaudo
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo commented Jun 9, 2021

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

This is #9146 rebased. I couldn't push to the original PR because the fork has been deleted.

@nicolo-ribaudo nicolo-ribaudo added the PR: Bug Fix 🐛 A type of pull request used for our changelog categories label Jun 9, 2021
@codesandbox-ci
Copy link

codesandbox-ci bot commented Jun 9, 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 c685177:

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

@babel-bot
Copy link
Collaborator

babel-bot commented Jun 9, 2021

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

@nicolo-ribaudo nicolo-ribaudo force-pushed the fix-transform-spread-arrays-with-holes branch from cd73f2e to f604e84 Compare June 9, 2021 16:44

if (t.isArrayExpression(spreadLiteral) && hasHole(spreadLiteral)) {
spreadLiteral = t.callExpression(
file.addHelper("arrayWithoutHoles"),
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: we can call arrayLikeToArray since we know for sure spreadLiteral is an array.

Copy link
Member Author

Choose a reason for hiding this comment

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

arrayLikeToArray isn't available in every Babel 7 version, I went for the easy route. I'll add a switch to use arrayLikeToArray in Babel 8.

@@ -0,0 +1,2 @@
let arr = ['a', ...['b',,'c']];
Copy link
Member

Choose a reason for hiding this comment

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

What happens if this isn't statically analyzable?

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 uses toConsumableArray(spreadArgument), which does all the necessary checks to ensure that it's correct. arrayWithoutHoles is part of those checks.

Copy link
Member Author

@nicolo-ribaudo nicolo-ribaudo Jun 9, 2021

Choose a reason for hiding this comment

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

@nicolo-ribaudo nicolo-ribaudo force-pushed the fix-transform-spread-arrays-with-holes branch from 443bc8d to c685177 Compare June 9, 2021 17:30
@nicolo-ribaudo nicolo-ribaudo merged commit e914d12 into babel:main Jun 10, 2021
@nicolo-ribaudo nicolo-ribaudo deleted the fix-transform-spread-arrays-with-holes branch June 10, 2021 10:34
@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 Sep 10, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
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.

Hole array spread inside of another array isn't work correctly
6 participants