Skip to content

Commit

Permalink
Fix bug where invalid array length throws (close #206) (#215)
Browse files Browse the repository at this point in the history
* Fix bug where invalid array length throws (close #206)

* Add >=0 condition too
  • Loading branch information
boopathi authored and kangax committed Oct 23, 2016
1 parent e944dcb commit f2ec0ae
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f2ec0ae

Please sign in to comment.