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

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
23 changes: 20 additions & 3 deletions packages/babel-plugin-transform-spread/src/index.js
Expand Up @@ -20,6 +20,10 @@ export default declare((api, options) => {
}
}

function hasHole(spread) {
return spread.elements.some(el => el === null);
}

function hasSpread(nodes) {
for (let i = 0; i < nodes.length; i++) {
if (t.isSpreadElement(nodes[i])) {
Expand All @@ -35,14 +39,27 @@ export default declare((api, options) => {
return [];
}

function build(props: Array, scope) {
function build(props: Array, scope, file) {
const nodes = [];
let _props = [];

for (const prop of props) {
if (t.isSpreadElement(prop)) {
_props = push(_props, nodes);
nodes.push(getSpreadLiteral(prop, scope));
let spreadLiteral = getSpreadLiteral(prop, scope);

if (t.isArrayExpression(spreadLiteral) && hasHole(spreadLiteral)) {
spreadLiteral = t.callExpression(
file.addHelper(
process.env.BABEL_8_BREAKING
? "arrayLikeToArray"
: "arrayWithoutHoles",
),
[spreadLiteral],
);
}

nodes.push(spreadLiteral);
} else {
_props.push(prop);
}
Expand All @@ -62,7 +79,7 @@ export default declare((api, options) => {
const elements = node.elements;
if (!hasSpread(elements)) return;

const nodes = build(elements, scope);
const nodes = build(elements, scope, this);
let first = nodes[0];

// If there is only one element in the ArrayExpression and
Expand Down
@@ -0,0 +1,2 @@
let arr = ['a', ...['b',,'c']];
expect(2 in arr).toBe(true);
@@ -0,0 +1,2 @@
var a = [...['a',, 'b']];
var b = ['a', ...['b',, 'c']];
@@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": false
}
@@ -0,0 +1,2 @@
var a = babelHelpers.arrayWithoutHoles(['a',, 'b']);
var b = ['a'].concat(babelHelpers.arrayWithoutHoles(['b',, 'c']));
@@ -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

Choose a reason for hiding this comment

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

expect(2 in arr).toBe(true);
@@ -0,0 +1,2 @@
var a = [...['a',, 'b']];
var b = ['a', ...['b',, 'c']];
@@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": true
}
@@ -0,0 +1,2 @@
var a = babelHelpers.arrayLikeToArray(['a',, 'b']);
var b = ['a'].concat(babelHelpers.arrayLikeToArray(['b',, 'c']));