aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMihai Bazon <mihai.bazon@gmail.com>2013-09-19 18:20:45 +0300
committerMihai Bazon <mihai.bazon@gmail.com>2013-09-19 18:20:45 +0300
commit78e98d2611f5698797785adbd12faf2ba46ec783 (patch)
tree6bace696ebb22da18ef3fb06289ffee963ffb9dc
parent8d14efe818d9a33821a6acc26b612aaf1fd6e967 (diff)
downloadtracifyjs-78e98d2611f5698797785adbd12faf2ba46ec783.tar.gz
tracifyjs-78e98d2611f5698797785adbd12faf2ba46ec783.zip
When `unsafe` is set, evaluate [...].join() if possible
Close #298
-rw-r--r--lib/compress.js15
-rw-r--r--test/compress/arrays.js19
2 files changed, 33 insertions, 1 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 8bd58bb1..35646cf1 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -746,6 +746,19 @@ merge(Compressor.prototype, {
if (d && d.constant && d.init) return ev(d.init, compressor);
throw def;
});
+ def(AST_Call, function(compressor){
+ if (compressor.option("unsafe")) {
+ if (this.expression instanceof AST_Dot
+ && this.expression.expression instanceof AST_Array
+ && this.expression.property == "join") {
+ var x = this.expression.expression.elements.map(function(el){
+ return ev(el, compressor);
+ });
+ return x.join(ev(this.args[0]));
+ }
+ }
+ throw def;
+ });
})(function(node, func){
node.DEFMETHOD("_eval", func);
});
@@ -1704,7 +1717,7 @@ merge(Compressor.prototype, {
return make_node(AST_Undefined, self).transform(compressor);
}
}
- return self;
+ return self.evaluate(compressor)[0];
});
OPT(AST_New, function(self, compressor){
diff --git a/test/compress/arrays.js b/test/compress/arrays.js
index 0c3d8ba2..214928dd 100644
--- a/test/compress/arrays.js
+++ b/test/compress/arrays.js
@@ -12,3 +12,22 @@ holes_and_undefined: {
z=[1,void 0,3];
}
}
+
+constant_join: {
+ options = {
+ unsafe : true,
+ evaluate : true
+ };
+ input: {
+ var a = [ "foo", "bar", "baz" ].join("");
+ var b = [ "foo", 1, 2, 3, "bar" ].join("");
+ var c = [ boo(), "foo", 1, 2, 3, "bar", bar() ].join("");
+ var d = [ "foo", 1 + 2 + "bar", "baz" ].join("-");
+ }
+ expect: {
+ var a = "foobarbaz";
+ var b = "foo123bar";
+ var c = [ boo(), "foo", 1, 2, 3, "bar", bar() ].join(""); // we could still shorten this one, but oh well.
+ var d = "foo-3bar-baz";
+ }
+}