Skip to content

Commit

Permalink
fix corner case in hoist_vars (#5196)
Browse files Browse the repository at this point in the history
fixes #5195
  • Loading branch information
alexlamsl authored Nov 24, 2021
1 parent 0b6c185 commit 1b4bd70
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 11 deletions.
1 change: 1 addition & 0 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -9102,6 +9102,7 @@ merge(Compressor.prototype, {
});
def.references.push(name);
}
def.assignments++;
def.eliminated++;
def.single_use = false;
return a;
Expand Down
74 changes: 63 additions & 11 deletions test/compress/hoist_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ issue_4487_1: {
var b = a();
}
expect: {
function a() {
var a = function f() {
var f = console.log(typeof f);
}
};
a();
}
expect_stdout: "undefined"
Expand All @@ -175,6 +175,31 @@ issue_4487_2: {
};
var b = a();
}
expect: {
function a() {
var f = console.log(typeof f);
}
a();
}
expect_stdout: "undefined"
}

issue_4487_3: {
options = {
functions: true,
hoist_vars: true,
keep_fnames: true,
passes: 3,
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
var a = function f() {
var f = console.log(typeof f);
};
var b = a();
}
expect: {
(function a() {
console.log(typeof void 0);
Expand Down Expand Up @@ -256,9 +281,8 @@ issue_4736: {
expect: {
(function() {
(function() {
var b = 1 << 30;
0,
console.log(b);
console.log(1073741824);
})();
})();
}
Expand All @@ -275,18 +299,18 @@ issue_4839: {
unused: true,
}
input: {
var o = function(a, b) {
var log = console.log, o = function(a, b) {
return b && b;
}("foo");
for (var k in o)
throw "FAIL";
console.log("PASS");
log("PASS");
}
expect: {
var k, o = void 0;
for (k in o)
var k, log = console.log;
for (k in void 0)
throw "FAIL";
console.log("PASS");
log("PASS");
}
expect_stdout: "PASS"
}
Expand All @@ -312,8 +336,7 @@ issue_4859: {
}
expect: {
(function f(a) {
var d = 1 / 0, d = Infinity;
console.log(d);
console.log(Infinity);
return f;
})();
}
Expand Down Expand Up @@ -448,3 +471,32 @@ issue_5187: {
}
expect_stdout: "42"
}

issue_5195: {
options = {
hoist_props: true,
hoist_vars: true,
reduce_vars: true,
side_effects: true,
toplevel: true,
unused: true,
}
input: {
function f() {
var a;
do {
var b = { p: a };
} while (console.log(b += ""));
}
f();
}
expect: {
(function() {
var a, b;
do {
b = { p: a };
} while (console.log(b += ""));
})();
}
expect_stdout: "[object Object]"
}

0 comments on commit 1b4bd70

Please sign in to comment.