aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2021-05-17 22:52:24 +0100
committerGitHub <noreply@github.com>2021-05-18 05:52:24 +0800
commit254937754c2e3c33f934dcf7df619d8a46319a14 (patch)
tree2c31e19f7e47d4a3e50583b8a3fc7872397bdd9b
parentae4dbcb5b96ddb2696de1d890e6a71b2445f7ec5 (diff)
downloadtracifyjs-254937754c2e3c33f934dcf7df619d8a46319a14.tar.gz
tracifyjs-254937754c2e3c33f934dcf7df619d8a46319a14.zip
fix corner case in `reduce_vars` (#4944)
fixes #4943
-rw-r--r--lib/compress.js20
-rw-r--r--test/compress/reduce_vars.js60
2 files changed, 73 insertions, 7 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 6bc5d3b4..6f702ea9 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -1194,11 +1194,20 @@ merge(Compressor.prototype, {
def(AST_SymbolRef, function(tw, descend, compressor) {
var d = this.definition();
push_ref(d, this);
- if (d.references.length == 1
- && !d.fixed
- && d.orig[0] instanceof AST_SymbolDefun) {
+ if (d.references.length == 1 && !d.fixed && d.orig[0] instanceof AST_SymbolDefun) {
tw.loop_ids[d.id] = tw.in_loop;
}
+ var recursive = recursive_ref(tw, d);
+ if (recursive) recursive.enclosed.forEach(function(def) {
+ if (d === def) return;
+ if (def.scope === recursive) return;
+ var assigns = def.fixed && def.fixed.assigns;
+ if (!assigns) return;
+ if (assigns[assigns.length - 1] instanceof AST_VarDef) return;
+ var safe = tw.safe_ids[def.id];
+ if (!safe) return;
+ safe.assign = true;
+ });
if (d.fixed === false) {
var redef = d.redefined();
if (redef && cross_scope(d.scope, this.scope)) redef.single_use = false;
@@ -1207,7 +1216,6 @@ merge(Compressor.prototype, {
} else if (d.fixed) {
if (this.in_arg && d.orig[0] instanceof AST_SymbolLambda) this.fixed = d.scope;
var value = this.fixed_value();
- var recursive = recursive_ref(tw, d);
if (recursive) {
d.recursive_refs++;
} else if (value && ref_once(compressor, d)) {
@@ -1228,9 +1236,7 @@ merge(Compressor.prototype, {
d.fixed = false;
}
}
- if (d.fixed && tw.loop_ids[d.id] !== tw.in_loop) {
- d.cross_loop = true;
- }
+ if (d.fixed && tw.loop_ids[d.id] !== tw.in_loop) d.cross_loop = true;
mark_escaped(tw, d, this.scope, this, value, 0, 1);
}
if (!this.fixed) this.fixed = d.fixed;
diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js
index c899fcf0..04d96b51 100644
--- a/test/compress/reduce_vars.js
+++ b/test/compress/reduce_vars.js
@@ -7662,3 +7662,63 @@ issue_4937: {
}
expect_stdout: "PASS"
}
+
+issue_4943_1: {
+ options = {
+ evaluate: true,
+ reduce_vars: true,
+ toplevel: true,
+ }
+ input: {
+ var a, b = 1;
+ (function f() {
+ a = "foo";
+ b-- && f();
+ console.log(a);
+ a = "bar";
+ })();
+ }
+ expect: {
+ var a, b = 1;
+ (function f() {
+ a = "foo";
+ b-- && f();
+ console.log(a);
+ a = "bar";
+ })();
+ }
+ expect_stdout: [
+ "foo",
+ "bar",
+ ]
+}
+
+issue_4943_2: {
+ options = {
+ reduce_vars: true,
+ toplevel: true,
+ unused: true,
+ }
+ input: {
+ var a, b = 1;
+ (function f() {
+ a = "foo";
+ b-- && f();
+ console.log(a);
+ a = "bar";
+ })();
+ }
+ expect: {
+ var a, b = 1;
+ (function f() {
+ a = "foo";
+ b-- && f();
+ console.log(a);
+ a = "bar";
+ })();
+ }
+ expect_stdout: [
+ "foo",
+ "bar",
+ ]
+}