[Pkg-javascript-commits] [uglifyjs] 305/491: improve switch case compression (#2547)

Jonas Smedegaard dr at jones.dk
Wed Feb 14 19:51:47 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 7ac6fdcc9923e173522c4b94b919ac09049024f9
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date:   Fri Dec 1 14:32:00 2017 +0800

    improve switch case compression (#2547)
---
 lib/compress.js             | 20 +++++++++++++-------
 test/compress/issue-1750.js |  4 ++--
 test/compress/switch.js     | 27 +++++++++++++++++++++++++++
 3 files changed, 42 insertions(+), 9 deletions(-)

diff --git a/lib/compress.js b/lib/compress.js
index dfee94f..adfbb79 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -3312,6 +3312,11 @@ merge(Compressor.prototype, {
                 }
             } else if (!(value instanceof AST_Node)) {
                 var exp = branch.expression.evaluate(compressor);
+                if (!(exp instanceof AST_Node) && exp !== value) {
+                    eliminate_branch(branch, body[body.length - 1]);
+                    continue;
+                }
+                if (exp instanceof AST_Node) exp = branch.expression.tail_node().evaluate(compressor);
                 if (exp === value) {
                     exact_match = branch;
                     if (default_branch) {
@@ -3320,9 +3325,6 @@ merge(Compressor.prototype, {
                         eliminate_branch(default_branch, body[default_index - 1]);
                         default_branch = null;
                     }
-                } else if (exp !== branch.expression) {
-                    eliminate_branch(branch, body[body.length - 1]);
-                    continue;
                 }
             }
             if (aborts(branch)) {
@@ -3365,12 +3367,16 @@ merge(Compressor.prototype, {
             });
             self.walk(tw);
             if (!has_break) {
-                body = body[0].body.slice();
-                body.unshift(make_node(AST_SimpleStatement, self.expression, {
-                    body: self.expression
+                var statements = body[0].body.slice();
+                var exp = body[0].expression;
+                if (exp) statements.unshift(make_node(AST_SimpleStatement, exp, {
+                    body: exp
+                }));
+                statements.unshift(make_node(AST_SimpleStatement, self.expression, {
+                    body:self.expression
                 }));
                 return make_node(AST_BlockStatement, self, {
-                    body: body
+                    body: statements
                 }).optimize(compressor);
             }
         }
diff --git a/test/compress/issue-1750.js b/test/compress/issue-1750.js
index d18bc49..970cea1 100644
--- a/test/compress/issue-1750.js
+++ b/test/compress/issue-1750.js
@@ -7,7 +7,7 @@ case_1: {
     input: {
         var a = 0, b = 1;
         switch (true) {
-          case a, true:
+          case a || true:
           default:
             b = 2;
           case true:
@@ -17,7 +17,7 @@ case_1: {
     expect: {
         var a = 0, b = 1;
         switch (true) {
-          case a, true:
+          case a || true:
             b = 2;
         }
         console.log(a, b);
diff --git a/test/compress/switch.js b/test/compress/switch.js
index b763d74..fbb86ed 100644
--- a/test/compress/switch.js
+++ b/test/compress/switch.js
@@ -833,7 +833,34 @@ issue_2535: {
     }
     expect: {
         w(), 42;
+        42;
         y();
         z();
     }
 }
+
+issue_1750: {
+    options = {
+        dead_code: true,
+        evaluate: true,
+        switches: true,
+    }
+    input: {
+        var a = 0, b = 1;
+        switch (true) {
+          case a, true:
+          default:
+            b = 2;
+          case true:
+        }
+        console.log(a, b);
+    }
+    expect: {
+        var a = 0, b = 1;
+        true;
+        a, true;
+        b = 2;
+        console.log(a, b);
+    }
+    expect_stdout: "0 2"
+}

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