Skip to content

Commit

Permalink
introduce unsafe_regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed May 18, 2017
1 parent 43add94 commit e533a9e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
47 changes: 47 additions & 0 deletions test/compress/evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit e533a9e

Please sign in to comment.