Referencing a global is assumed to have side effects.
authorMihai Bazon <mihai@bazon.net>
Sun, 28 Sep 2014 08:16:51 +0000 (11:16 +0300)
committerMihai Bazon <mihai@bazon.net>
Sun, 28 Sep 2014 08:18:25 +0000 (11:18 +0300)
Close #550

lib/compress.js
test/compress/conditionals.js
test/compress/sequences.js

index 77de594..e63ce14 100644 (file)
@@ -897,7 +897,9 @@ merge(Compressor.prototype, {
                 || this.operator == "--"
                 || this.expression.has_side_effects(compressor);
         });
-        def(AST_SymbolRef, function(compressor){ return false });
+        def(AST_SymbolRef, function(compressor){
+            return this.global() && this.undeclared();
+        });
         def(AST_Object, function(compressor){
             for (var i = this.properties.length; --i >= 0;)
                 if (this.properties[i].has_side_effects(compressor))
index c20297a..2212f85 100644 (file)
@@ -239,6 +239,7 @@ cond_7: {
         evaluate    : true
     };
     input: {
+        var x, y, z, a, b;
         // compress these
         if (y) {
             x = 1+1;
@@ -281,6 +282,7 @@ cond_7: {
         x = y ? 'foo' : 'fo';
     }
     expect: {
+        var x, y, z, a, b;
         x = 2;
         x = 2;
         x = 'foo';
@@ -293,3 +295,20 @@ cond_7: {
     }
 }
 
+cond_7_1: {
+    options = {
+        conditionals: true,
+        evaluate    : true
+    };
+    input: {
+        // access to global should be assumed to have side effects
+        if (y) {
+            x = 1+1;
+        } else {
+            x = 2;
+        }
+    }
+    expect: {
+        x = (y, 2);
+    }
+}
index 4669571..0e3319a 100644 (file)
@@ -91,9 +91,11 @@ make_sequences_4: {
 lift_sequences_1: {
     options = { sequences: true };
     input: {
+        var foo, x, y, bar;
         foo = !(x(), y(), bar());
     }
     expect: {
+        var foo, x, y, bar;
         x(), y(), foo = !bar();
     }
 }
@@ -101,10 +103,12 @@ lift_sequences_1: {
 lift_sequences_2: {
     options = { sequences: true, evaluate: true };
     input: {
+        var foo, bar;
         foo.x = (foo = {}, 10);
         bar = (bar = {}, 10);
     }
     expect: {
+        var foo, bar;
         foo.x = (foo = {}, 10),
         bar = {}, bar = 10;
     }
@@ -113,9 +117,11 @@ lift_sequences_2: {
 lift_sequences_3: {
     options = { sequences: true, conditionals: true };
     input: {
+        var x, foo, bar, baz;
         x = (foo(), bar(), baz()) ? 10 : 20;
     }
     expect: {
+        var x, foo, bar, baz;
         foo(), bar(), x = baz() ? 10 : 20;
     }
 }
@@ -123,9 +129,11 @@ lift_sequences_3: {
 lift_sequences_4: {
     options = { side_effects: true };
     input: {
+        var x, foo, bar, baz;
         x = (foo, bar, baz);
     }
     expect: {
+        var x, foo, bar, baz;
         x = baz;
     }
 }