[Pkg-javascript-commits] [uglifyjs] 149/491: correctly determine scope of `AST_This` (#2109)

Jonas Smedegaard dr at jones.dk
Wed Feb 14 19:51:30 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 11e63bc3351597a7cd05a769346bb577e65069d4
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date:   Fri Jun 16 14:54:46 2017 +0800

    correctly determine scope of `AST_This` (#2109)
    
    fixes #2107
---
 lib/compress.js            | 19 +++++++++++--------
 test/compress/functions.js | 29 +++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 8 deletions(-)

diff --git a/lib/compress.js b/lib/compress.js
index e14e63a..a4552e8 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -3207,18 +3207,21 @@ merge(Compressor.prototype, {
                     if (body.length == 1 && body[0] instanceof AST_Return) {
                         value = body[0].value;
                         if (!value) return make_node(AST_Undefined, self);
-                        value.walk(new TreeWalker(function(node) {
+                        var tw = new TreeWalker(function(node) {
                             if (value === self) return true;
-                            if (node instanceof AST_SymbolRef && matches(node.scope.find_variable(node))
-                                || node instanceof AST_This && matches(node)) {
+                            if (node instanceof AST_SymbolRef) {
+                                var ref = node.scope.find_variable(node);
+                                if (ref && ref.scope.parent_scope === fn.parent_scope) {
+                                    value = self;
+                                    return true;
+                                }
+                            }
+                            if (node instanceof AST_This && !tw.find_parent(AST_Scope)) {
                                 value = self;
                                 return true;
                             }
-
-                            function matches(ref) {
-                                return ref && ref.scope.parent_scope === fn.parent_scope;
-                            }
-                        }));
+                        });
+                        value.walk(tw);
                         if (value !== self) value = best_of(compressor, value, self);
                     } else {
                         value = self;
diff --git a/test/compress/functions.js b/test/compress/functions.js
index c2794f2..dc430d1 100644
--- a/test/compress/functions.js
+++ b/test/compress/functions.js
@@ -414,3 +414,32 @@ inner_ref: {
     }
     expect_stdout: "1 undefined"
 }
+
+issue_2107: {
+    options = {
+        cascade: true,
+        collapse_vars: true,
+        inline: true,
+        sequences: true,
+        side_effects: true,
+        unused: true,
+    }
+    input: {
+        var c = 0;
+        !function() {
+            c++;
+        }(c++ + new function() {
+            this.a = 0;
+            var a = (c = c + 1) + (c = 1 + c);
+            return c++ + a;
+        }());
+        console.log(c);
+    }
+    expect: {
+        var c = 0;
+        c++, new function() {
+            this.a = 0, c = 1 + (c += 1), c++;
+        }(), c++, console.log(c);
+    }
+    expect_stdout: "5"
+}

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