[Pkg-javascript-commits] [uglifyjs] 210/491: extend `unsafe` on pure global functions (#2303)

Jonas Smedegaard dr at jones.dk
Wed Feb 14 19:51:36 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 aacf3edc68558cc19873d5247951dea328d8f7a9
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date:   Thu Sep 7 22:08:34 2017 +0800

    extend `unsafe` on pure global functions (#2303)
---
 lib/compress.js            | 13 +++++++--
 test/compress/dead-code.js | 68 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 79 insertions(+), 2 deletions(-)

diff --git a/lib/compress.js b/lib/compress.js
index 3da7a31..0ba2951 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -1990,6 +1990,15 @@ merge(Compressor.prototype, {
         return this.pure = pure;
     });
 
+    var global_pure_fns = makePredicate("Boolean decodeURI decodeURIComponent Date encodeURI encodeURIComponent Error escape EvalError isFinite isNaN Number Object parseFloat parseInt RangeError ReferenceError String SyntaxError TypeError unescape URIError");
+    AST_Call.DEFMETHOD("is_expr_pure", function(compressor) {
+        if (compressor.option("unsafe")) {
+            var expr = this.expression;
+            if (is_undeclared_ref(expr) && global_pure_fns(expr.name)) return true;
+        }
+        return this.has_pure_annotation(compressor) || !compressor.pure_funcs(this);
+    });
+
     // determine if expression has side effects
     (function(def){
         def(AST_Node, return_true);
@@ -1999,7 +2008,7 @@ merge(Compressor.prototype, {
         def(AST_This, return_false);
 
         def(AST_Call, function(compressor){
-            if (!this.has_pure_annotation(compressor) && compressor.pure_funcs(this)) return true;
+            if (!this.is_expr_pure(compressor)) return true;
             for (var i = this.args.length; --i >= 0;) {
                 if (this.args[i].has_side_effects(compressor))
                     return true;
@@ -2618,7 +2627,7 @@ merge(Compressor.prototype, {
         def(AST_Constant, return_null);
         def(AST_This, return_null);
         def(AST_Call, function(compressor, first_in_statement){
-            if (!this.has_pure_annotation(compressor) && compressor.pure_funcs(this)) {
+            if (!this.is_expr_pure(compressor)) {
                 if (this.expression instanceof AST_Function
                     && (!this.expression.name || !this.expression.name.definition().references.length)) {
                     var node = this.clone();
diff --git a/test/compress/dead-code.js b/test/compress/dead-code.js
index aea0e54..e763056 100644
--- a/test/compress/dead-code.js
+++ b/test/compress/dead-code.js
@@ -345,3 +345,71 @@ issue_2233_3: {
         UndeclaredGlobal;
     }
 }
+
+global_fns: {
+    options = {
+        side_effects: true,
+        unsafe: true,
+    }
+    input: {
+        Boolean(1, 2);
+        decodeURI(1, 2);
+        decodeURIComponent(1, 2);
+        Date(1, 2);
+        encodeURI(1, 2);
+        encodeURIComponent(1, 2);
+        Error(1, 2);
+        escape(1, 2);
+        EvalError(1, 2);
+        isFinite(1, 2);
+        isNaN(1, 2);
+        Number(1, 2);
+        Object(1, 2);
+        parseFloat(1, 2);
+        parseInt(1, 2);
+        RangeError(1, 2);
+        ReferenceError(1, 2);
+        String(1, 2);
+        SyntaxError(1, 2);
+        TypeError(1, 2);
+        unescape(1, 2);
+        URIError(1, 2);
+        try {
+            Function(1, 2);
+        } catch (e) {
+            console.log(e.name);
+        }
+        try {
+            RegExp(1, 2);
+        } catch (e) {
+            console.log(e.name);
+        }
+        try {
+            Array(NaN);
+        } catch (e) {
+            console.log(e.name);
+        }
+    }
+    expect: {
+        try {
+            Function(1, 2);
+        } catch (e) {
+            console.log(e.name);
+        }
+        try {
+            RegExp(1, 2);
+        } catch (e) {
+            console.log(e.name);
+        }
+        try {
+            Array(NaN);
+        } catch (e) {
+            console.log(e.name);
+        }
+    }
+    expect_stdout: [
+        "SyntaxError",
+        "SyntaxError",
+        "RangeError",
+    ]
+}

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