babel-plugin-minify-type-constructors
Advanced tools
Comparing version 0.4.0-alpha.6546ad11 to 0.4.0-alpha.caaefb4c
"use strict"; | ||
function replaceArray(t, path) { | ||
var node = path.node; | ||
// arguments is taken :( | ||
var node = path.node; // arguments is taken :( | ||
var constructorArgs = path.get("arguments"); | ||
if (t.isIdentifier(node.callee, { name: "Array" }) && !path.scope.getBinding("Array")) { | ||
if (t.isIdentifier(node.callee, { | ||
name: "Array" | ||
}) && !path.scope.getBinding("Array")) { | ||
if (constructorArgs.length === 0) { | ||
@@ -30,2 +32,3 @@ // Array() -> [] | ||
var transformables = ["ArrayExpression", "ObjectExpression", "FunctionExpression", "ArrowFunctionExpression", "ClassExpression"]; | ||
if (transformables.indexOf(arg.node.type) !== -1) { | ||
@@ -44,2 +47,3 @@ // Array([]), Array({}) | ||
} | ||
return true; | ||
@@ -58,31 +62,24 @@ } | ||
if (t.isIdentifier(node.callee, { name: "Object" }) && !path.scope.getBinding("Object")) { | ||
if (t.isIdentifier(node.callee, { | ||
name: "Object" | ||
}) && !path.scope.getBinding("Object")) { | ||
var isVoid0 = require("babel-helper-is-void-0")(t); | ||
var arg = node.arguments[0]; | ||
var binding = arg && t.isIdentifier(arg) && path.scope.getBinding(arg.name); | ||
var binding = arg && t.isIdentifier(arg) && path.scope.getBinding(arg.name); // Object() -> {} | ||
// Object() -> {} | ||
if (node.arguments.length === 0) { | ||
path.replaceWith(t.objectExpression([])); | ||
// Object([]) -> [] | ||
path.replaceWith(t.objectExpression([])); // Object([]) -> [] | ||
} else if (arg.type === "ArrayExpression" || t.isFunctionExpression(arg)) { | ||
path.replaceWith(arg); | ||
// Object(null) -> {} | ||
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 | ||
path.replaceWith(t.objectExpression([])); // Object(localFn) -> localFn | ||
} else if (binding && binding.path.isFunction()) { | ||
path.replaceWith(arg); | ||
// Object({a:b}) -> {a:b} | ||
path.replaceWith(arg); // Object({a:b}) -> {a:b} | ||
} else if (arg.type === "ObjectExpression") { | ||
path.replaceWith(arg); | ||
// new Object(a) -> Object(a) | ||
path.replaceWith(arg); // new Object(a) -> Object(a) | ||
} else if (node.type === "NewExpression") { | ||
path.replaceWith(t.callExpression(node.callee, node.arguments)); | ||
} | ||
return true; | ||
@@ -95,11 +92,11 @@ } | ||
_ref$boolean = _ref.boolean, | ||
boolean = _ref$boolean === undefined ? true : _ref$boolean, | ||
boolean = _ref$boolean === void 0 ? true : _ref$boolean, | ||
_ref$number = _ref.number, | ||
number = _ref$number === undefined ? true : _ref$number, | ||
number = _ref$number === void 0 ? true : _ref$number, | ||
_ref$string = _ref.string, | ||
string = _ref$string === undefined ? true : _ref$string, | ||
string = _ref$string === void 0 ? true : _ref$string, | ||
_ref$array = _ref.array, | ||
array = _ref$array === undefined ? true : _ref$array, | ||
array = _ref$array === void 0 ? true : _ref$array, | ||
_ref$object = _ref.object, | ||
object = _ref$object === undefined ? true : _ref$object; | ||
object = _ref$object === void 0 ? true : _ref$object; | ||
@@ -117,3 +114,2 @@ return { | ||
var t = _ref2.types; | ||
return { | ||
@@ -124,29 +120,33 @@ name: "minify-type-constructors", | ||
var node = path.node; | ||
var opts = defaults(this.opts); // Boolean(foo) -> !!foo | ||
var opts = defaults(this.opts); | ||
// Boolean(foo) -> !!foo | ||
if (opts.boolean && t.isIdentifier(node.callee, { name: "Boolean" }) && node.arguments.length === 1 && !path.scope.getBinding("Boolean")) { | ||
if (opts.boolean && t.isIdentifier(node.callee, { | ||
name: "Boolean" | ||
}) && node.arguments.length === 1 && !path.scope.getBinding("Boolean")) { | ||
path.replaceWith(t.unaryExpression("!", t.unaryExpression("!", node.arguments[0], true), true)); | ||
return; | ||
} | ||
} // Number(foo) -> +foo | ||
// Number(foo) -> +foo | ||
if (opts.number && t.isIdentifier(node.callee, { name: "Number" }) && node.arguments.length === 1 && !path.scope.getBinding("Number")) { | ||
if (opts.number && t.isIdentifier(node.callee, { | ||
name: "Number" | ||
}) && node.arguments.length === 1 && !path.scope.getBinding("Number")) { | ||
path.replaceWith(t.unaryExpression("+", node.arguments[0], true)); | ||
return; | ||
} | ||
} // String(foo) -> foo + '' | ||
// String(foo) -> foo + '' | ||
if (opts.string && t.isIdentifier(node.callee, { name: "String" }) && node.arguments.length === 1 && !path.scope.getBinding("String")) { | ||
if (opts.string && t.isIdentifier(node.callee, { | ||
name: "String" | ||
}) && node.arguments.length === 1 && !path.scope.getBinding("String")) { | ||
path.replaceWith(t.binaryExpression("+", node.arguments[0], t.stringLiteral(""))); | ||
return; | ||
} | ||
} // Array() -> [] | ||
// Array() -> [] | ||
if (opts.array && replaceArray(t, path)) { | ||
return; | ||
} | ||
} // Object() -> {} | ||
// Object() -> {} | ||
if (opts.object && replaceObject(t, path)) { | ||
@@ -156,11 +156,11 @@ return; | ||
}, | ||
NewExpression(path) { | ||
var opts = defaults(this.opts); | ||
var opts = defaults(this.opts); // new Array() -> [] | ||
// new Array() -> [] | ||
if (opts.array && replaceArray(t, path)) { | ||
return; | ||
} | ||
} // new Object() -> {} | ||
// new Object() -> {} | ||
if (opts.object && replaceObject(t, path)) { | ||
@@ -170,4 +170,5 @@ return; | ||
} | ||
} | ||
}; | ||
}; |
{ | ||
"name": "babel-plugin-minify-type-constructors", | ||
"version": "0.4.0-alpha.6546ad11", | ||
"version": "0.4.0-alpha.caaefb4c", | ||
"description": "", | ||
@@ -15,4 +15,4 @@ "keywords": [ | ||
"dependencies": { | ||
"babel-helper-is-void-0": "^0.4.0-alpha.6546ad11" | ||
"babel-helper-is-void-0": "^0.4.0-alpha.caaefb4c" | ||
} | ||
} |
@@ -56,3 +56,3 @@ # babel-plugin-minify-type-constructors | ||
```javascript | ||
require("babel-core").transform("code", { | ||
require("@babel/core").transform("code", { | ||
plugins: ["minify-type-constructors"] | ||
@@ -59,0 +59,0 @@ }); |
6820
136