aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2019-04-24 22:21:28 +0800
committerGitHub <noreply@github.com>2019-04-24 22:21:28 +0800
commitd3b93ec682f948e80c9617421128597a12651f78 (patch)
tree2d4bd83bfe8a308ec3a6f1acb59bb5c74906a79d
parent6fe20dbe33bc78b161489207287c9154d5d32fd4 (diff)
downloadtracifyjs-d3b93ec682f948e80c9617421128597a12651f78.tar.gz
tracifyjs-d3b93ec682f948e80c9617421128597a12651f78.zip
fix corner case in `unsafe` (#3380)
-rw-r--r--lib/compress.js2
-rw-r--r--test/compress/dead-code.js18
-rw-r--r--test/compress/evaluate.js18
3 files changed, 38 insertions, 0 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 767f3dcc..758e4595 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -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) {
@@ -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;
diff --git a/test/compress/dead-code.js b/test/compress/dead-code.js
index 1ce5355a..5f1ca71c 100644
--- a/test/compress/dead-code.js
+++ b/test/compress/dead-code.js
@@ -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"
+}
diff --git a/test/compress/evaluate.js b/test/compress/evaluate.js
index ebdffadb..5b16824b 100644
--- a/test/compress/evaluate.js
+++ b/test/compress/evaluate.js
@@ -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"
+}