[Pkg-javascript-commits] [uglifyjs] 29/50: introduce `unsafe_regexp` (#1970)

Jonas Smedegaard dr at jones.dk
Thu Aug 17 23:06:45 UTC 2017


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository uglifyjs.

commit 957c54bc87d4febd774c9fd700da869fea7f18be
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date:   Fri May 19 09:06:29 2017 +0800

    introduce `unsafe_regexp` (#1970)
    
    fixes #1964
---
 .travis.yml               |  1 -
 README.md                 | 11 +++++++----
 lib/compress.js           |  3 ++-
 test/compress/evaluate.js | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 56 insertions(+), 6 deletions(-)

diff --git a/.travis.yml b/.travis.yml
index 06929a3..cdee430 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,5 +1,4 @@
 language: node_js
-before_install: "npm install -g npm"
 node_js:
     - "0.10"
     - "0.12"
diff --git a/README.md b/README.md
index ac29e79..8c1d676 100644
--- a/README.md
+++ b/README.md
@@ -353,6 +353,9 @@ to set `true`; it's effectively a shortcut for `foo=true`).
 - `unsafe_proto` (default: false) -- optimize expressions like
   `Array.prototype.slice.call(a)` into `[].slice.call(a)`
 
+- `unsafe_regexp` (default: false) -- enable substitutions of variables with
+  `RegExp` values the same way as if they are constants.
+
 - `conditionals` -- apply optimizations for `if`-s and conditional
   expressions
 
@@ -443,10 +446,10 @@ to set `true`; it's effectively a shortcut for `foo=true`).
 - `keep_infinity` -- default `false`. Pass `true` to prevent `Infinity` from
   being compressed into `1/0`, which may cause performance issues on Chrome.
 
-- `side_effects` -- default `false`. Pass `true` to potentially drop functions
-marked as "pure".  A function call is marked as "pure" if a comment annotation
-`/*@__PURE__*/` or `/*#__PURE__*/` immediately precedes the call. For example:
-`/*@__PURE__*/foo()`;
+- `side_effects` -- default `true`. Pass `false` to disable potentially dropping
+  functions marked as "pure".  A function call is marked as "pure" if a comment
+  annotation `/*@__PURE__*/` or `/*#__PURE__*/` immediately precedes the call. For
+  example: `/*@__PURE__*/foo();`
 
 
 ### The `unsafe` option
diff --git a/lib/compress.js b/lib/compress.js
index c3adcc5..d8a491e 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -84,6 +84,7 @@ function Compressor(options, false_by_default) {
         unsafe_comps  : false,
         unsafe_math   : false,
         unsafe_proto  : false,
+        unsafe_regexp : false,
         unused        : !false_by_default,
         warnings      : true,
     }, true);
@@ -3680,7 +3681,7 @@ merge(Compressor.prototype, {
             if (fixed) {
                 if (d.should_replace === undefined) {
                     var init = fixed.evaluate(compressor);
-                    if (init !== fixed) {
+                    if (init !== fixed && (compressor.option("unsafe_regexp") || !(init instanceof RegExp))) {
                         init = make_node_from_constant(init, fixed);
                         var value = init.optimize(compressor).print_to_string().length;
                         var fn;
diff --git a/test/compress/evaluate.js b/test/compress/evaluate.js
index 611acf0..985a2d0 100644
--- a/test/compress/evaluate.js
+++ b/test/compress/evaluate.js
@@ -989,3 +989,50 @@ Infinity_NaN_undefined_LHS: {
         "}",
     ]
 }
+
+issue_1964_1: {
+    options = {
+        evaluate: true,
+        reduce_vars: true,
+        unsafe_regexp: false,
+        unused: true,
+    }
+    input: {
+        function f() {
+            var long_variable_name = /\s/;
+            return "a b c".split(long_variable_name)[1];
+        }
+        console.log(f());
+    }
+    expect: {
+        function f() {
+            var long_variable_name = /\s/;
+            return "a b c".split(long_variable_name)[1];
+        }
+        console.log(f());
+    }
+    expect_stdout: "b"
+}
+
+issue_1964_2: {
+    options = {
+        evaluate: true,
+        reduce_vars: true,
+        unsafe_regexp: true,
+        unused: true,
+    }
+    input: {
+        function f() {
+            var long_variable_name = /\s/;
+            return "a b c".split(long_variable_name)[1];
+        }
+        console.log(f());
+    }
+    expect: {
+        function f() {
+            return "a b c".split(/\s/)[1];
+        }
+        console.log(f());
+    }
+    expect_stdout: "b"
+}

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