From ca943e7343e2f84ffb5eee36d07ff963885358d7 Mon Sep 17 00:00:00 2001 From: Boopathi Rajaa Date: Fri, 30 Jun 2017 17:59:05 +0200 Subject: [PATCH] Fix folding Array literals (close #608) (close #609) --- .../__tests__/constant-folding-test.js | 12 ++++++++++++ .../src/replacements.js | 6 ++++++ 2 files changed, 18 insertions(+) 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; }