Skip to content

Correctly transform spreads of arrays with holes#13439

Merged
nicolo-ribaudo merged 4 commits into
babel:mainfrom
nicolo-ribaudo:fix-transform-spread-arrays-with-holes
Jun 10, 2021
Merged

Correctly transform spreads of arrays with holes#13439
nicolo-ribaudo merged 4 commits into
babel:mainfrom
nicolo-ribaudo:fix-transform-spread-arrays-with-holes

Conversation

@nicolo-ribaudo

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

Copy link
Copy Markdown
Member
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

codesandbox-ci Bot commented Jun 9, 2021

Copy link
Copy Markdown

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

babel-bot commented Jun 9, 2021

Copy link
Copy Markdown
Collaborator

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

@nicolo-ribaudo nicolo-ribaudo Jun 9, 2021

Copy link
Copy Markdown
Member Author

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