From eae26756f1419e7e601bae8b44d69f4e80dd0d61 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Fri, 19 May 2017 09:06:29 +0800 Subject: [PATCH 1/2] introduce `unsafe_regexp` (#1970) fixes #1964 --- README.md | 11 +++++---- lib/compress.js | 3 ++- test/compress/evaluate.js | 47 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a2997b7fe1a..a76350955b7 100644 --- a/README.md +++ b/README.md @@ -462,6 +462,9 @@ If you're using the `X-SourceMap` header instead, you can just omit `sourceMap.u - `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 @@ -552,10 +555,10 @@ If you're using the `X-SourceMap` header instead, you can just omit `sourceMap.u - `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 `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()`; +- `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();` ## Mangle options diff --git a/lib/compress.js b/lib/compress.js index f168b94218a..f6bf3d05435 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -83,6 +83,7 @@ function Compressor(options, false_by_default) { unsafe_comps : false, unsafe_math : false, unsafe_proto : false, + unsafe_regexp : false, unused : !false_by_default, warnings : false, }, true); @@ -3765,7 +3766,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_length = init.optimize(compressor).print_to_string().length; var fn; diff --git a/test/compress/evaluate.js b/test/compress/evaluate.js index b8077564562..a1e3d0be5e2 100644 --- a/test/compress/evaluate.js +++ b/test/compress/evaluate.js @@ -990,3 +990,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" +} From 3408fc9d32c4491afdd73f0c46112c5a41963062 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Fri, 19 May 2017 09:35:26 +0800 Subject: [PATCH 2/2] v3.0.9 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 70967fdd9d2..4bd70c4b889 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "homepage": "http://lisperator.net/uglifyjs", "author": "Mihai Bazon (http://lisperator.net/)", "license": "BSD-2-Clause", - "version": "3.0.8", + "version": "3.0.9", "engines": { "node": ">=0.8.0" },