Skip to content

Commit

Permalink
fix corner case in hoist_vars (#4900)
Browse files Browse the repository at this point in the history
fixes #4898
  • Loading branch information
alexlamsl committed May 2, 2021
1 parent 4114431 commit 203f4b7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
14 changes: 11 additions & 3 deletions lib/compress.js
Expand Up @@ -8438,12 +8438,20 @@ merge(Compressor.prototype, {
if (value) {
if (value instanceof AST_Sequence) value = value.clone();
var name = make_node(AST_SymbolRef, defn.name, defn.name);
name.fixed = value;
a.push(make_node(AST_Assign, defn, {
var assign = make_node(AST_Assign, defn, {
operator: "=",
left: name,
right: value,
}));
});
a.push(assign);
name.fixed = function() {
return assign.right;
};
name.fixed.assigns = [ assign ];
def.references.forEach(function(ref) {
var assigns = ref.fixed && ref.fixed.assigns;
if (assigns && assigns[0] === defn) assigns[0] = assign;
});
def.references.push(name);
}
def.eliminated++;
Expand Down
27 changes: 25 additions & 2 deletions test/compress/hoist_vars.js
Expand Up @@ -232,9 +232,8 @@ issue_4736: {
expect: {
(function() {
(function() {
var b = 1 << 30;
0,
console.log(b);
console.log(1073741824);
})();
})();
}
Expand Down Expand Up @@ -373,3 +372,27 @@ issue_4893_2: {
}
expect_stdout: "PASS"
}

issue_4898: {
options = {
collapse_vars: true,
evaluate: true,
hoist_vars: true,
loops: true,
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
do {
var b = [ console.log("PASS") ];
var c = b;
} while (c.p = 0);
}
expect: {
var b;
b = [ console.log("PASS") ];
b.p = 0;
}
expect_stdout: "PASS"
}

0 comments on commit 203f4b7

Please sign in to comment.