From 2b59abfd5adcd15470fcece470eef468044b6b03 Mon Sep 17 00:00:00 2001 From: Dan Wolff Date: Fri, 20 Sep 2013 06:24:25 +0200 Subject: [PATCH 1/2] Evaluate [...].join() if possible: minor bugfix Follow-up to 78e98d2. --- lib/compress.js | 7 +++---- test/compress/arrays.js | 2 ++ 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 35646cf1..abf2001d 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -655,6 +655,8 @@ merge(Compressor.prototype, { throw def; }); function ev(node, compressor) { + if (!compressor) throw new Error("Compressor must be passed"); + return node._eval(compressor); }; def(AST_Node, function(){ @@ -700,9 +702,6 @@ merge(Compressor.prototype, { case "+" : // handle concatenating strings even if the left part cannot // be evaluated, e.g. (variable + "str") + "str" - if (!(c instanceof Compressor)) { - throw new Error("Compressor must be passed!!"); - } if (left instanceof AST_Binary && left.operator == "+" && left.is_string(c)) { return make_node(AST_Binary, this, { operator: "+", @@ -754,7 +753,7 @@ merge(Compressor.prototype, { var x = this.expression.expression.elements.map(function(el){ return ev(el, compressor); }); - return x.join(ev(this.args[0])); + return x.join(ev(this.args[0], compressor)); } } throw def; diff --git a/test/compress/arrays.js b/test/compress/arrays.js index 214928dd..06e03ae4 100644 --- a/test/compress/arrays.js +++ b/test/compress/arrays.js @@ -23,11 +23,13 @@ constant_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("-"); + var e = [].join(foo + bar); } 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"; + var e = [].join(foo + bar); } } From 1588538bc14e6685babb75a0766de46fe39559e0 Mon Sep 17 00:00:00 2001 From: Dan Wolff Date: Sat, 21 Sep 2013 23:18:35 +0200 Subject: [PATCH 2/2] Compress [...].join() also when it cannot be completely evaluated --- lib/compress.js | 31 +++++++++++++++++++++++++++---- test/compress/arrays.js | 14 +++++++++----- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index abf2001d..f6c7f833 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -750,10 +750,33 @@ merge(Compressor.prototype, { 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], compressor)); + + var join_arg = this.args[0] ? "" + ev(this.args[0], compressor) : ","; + try { + var x = this.expression.expression.elements.map(function(el){ + return ev(el, compressor); + }); + return x.join(join_arg); + } catch (ex) { + if (ex !== def) throw ex; + join_arg = make_node_from_constant(compressor, join_arg, this.args[0]); + var x = this.expression.expression.elements.reduce(function(el1, el2){ + return make_node(AST_Binary, null, { + operator : "+", + left : make_node(AST_Binary, null, { + operator : "+", + left : el1, + right : join_arg, + start : el1.start, + end : join_arg.end + }), + right : el2, + start : el1.start, + end : el2.end + }); + }); + return x.transform(compressor); + } } } throw def; diff --git a/test/compress/arrays.js b/test/compress/arrays.js index 06e03ae4..f0f8eb17 100644 --- a/test/compress/arrays.js +++ b/test/compress/arrays.js @@ -19,17 +19,21 @@ constant_join: { evaluate : true }; input: { - var a = [ "foo", "bar", "baz" ].join(""); + 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("-"); - var e = [].join(foo + bar); + var e = [ boo ].join(foo + bar); + var f = [ boo ].join(""); + var g = [ "foo", "boo" + bar, "baz"].join("-"); } expect: { - var a = "foobarbaz"; + var a = "foo,bar,baz"; var b = "foo123bar"; - var c = [ boo(), "foo", 1, 2, 3, "bar", bar() ].join(""); // we could still shorten this one, but oh well. + var c = boo() + "foo123bar" + bar(); var d = "foo-3bar-baz"; - var e = [].join(foo + bar); + var e = [ boo ].join(foo + bar); + var f = [ boo ].join(); + var g = "foo-" + ("boo" + bar) + "-baz"; // we could still shorten this one, but oh well. } }