aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2018-05-19 05:45:14 +0800
committerGitHub <noreply@github.com>2018-05-19 05:45:14 +0800
commit4fc39d8dadcec807be9d80ceb1d585f41b20c961 (patch)
treeaa798fa8d4438e3265502dcc4a92f434f7c5cdc2
parent0b7c70f7260fc48023aa6e1685f35547b1cca6f2 (diff)
downloadtracifyjs-4fc39d8dadcec807be9d80ceb1d585f41b20c961.tar.gz
tracifyjs-4fc39d8dadcec807be9d80ceb1d585f41b20c961.zip
fix corner case in `collapse_vars` (#3139)
-rw-r--r--lib/compress.js6
-rw-r--r--test/compress/collapse_vars.js21
2 files changed, 25 insertions, 2 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 5f35b98a..efc7c47f 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -1240,7 +1240,10 @@ merge(Compressor.prototype, {
}
function should_stop(node, parent) {
- if (node instanceof AST_Assign) return node.operator != "=" && lhs.equivalent_to(node.left);
+ if (parent instanceof AST_For) return node !== parent.init;
+ if (node instanceof AST_Assign) {
+ return node.operator != "=" && lhs.equivalent_to(node.left);
+ }
if (node instanceof AST_Call) {
return lhs instanceof AST_PropAccess && lhs.equivalent_to(node.expression);
}
@@ -1249,7 +1252,6 @@ merge(Compressor.prototype, {
if (node instanceof AST_LoopControl) return true;
if (node instanceof AST_Try) return true;
if (node instanceof AST_With) return true;
- if (parent instanceof AST_For) return node !== parent.init;
if (replace_all) return false;
return node instanceof AST_SymbolRef && !node.is_declared(compressor);
}
diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js
index c915041f..bcc8b833 100644
--- a/test/compress/collapse_vars.js
+++ b/test/compress/collapse_vars.js
@@ -4931,6 +4931,27 @@ collapse_rhs_lhs_2: {
expect_stdout: "PASS"
}
+collapse_rhs_loop: {
+ options = {
+ collapse_vars: true,
+ }
+ input: {
+ var s;
+ s = "<tpl>PASS</tpl>";
+ for (var m, r = /<tpl>(.*)<\/tpl>/; m = s.match(r);)
+ s = s.replace(m[0], m[1]);
+ console.log(s);
+ }
+ expect: {
+ var s;
+ s = "<tpl>PASS</tpl>";
+ for (var m, r = /<tpl>(.*)<\/tpl>/; m = s.match(r);)
+ s = s.replace(m[0], m[1]);
+ console.log(s);
+ }
+ expect_stdout: "PASS"
+}
+
collapse_rhs_side_effects: {
options = {
collapse_vars: true,