Skip to content

Commit

Permalink
fix corner case in reduce_vars (#4944)
Browse files Browse the repository at this point in the history
fixes #4943
  • Loading branch information
alexlamsl committed May 17, 2021
1 parent ae4dbcb commit 2549377
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 7 deletions.
20 changes: 13 additions & 7 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)) {
Expand All @@ -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;
Expand Down
60 changes: 60 additions & 0 deletions test/compress/reduce_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
}

0 comments on commit 2549377

Please sign in to comment.