aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRichard van Velzen <rvanvelzen1@gmail.com>2013-10-24 11:08:33 +0200
committerRichard van Velzen <rvanvelzen1@gmail.com>2013-10-24 11:08:33 +0200
commit1a5a4bd63185e38eb550a9b0ca85890002d5243a (patch)
tree2ba5995f6e369d336bae43b243055dd5a7f88ece /lib
parent63e1a8e1fd17958557f41a55ea08d678b7995f9b (diff)
downloadtracifyjs-1a5a4bd63185e38eb550a9b0ca85890002d5243a.tar.gz
tracifyjs-1a5a4bd63185e38eb550a9b0ca85890002d5243a.zip
Fix #269
Shorten most primitives where possible. Also optimize some edge cases.
Diffstat (limited to 'lib')
-rw-r--r--lib/compress.js33
1 files changed, 31 insertions, 2 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 083b5d29..121e312a 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -1620,7 +1620,7 @@ merge(Compressor.prototype, {
if (self.args.length != 1) {
return make_node(AST_Array, self, {
elements: self.args
- });
+ }).transform(compressor);
}
break;
case "Object":
@@ -1634,11 +1634,30 @@ merge(Compressor.prototype, {
if (self.args.length == 0) return make_node(AST_String, self, {
value: ""
});
- return make_node(AST_Binary, self, {
+ if (self.args.length <= 1) return make_node(AST_Binary, self, {
left: self.args[0],
operator: "+",
right: make_node(AST_String, self, { value: "" })
+ }).transform(compressor);
+ break;
+ case "Number":
+ if (self.args.length == 0) return make_node(AST_Number, self, {
+ value: 0
});
+ if (self.args.length == 1) return make_node(AST_UnaryPrefix, self, {
+ expression: self.args[0],
+ operator: "+"
+ }).transform(compressor);
+ case "Boolean":
+ if (self.args.length == 0) return make_node(AST_False, self);
+ if (self.args.length == 1) return make_node(AST_UnaryPrefix, self, {
+ expression: make_node(AST_UnaryPrefix, null, {
+ expression: self.args[0],
+ operator: "!"
+ }),
+ operator: "!"
+ }).transform(compressor);
+ break;
case "Function":
if (all(self.args, function(x){ return x instanceof AST_String })) {
// quite a corner-case, but we can handle it:
@@ -1999,6 +2018,16 @@ merge(Compressor.prototype, {
&& self.right.getValue() === "" && self.left instanceof AST_Binary
&& self.left.operator == "+" && self.left.is_string(compressor)) {
return self.left;
+ } else if (self.operator == "+" && self.right instanceof AST_String
+ && self.right.getValue() === "" && self.left instanceof AST_Binary
+ && self.left.operator == "+" && self.left.right instanceof AST_Number) {
+ return make_node(AST_Binary, self, {
+ left: self.left.left,
+ operator: "+",
+ right: make_node(AST_String, self.right, {
+ value: String(self.left.right.value)
+ })
+ });
}
if (compressor.option("evaluate")) {
if (self.operator == "+") {