[Pkg-javascript-commits] [uglifyjs] 83/228: collapse_vars: do not replace a constant in loop condition or init (#1562)

Jonas Smedegaard dr at jones.dk
Sat Apr 15 14:25:19 UTC 2017


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository uglifyjs.

commit 3ac24219322384539caae803482ea257e7971cf2
Author: kzc <kzc at users.noreply.github.com>
Date:   Mon Mar 6 12:42:33 2017 -0500

    collapse_vars: do not replace a constant in loop condition or init (#1562)
---
 lib/compress.js                |  9 +++++---
 test/compress/collapse_vars.js | 50 +++++++++++++++++++++++++++++++++++-------
 2 files changed, 48 insertions(+), 11 deletions(-)

diff --git a/lib/compress.js b/lib/compress.js
index d9a67c1..8c3fb15 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -535,10 +535,13 @@ merge(Compressor.prototype, {
                     // Constant single use vars can be replaced in any scope.
                     if (var_decl.value.is_constant()) {
                         var ctt = new TreeTransformer(function(node) {
-                            if (node === ref
-                                && !ctt.find_parent(AST_ForIn)) {
-                                return replace_var(node, ctt.parent(), true);
+                            var parent = ctt.parent();
+                            if (parent instanceof AST_IterationStatement
+                                && (parent.condition === node || parent.init === node)) {
+                                return node;
                             }
+                            if (node === ref)
+                                return replace_var(node, parent, true);
                         });
                         stat.transform(ctt);
                         continue;
diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js
index 82d943f..6d7e2d9 100644
--- a/test/compress/collapse_vars.js
+++ b/test/compress/collapse_vars.js
@@ -344,9 +344,9 @@ collapse_vars_do_while: {
     }
     input: {
         function f1(y) {
-            // The constant do-while condition `c` will be replaced.
+            // The constant do-while condition `c` will not be replaced.
             var c = 9;
-            do { } while (c === 77);
+            do {} while (c === 77);
         }
         function f2(y) {
             // The non-constant do-while condition `c` will not be replaced.
@@ -381,7 +381,8 @@ collapse_vars_do_while: {
     }
     expect: {
         function f1(y) {
-            do ; while (false);
+            var c = 9;
+            do ; while (77 === c);
         }
         function f2(y) {
             var c = 5 - y;
@@ -418,9 +419,9 @@ collapse_vars_do_while_drop_assign: {
     }
     input: {
         function f1(y) {
-            // The constant do-while condition `c` will be replaced.
+            // The constant do-while condition `c` will be not replaced.
             var c = 9;
-            do { } while (c === 77);
+            do {} while (c === 77);
         }
         function f2(y) {
             // The non-constant do-while condition `c` will not be replaced.
@@ -455,7 +456,8 @@ collapse_vars_do_while_drop_assign: {
     }
     expect: {
         function f1(y) {
-            do ; while (false);
+            var c = 9;
+            do ; while (77 === c);
         }
         function f2(y) {
             var c = 5 - y;
@@ -1309,8 +1311,8 @@ collapse_vars_regexp: {
             };
         }
         (function(){
-            var result, rx = /ab*/g;
-            while (result = rx.exec('acdabcdeabbb'))
+            var result, s = "acdabcdeabbb", rx = /ab*/g;
+            while (result = rx.exec(s))
                 console.log(result[0]);
         })();
     }
@@ -1329,3 +1331,35 @@ issue_1537: {
         for (k in {prop: 'val'});
     }
 }
+
+issue_1562: {
+    options = {
+        collapse_vars: true,
+    }
+    input: {
+        var v = 1, B = 2;
+        for (v in objs) f(B);
+
+        var x = 3, C = 10;
+        while(x + 2) bar(C);
+
+        var y = 4, D = 20;
+        do bar(D); while(y + 2);
+
+        var z = 5, E = 30;
+        for (; f(z + 2) ;) bar(E);
+    }
+    expect: {
+        var v = 1;
+        for (v in objs) f(2);
+
+        var x = 3;
+        while(x + 2) bar(10);
+
+        var y = 4;
+        do bar(20); while(y + 2);
+
+        var z = 5;
+        for (; f(z + 2) ;) bar(30);
+    }
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/uglifyjs.git



More information about the Pkg-javascript-commits mailing list