diff --git a/packages/babel-plugin-minify-constant-folding/__tests__/constant-folding-test.js b/packages/babel-plugin-minify-constant-folding/__tests__/constant-folding-test.js index 60340b73a..2cacfd768 100644 --- a/packages/babel-plugin-minify-constant-folding/__tests__/constant-folding-test.js +++ b/packages/babel-plugin-minify-constant-folding/__tests__/constant-folding-test.js @@ -226,4 +226,16 @@ describe("constant-folding-plugin", () => { ({}).toString; ` ); + + thePlugin( + "should bail for spread element in array", + ` + function foo() { + return [...iter].length; + } + function bar() { + return [...iter][0]; + } + ` + ); }); diff --git a/packages/babel-plugin-minify-constant-folding/src/replacements.js b/packages/babel-plugin-minify-constant-folding/src/replacements.js index 0f6300d66..0cbc591b0 100644 --- a/packages/babel-plugin-minify-constant-folding/src/replacements.js +++ b/packages/babel-plugin-minify-constant-folding/src/replacements.js @@ -23,9 +23,15 @@ module.exports = ({ types: t }) => { ArrayExpression: { members: { length() { + if (this.elements.some(el => t.isSpreadElement(el))) { + return; + } return t.numericLiteral(this.elements.length); }, [FALLBACK_HANDLER](i) { + if (this.elements.some(el => t.isSpreadElement(el))) { + return; + } if (typeof i === "number" || i.match(/^\d+$/)) { return this.elements[i] || undef; }