From f2ec0ae0acd8ac5505fb51ecdc0b021efadd8310 Mon Sep 17 00:00:00 2001 From: Boopathi Rajaa Date: Sun, 23 Oct 2016 19:50:30 +0200 Subject: [PATCH] Fix bug where invalid array length throws (close #206) (#215) * Fix bug where invalid array length throws (close #206) * Add >=0 condition too --- .../__tests__/type-constructors-test.js | 13 +++++++++++++ .../src/index.js | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/babel-plugin-minify-type-constructors/__tests__/type-constructors-test.js b/packages/babel-plugin-minify-type-constructors/__tests__/type-constructors-test.js index 6576fb18c..e7ea41be0 100644 --- a/packages/babel-plugin-minify-type-constructors/__tests__/type-constructors-test.js +++ b/packages/babel-plugin-minify-type-constructors/__tests__/type-constructors-test.js @@ -229,4 +229,17 @@ describe("type-constructors-plugin", () => { `); expect(transform(source)).toBe(expected); }); + + // https://github.com/babel/babili/issues/206 + it("should handle floating point numbers in Array()", () => { + const source = unpad(` + new Array(-0.01); + new Array(-1); + `); + const expected = unpad(` + Array(-0.01); + Array(-1); + `); + expect(transform(source)).toBe(expected); + }); }); diff --git a/packages/babel-plugin-minify-type-constructors/src/index.js b/packages/babel-plugin-minify-type-constructors/src/index.js index 658076166..8fc452f1e 100644 --- a/packages/babel-plugin-minify-type-constructors/src/index.js +++ b/packages/babel-plugin-minify-type-constructors/src/index.js @@ -16,7 +16,7 @@ function replaceArray(t, path) { if (result.confident) { if (typeof result.value === "number") { - if (result.value <= 6) { + if (result.value >= 0 && result.value <= 6 && result.value % 1 === 0) { // "Array(7)" is shorter than "[,,,,,,,]" path.replaceWith(t.arrayExpression(Array(result.value).fill(null))); } else {