Skip to content

Commit

Permalink
Transform: Object() -> {}
Browse files Browse the repository at this point in the history
  • Loading branch information
bfred-it committed Jul 10, 2016
1 parent 036eb93 commit 9a84e1b
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,96 @@ describe("type-constructors-plugin", () => {
expect(transform(source)).toBe(expected);
});


it("should turn Object() to {}", () => {
const source = "var x = Object();";
const expected = "var x = {};";
expect(transform(source)).toBe(expected);
});

it("should turn new Object() to {}", () => {
const source = "var x = new Object();";
const expected = "var x = {};";
expect(transform(source)).toBe(expected);
});

it("should change Object(null|undefined|{}) to {}", () => {
const source = unpad(`
[
Object(null),
Object(undefined),
new Object(void 0),
Object({})
]
`);
const expected = "[{}, {}, {}, {}];";
expect(transform(source)).toBe(expected);
});

it("should change Object([]) to []", () => {
const source = unpad(`
[
Object([]),
Object([1]),
Object([1,2]),
new Object([null])
]
`);// todo: add Object(Array())
const expected = "[[], [1], [1, 2], [null]];";
expect(transform(source)).toBe(expected);
});

it("should change Object(localFn) to localFn", () => {
const source = unpad(`
function a() {};
[
Object(function () {}),
new Object(a),
Object(Array)
]
`);
const expected = unpad(`
function a() {};
[function () {}, a, Object(Array)];
`);
expect(transform(source)).toBe(expected);
});

it("shouldn't change Object(value) for unrecognized values", () => {
const source = unpad(`
[
Object("undefined"),
Object(nulled),
Object(false)
]
`);
const expected = "[Object(\"undefined\"), Object(nulled), Object(false)];";
expect(transform(source)).toBe(expected);
});

it("should change new Object(value) to Object(value) for unrecognized values", () => {
const source = unpad(`
[
new Object("function"),
new Object(Symbol),
new Object(Array),
new Object(true)
]
`);
const expected = "[Object(\"function\"), Object(Symbol), Object(Array), Object(true)];";
expect(transform(source)).toBe(expected);
});

it("shouldn't change referenced identifiers", () => {
const source = unpad(`
(function (Boolean, String, Number, Array) {
return Boolean(a), String(b), Number(c), Array(d);
})(MyBoolean, MyString, MyNumber, MyArray);
(function (Boolean, String, Number, Array, Object) {
return Boolean(a), String(b), Number(c), Array(d), Object(d);
})(MyBoolean, MyString, MyNumber, MyArray, MyObject);
`);
const expected = unpad(`
(function (Boolean, String, Number, Array) {
return Boolean(a), String(b), Number(c), Array(d);
})(MyBoolean, MyString, MyNumber, MyArray);
(function (Boolean, String, Number, Array, Object) {
return Boolean(a), String(b), Number(c), Array(d), Object(d);
})(MyBoolean, MyString, MyNumber, MyArray, MyObject);
`);
expect(transform(source)).toBe(expected);
});
Expand Down
4 changes: 3 additions & 1 deletion packages/babel-plugin-minify-type-constructors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"keywords": [
"babel-plugin"
],
"dependencies": {},
"dependencies": {
"babel-helper-is-void-0": "^0.0.0"
},
"devDependencies": {}
}
47 changes: 47 additions & 0 deletions packages/babel-plugin-minify-type-constructors/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,43 @@ function replaceArray(t, path) {
}
}

function replaceObject(t, path) {
const { node } = path;
if (t.isIdentifier(node.callee, { name: "Object" }) &&
!path.scope.getBinding("Object")) {

const isVoid0 = require("babel-helper-is-void-0")(t);
const arg = node.arguments[0];
const binding = arg && t.isIdentifier(arg) && path.scope.getBinding(arg.name);

// Object() -> {}
if (node.arguments.length === 0) {
path.replaceWith(t.objectExpression([]));

// Object([]) -> []
} else if (arg.type === "ArrayExpression" ||
t.isFunctionExpression(arg)) {
path.replaceWith(arg);

// Object(null) -> {}
} else if (isVoid0(arg) ||
arg.name === "undefined" ||
arg.type === "NullLiteral" ||
arg.type === "ObjectExpression" && arg.properties.length === 0) {
path.replaceWith(t.objectExpression([]));

// Object(localFn) -> localFn
} else if (binding && binding.path.isFunction()) {
path.replaceWith(arg);

// new Object(a) -> Object(a)
} else if (node.type === "NewExpression") {
path.replaceWith(t.callExpression(node.callee, node.arguments));
}
return true;
}
}

module.exports = function({ types: t }) {
return {
visitor: {
Expand Down Expand Up @@ -60,12 +97,22 @@ module.exports = function({ types: t }) {
if (replaceArray(t, path)) {
return;
}

// Object() -> {}
if (replaceObject(t, path)) {
return;
}
},
NewExpression(path) {
// new Array() -> []
if (replaceArray(t, path)) {
return;
}

// new Object() -> {}
if (replaceObject(t, path)) {
return;
}
},
},
};
Expand Down

0 comments on commit 9a84e1b

Please sign in to comment.