[Pkg-javascript-commits] [uglifyjs] 370/491: fix `dead_code` on `return` assignments (#2668)

Jonas Smedegaard dr at jones.dk
Wed Feb 14 19:51:53 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 f30790b11bb9e162a19d7769ab54d8bb3f61cc27
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date:   Wed Dec 27 07:40:34 2017 +0800

    fix `dead_code` on `return` assignments (#2668)
    
    fixes #2666
---
 lib/compress.js            | 22 +++++++++++++++++++++-
 test/compress/dead-code.js | 27 +++++++++++++++++++++++++++
 2 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/lib/compress.js b/lib/compress.js
index c8cddc3..bd61b87 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -4841,15 +4841,17 @@ merge(Compressor.prototype, {
     var ASSIGN_OPS = [ '+', '-', '/', '*', '%', '>>', '<<', '>>>', '|', '^', '&' ];
     var ASSIGN_OPS_COMMUTATIVE = [ '*', '|', '^', '&' ];
     OPT(AST_Assign, function(self, compressor){
+        var def;
         if (compressor.option("dead_code")
             && self.left instanceof AST_SymbolRef
-            && self.left.definition().scope === compressor.find_parent(AST_Lambda)) {
+            && (def = self.left.definition()).scope === compressor.find_parent(AST_Lambda)) {
             var level = 0, node, parent = self;
             do {
                 node = parent;
                 parent = compressor.parent(level++);
                 if (parent instanceof AST_Exit) {
                     if (in_try(level, parent instanceof AST_Throw)) break;
+                    if (is_reachable(def)) break;
                     if (self.operator == "=") return self.right;
                     return make_node(AST_Binary, self, {
                         operator: self.operator.slice(0, -1),
@@ -4891,6 +4893,24 @@ merge(Compressor.prototype, {
                 }
             }
         }
+
+        function is_reachable(def) {
+            var reachable = false;
+            var find_ref = new TreeWalker(function(node) {
+                if (reachable) return true;
+                if (node instanceof AST_SymbolRef && node.definition() === def) {
+                    return reachable = true;
+                }
+            });
+            self.right.walk(new TreeWalker(function(node) {
+                if (reachable) return true;
+                if (node instanceof AST_Scope) {
+                    node.walk(find_ref);
+                    return true;
+                }
+            }));
+            return reachable;
+        }
     });
 
     OPT(AST_Conditional, function(self, compressor){
diff --git a/test/compress/dead-code.js b/test/compress/dead-code.js
index 591dd3a..7ea380d 100644
--- a/test/compress/dead-code.js
+++ b/test/compress/dead-code.js
@@ -828,3 +828,30 @@ issue_2597: {
     }
     expect_stdout: "PASS"
 }
+
+issue_2666: {
+    options = {
+        dead_code: true,
+    }
+    input: {
+        function f(a) {
+            return a = {
+                p: function() {
+                    return a;
+                }
+            };
+        }
+        console.log(typeof f().p());
+    }
+    expect: {
+        function f(a) {
+            return a = {
+                p: function() {
+                    return a;
+                }
+            };
+        }
+        console.log(typeof f().p());
+    }
+    expect_stdout: "object"
+}

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