[Pkg-javascript-commits] [uglifyjs] 428/491: enhance `collapse_vars` (#2788)

Jonas Smedegaard dr at jones.dk
Wed Feb 14 19:52:00 UTC 2018


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

js pushed a commit to annotated tag debian/3.3.10-1
in repository uglifyjs.

commit 10f961c27b0db61c3a197f8f88080d9197361ea4
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date:   Mon Jan 15 18:47:23 2018 +0800

    enhance `collapse_vars` (#2788)
---
 lib/compress.js                | 14 ++++++++++++
 test/compress/collapse_vars.js | 50 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+)

diff --git a/lib/compress.js b/lib/compress.js
index a461cb0..fb1c2fd 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -1181,14 +1181,26 @@ merge(Compressor.prototype, {
                     expr.definitions.forEach(extract_candidates);
                 } else if (expr instanceof AST_DWLoop) {
                     extract_candidates(expr.condition);
+                    if (!(expr.body instanceof AST_Block)) {
+                        extract_candidates(expr.body);
+                    }
                 } else if (expr instanceof AST_Exit) {
                     if (expr.value) extract_candidates(expr.value);
                 } else if (expr instanceof AST_For) {
                     if (expr.init) extract_candidates(expr.init);
                     if (expr.condition) extract_candidates(expr.condition);
                     if (expr.step) extract_candidates(expr.step);
+                    if (!(expr.body instanceof AST_Block)) {
+                        extract_candidates(expr.body);
+                    }
                 } else if (expr instanceof AST_If) {
                     extract_candidates(expr.condition);
+                    if (!(expr.body instanceof AST_Block)) {
+                        extract_candidates(expr.body);
+                    }
+                    if (expr.alternative && !(expr.alternative instanceof AST_Block)) {
+                        extract_candidates(expr.alternative);
+                    }
                 } else if (expr instanceof AST_Sequence) {
                     expr.expressions.forEach(extract_candidates);
                 } else if (expr instanceof AST_SimpleStatement) {
@@ -1215,10 +1227,12 @@ merge(Compressor.prototype, {
                 if (parent instanceof AST_Call) return node;
                 if (parent instanceof AST_Case) return node;
                 if (parent instanceof AST_Conditional) return node;
+                if (parent instanceof AST_Definitions) return find_stop(parent, level + 1);
                 if (parent instanceof AST_Exit) return node;
                 if (parent instanceof AST_If) return node;
                 if (parent instanceof AST_IterationStatement) return node;
                 if (parent instanceof AST_Sequence) return find_stop(parent, level + 1);
+                if (parent instanceof AST_SimpleStatement) return find_stop(parent, level + 1);
                 if (parent instanceof AST_Switch) return node;
                 if (parent instanceof AST_VarDef) return node;
                 return null;
diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js
index 12b4923..f252a7f 100644
--- a/test/compress/collapse_vars.js
+++ b/test/compress/collapse_vars.js
@@ -4012,3 +4012,53 @@ replace_all_var: {
     }
     expect_stdout: "PASS"
 }
+
+cascade_statement: {
+    options = {
+        collapse_vars: true,
+    }
+    input: {
+        function f1(a, b) {
+            var c;
+            if (a)
+                return c = b, c || a;
+            else
+                c = a, c(b);
+        }
+        function f2(a, b) {
+            var c;
+            while (a)
+                c = b, a = c + b;
+            do
+                throw c = a + b, c;
+            while (c);
+        }
+        function f3(a, b) {
+            for (; a < b; a++)
+                if (c = a, c && b)
+                    var c = (c = b(a), c);
+        }
+    }
+    expect: {
+        function f1(a, b) {
+            var c;
+            if (a)
+                return (c = b) || a;
+            else
+                (c = a)(b);
+        }
+        function f2(a, b) {
+            var c;
+            while (a)
+                a = (c = b) + b;
+            do
+                throw c = a + b;
+            while (c);
+        }
+        function f3(a, b) {
+            for (; a < b; a++)
+                if ((c = a) && b)
+                    var c = c = b(a);
+        }
+    }
+}

-- 
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