[Pkg-javascript-commits] [uglifyjs] 216/491: suppress `collapse_vars` of `this` into "use strict" (#2326)

Jonas Smedegaard dr at jones.dk
Wed Feb 14 19:51:37 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 00f509405b44882bab5d63d97caacb21691cdad6
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date:   Wed Sep 20 05:23:20 2017 +0800

    suppress `collapse_vars` of `this` into "use strict" (#2326)
    
    fixes #2319
---
 lib/ast.js                     |  6 ++--
 lib/compress.js                |  4 ++-
 test/compress/collapse_vars.js | 70 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 75 insertions(+), 5 deletions(-)

diff --git a/lib/ast.js b/lib/ast.js
index 0918574..9b243f1 100644
--- a/lib/ast.js
+++ b/lib/ast.js
@@ -134,11 +134,10 @@ var AST_Debugger = DEFNODE("Debugger", null, {
     $documentation: "Represents a debugger statement",
 }, AST_Statement);
 
-var AST_Directive = DEFNODE("Directive", "value scope quote", {
+var AST_Directive = DEFNODE("Directive", "value quote", {
     $documentation: "Represents a directive, like \"use strict\";",
     $propdoc: {
         value: "[string] The value of this directive as a plain string (it's not an AST_String!)",
-        scope: "[AST_Scope/S] The scope that this directive affects",
         quote: "[string] the original quote character"
     },
 }, AST_Statement);
@@ -299,10 +298,9 @@ var AST_With = DEFNODE("With", "expression", {
 
 /* -----[ scope and functions ]----- */
 
-var AST_Scope = DEFNODE("Scope", "directives variables functions uses_with uses_eval parent_scope enclosed cname", {
+var AST_Scope = DEFNODE("Scope", "variables functions uses_with uses_eval parent_scope enclosed cname", {
     $documentation: "Base class for all statements introducing a lexical scope",
     $propdoc: {
-        directives: "[string*/S] an array of directives declared in this scope",
         variables: "[Object/S] a map of name -> SymbolDef for all variables/functions defined in this scope",
         functions: "[Object/S] like `variables`, but only lists function declarations",
         uses_with: "[boolean/S] tells whether this scope uses the `with` statement",
diff --git a/lib/compress.js b/lib/compress.js
index 3164c5a..9e516a8 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -842,6 +842,8 @@ merge(Compressor.prototype, {
                     && !fn.uses_eval
                     && (iife = compressor.parent()) instanceof AST_Call
                     && iife.expression === fn) {
+                    var fn_strict = compressor.has_directive("use strict");
+                    if (fn_strict && fn.body.indexOf(fn_strict) < 0) fn_strict = false;
                     var names = Object.create(null);
                     for (var i = fn.argnames.length; --i >= 0;) {
                         var sym = fn.argnames[i];
@@ -859,7 +861,7 @@ merge(Compressor.prototype, {
                                     }
                                     arg = null;
                                 }
-                                if (node instanceof AST_This && !tw.find_parent(AST_Scope)) {
+                                if (node instanceof AST_This && (fn_strict || !tw.find_parent(AST_Scope))) {
                                     arg = null;
                                     return true;
                                 }
diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js
index 5e7e982..ecd1861 100644
--- a/test/compress/collapse_vars.js
+++ b/test/compress/collapse_vars.js
@@ -2451,3 +2451,73 @@ issue_2313_2: {
     }
     expect_stdout: "0"
 }
+
+issue_2319_1: {
+    options = {
+        collapse_vars: true,
+        unused: true,
+    }
+    input: {
+        console.log(function(a) {
+            return a;
+        }(!function() {
+            return this;
+        }()));
+    }
+    expect: {
+        console.log(function(a) {
+            return !function() {
+                return this;
+            }();
+        }());
+    }
+    expect_stdout: "false"
+}
+
+issue_2319_2: {
+    options = {
+        collapse_vars: true,
+        unused: true,
+    }
+    input: {
+        console.log(function(a) {
+            "use strict";
+            return a;
+        }(!function() {
+            return this;
+        }()));
+    }
+    expect: {
+        console.log(function(a) {
+            "use strict";
+            return a;
+        }(!function() {
+            return this;
+        }()));
+    }
+    expect_stdout: "false"
+}
+
+issue_2319_3: {
+    options = {
+        collapse_vars: true,
+        unused: true,
+    }
+    input: {
+        "use strict";
+        console.log(function(a) {
+            return a;
+        }(!function() {
+            return this;
+        }()));
+    }
+    expect: {
+        "use strict";
+        console.log(function(a) {
+            return !function() {
+                return this;
+            }();
+        }());
+    }
+    expect_stdout: "true"
+}

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