Skip to content

Commit

Permalink
fix corner case in hoist_vars (#4860)
Browse files Browse the repository at this point in the history
fixes #4859
  • Loading branch information
alexlamsl committed Apr 21, 2021
1 parent c58e174 commit 3c161a6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
14 changes: 7 additions & 7 deletions lib/compress.js
Expand Up @@ -6942,9 +6942,7 @@ merge(Compressor.prototype, {
if (defs.length > 0) {
// try to merge in assignments
insert_vars(self.body);
defs = make_node(AST_Var, self, {
definitions: defs
});
defs = make_node(AST_Var, self, { definitions: defs });
hoisted.push(defs);
}
}
Expand Down Expand Up @@ -8367,12 +8365,14 @@ merge(Compressor.prototype, {
AST_Definitions.DEFMETHOD("to_assignments", function() {
var assignments = this.definitions.reduce(function(a, defn) {
var def = defn.name.definition();
if (defn.value) {
var value = defn.value;
if (value) {
if (value instanceof AST_Sequence) value = value.clone();
var name = make_node(AST_SymbolRef, defn.name, defn.name);
a.push(make_node(AST_Assign, defn, {
operator : "=",
left : name,
right : defn.value
operator: "=",
left: name,
right: value,
}));
def.references.push(name);
}
Expand Down
29 changes: 29 additions & 0 deletions test/compress/hoist_vars.js
Expand Up @@ -265,3 +265,32 @@ issue_4839: {
}
expect_stdout: "PASS"
}

issue_4859: {
options = {
evaluate: true,
hoist_vars: true,
keep_infinity: true,
merge_vars: true,
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
function f(a) {
var b = (a = 2, 1 / 0), c = 3;
var d = a + b;
console.log(d);
return f;
}
f();
}
expect: {
(function f(a) {
var d = 1 / 0, d = Infinity;
console.log(d);
return f;
})();
}
expect_stdout: "Infinity"
}
4 changes: 1 addition & 3 deletions test/ufuzz/index.js
Expand Up @@ -1796,11 +1796,9 @@ function createClassLiteral(recurmax, stmtDepth, canThrow, name) {
if (SUPPORT.class_field && rng(2)) {
s += internal || createObjectKey(recurmax, stmtDepth, canThrow);
if (rng(5)) {
async = false;
async = bug_async_class_await && fixed;
generator = false;
if (bug_async_class_await && fixed) addAvoidVar("await");
s += " = " + createExpression(recurmax, NO_COMMA, stmtDepth, fixed ? canThrow : CANNOT_THROW);
if (bug_async_class_await && fixed) removeAvoidVar("await");
generator = save_generator;
async = save_async;
}
Expand Down

0 comments on commit 3c161a6

Please sign in to comment.