Skip to content

Commit

Permalink
Fix Array(x) to [x] bug - (close #72) (#73)
Browse files Browse the repository at this point in the history
* Fix Array(x) to [x] bug - (close #72)

* Add more transformations for Array constructor

* Add a functions to the list of transformables
  • Loading branch information
boopathi authored and kangax committed Aug 2, 2016
1 parent 8ee1a80 commit 5ee6e67
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,23 @@ describe("type-constructors-plugin", () => {
Array("Rome");
Array(false);
Array(null);
Array({});
new Array({});
new Array([a, b]);
Array([]);
Array(t);
new Array(a.b);
new Array((() => 5)());
`);
const expected = unpad(`
["Rome"];
[false];
[null];
[{}];
[[a, b]];
[[]];
Array(t);
Array(a.b);
Array((() => 5)());
`);
expect(transform(source)).toBe(expected);
});
Expand All @@ -63,7 +71,7 @@ describe("type-constructors-plugin", () => {
const source = unpad(`
Array(0);
Array(1);
Array(6);
Array(2 + 4);
Array(7);
`);
const expected = unpad(`
Expand Down
57 changes: 44 additions & 13 deletions packages/babel-plugin-minify-type-constructors/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,59 @@

function replaceArray(t, path) {
const { node } = path;
// arguments is taken :(
const constructorArgs = path.get("arguments");
if (t.isIdentifier(node.callee, { name: "Array" }) &&
!path.scope.getBinding("Array")) {

// Array(5) -> [,,,,,]
if (node.arguments.length === 1 &&
typeof node.arguments[0].value === "number") {

// "Array(7)" is shorter than "[,,,,,,,]"
if (node.arguments[0].value <= 6) {
path.replaceWith(t.arrayExpression(Array(node.arguments[0].value).fill(null)));

// new Array(7) -> Array(7)
} else if (node.type === "NewExpression") {
path.replaceWith(t.callExpression(node.callee, node.arguments));
if (constructorArgs.length === 0) {
// Array() -> []
path.replaceWith(t.arrayExpression([]));
} else if (constructorArgs.length === 1) {
const arg = constructorArgs[0];
const result = arg.evaluate();

if (result.confident) {
if (typeof result.value === "number") {
if (result.value <= 6) {
// "Array(7)" is shorter than "[,,,,,,,]"
path.replaceWith(t.arrayExpression(Array(result.value).fill(null)));
} else {
dropNewIfPresent();
}
} else {
// Array("Asdf"), Array(true), Array(false)
path.replaceWith(t.arrayExpression([t.valueToNode(result.value)]));
}
} else {
const transformables = [
"ArrayExpression",
"ObjectExpression",
"FunctionExpression",
"ArrowFunctionExpression",
"ClassExpression"
];
if (transformables.includes(arg.node.type)) {
// Array([]), Array({})
// Array(()=>{}), Array(class{}), Array(function(){})
path.replaceWith(t.arrayExpression([arg.node]));
} else {
// Array(x); Array(a.b);
dropNewIfPresent();
}
}

// Array("Hello") -> ["Hello"]
} else {
// Array(2,3), Array(a,b) => [2,3], [a,b]
path.replaceWith(t.arrayExpression(node.arguments));
}
return true;
}

function dropNewIfPresent() {
if (path.isNewExpression()) {
path.replaceWith(t.callExpression(node.callee, node.arguments));
}
}
}

function replaceObject(t, path) {
Expand Down

0 comments on commit 5ee6e67

Please sign in to comment.