[Pkg-javascript-commits] [uglifyjs] 330/491: fix `dead_code` on `return`/`throw` within `try` (#2588)

Jonas Smedegaard dr at jones.dk
Wed Feb 14 19:51:50 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 8266993c6e0a78c4872cbcb81072094a4a7dba2d
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date:   Thu Dec 14 04:38:21 2017 +0800

    fix `dead_code` on `return`/`throw` within `try` (#2588)
---
 lib/compress.js            |  13 ++++
 test/compress/dead-code.js | 146 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 159 insertions(+)

diff --git a/lib/compress.js b/lib/compress.js
index 559fb51..e394e8b 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -4674,6 +4674,11 @@ merge(Compressor.prototype, {
                 node = parent;
                 parent = compressor.parent(level++);
                 if (parent instanceof AST_Exit) {
+                    var try_node = find_try(level);
+                    if (try_node) {
+                        if (try_node.bfinally) break;
+                        if (parent instanceof AST_Throw && try_node.bcatch) break;
+                    }
                     if (self.operator == "=") return self.right;
                     return make_node(AST_Binary, self, {
                         operator: self.operator.slice(0, -1),
@@ -4704,6 +4709,14 @@ merge(Compressor.prototype, {
             }
         }
         return self;
+
+        function find_try(level) {
+            var scope = self.left.definition().scope;
+            var parent;
+            while ((parent = compressor.parent(level++)) !== scope) {
+                if (parent instanceof AST_Try) return parent;
+            }
+        }
     });
 
     OPT(AST_Conditional, function(self, compressor){
diff --git a/test/compress/dead-code.js b/test/compress/dead-code.js
index ca7ad5c..9612b92 100644
--- a/test/compress/dead-code.js
+++ b/test/compress/dead-code.js
@@ -491,6 +491,20 @@ return_assignment: {
             var e;
             return e = x();
         }
+        function f5(a) {
+            try {
+                return a = x();
+            } catch (b) {
+                console.log(a);
+            }
+        }
+        function f6(a) {
+            try {
+                return a = x();
+            } finally {
+                console.log(a);
+            }
+        }
     }
     expect: {
         function f1(a, b, c) {
@@ -505,5 +519,137 @@ return_assignment: {
         function f4() {
             return x();
         }
+        function f5(a) {
+            try {
+                return x();
+            } catch (b) {
+                console.log(a);
+            }
+        }
+        function f6(a) {
+            try {
+                return a = x();
+            } finally {
+                console.log(a);
+            }
+        }
+    }
+}
+
+throw_assignment: {
+    options = {
+        dead_code: true,
+        unused: true,
+    }
+    input: {
+        function f1() {
+            throw a = x();
+        }
+        function f2(a) {
+            throw a = x();
+        }
+        function f3() {
+            var a;
+            throw a = x();
+        }
+        function f4() {
+            try {
+                throw a = x();
+            } catch (b) {
+                console.log(a);
+            }
+        }
+        function f5(a) {
+            try {
+                throw a = x();
+            } catch (b) {
+                console.log(a);
+            }
+        }
+        function f6() {
+            var a;
+            try {
+                throw a = x();
+            } catch (b) {
+                console.log(a);
+            }
+        }
+        function f7() {
+            try {
+                throw a = x();
+            } finally {
+                console.log(a);
+            }
+        }
+        function f8(a) {
+            try {
+                throw a = x();
+            } finally {
+                console.log(a);
+            }
+        }
+        function f9() {
+            var a;
+            try {
+                throw a = x();
+            } finally {
+                console.log(a);
+            }
+        }
+    }
+    expect: {
+        function f1() {
+            throw a = x();
+        }
+        function f2(a) {
+            throw x();
+        }
+        function f3() {
+            throw x();
+        }
+        function f4() {
+            try {
+                throw a = x();
+            } catch (b) {
+                console.log(a);
+            }
+        }
+        function f5(a) {
+            try {
+                throw a = x();
+            } catch (b) {
+                console.log(a);
+            }
+        }
+        function f6() {
+            var a;
+            try {
+                throw a = x();
+            } catch (b) {
+                console.log(a);
+            }
+        }
+        function f7() {
+            try {
+                throw a = x();
+            } finally {
+                console.log(a);
+            }
+        }
+        function f8(a) {
+            try {
+                throw a = x();
+            } finally {
+                console.log(a);
+            }
+        }
+        function f9() {
+            var a;
+            try {
+                throw a = x();
+            } finally {
+                console.log(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