Skip to content

Commit

Permalink
fix corner case in unsafe (#3380)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Apr 24, 2019
1 parent 6fe20db commit d3b93ec
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/compress.js
Expand Up @@ -3018,6 +3018,7 @@ merge(Compressor.prototype, {
if (arg === value) return this;
args.push(value);
}
if (key == "replace" && typeof args[1] == "function") return this;
try {
return val[key].apply(val, args);
} catch (ex) {
Expand Down Expand Up @@ -3139,6 +3140,7 @@ merge(Compressor.prototype, {
} else if (expr instanceof AST_RegExp) {
map = native_fns.RegExp;
} else if (expr.is_string(compressor)) {
if (this.property == "replace") return false;
map = native_fns.String;
} else if (!this.may_throw_on_access(compressor)) {
map = native_fns.Object;
Expand Down
18 changes: 18 additions & 0 deletions test/compress/dead-code.js
Expand Up @@ -942,3 +942,21 @@ issue_2929: {
}
expect_stdout: "PASS"
}

unsafe_string_replace: {
options = {
side_effects: true,
unsafe: true,
}
input: {
"foo".replace("f", function() {
console.log("PASS");
});
}
expect: {
"foo".replace("f", function() {
console.log("PASS");
});
}
expect_stdout: "PASS"
}
18 changes: 18 additions & 0 deletions test/compress/evaluate.js
Expand Up @@ -1712,3 +1712,21 @@ unsafe_escaped: {
}
expect_stdout: "PASS"
}

unsafe_string_replace: {
options = {
evaluate: true,
unsafe: true,
}
input: {
"foo".replace("f", function() {
console.log("PASS");
});
}
expect: {
"foo".replace("f", function() {
console.log("PASS");
});
}
expect_stdout: "PASS"
}

0 comments on commit d3b93ec

Please sign in to comment.