Evaluate [...].join() if possible: minor bugfix
authorDan Wolff <dan.d.wolff@gmail.com>
Fri, 20 Sep 2013 04:24:25 +0000 (06:24 +0200)
committerMihai Bazon <mihai@bazon.net>
Sun, 22 Sep 2013 08:34:30 +0000 (11:34 +0300)
Follow-up to 78e98d2.

lib/compress.js
test/compress/arrays.js

index 35646cf..abf2001 100644 (file)
@@ -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;
index 214928d..06e03ae 100644 (file)
@@ -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);
     }
 }