Skip to content

Commit 1b4bd70

Browse files
authored
fix corner case in hoist_vars (#5196)
fixes #5195
1 parent 0b6c185 commit 1b4bd70

File tree

2 files changed

+64
-11
lines changed

2 files changed

+64
-11
lines changed

lib/compress.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9102,6 +9102,7 @@ merge(Compressor.prototype, {
91029102
});
91039103
def.references.push(name);
91049104
}
9105+
def.assignments++;
91059106
def.eliminated++;
91069107
def.single_use = false;
91079108
return a;

test/compress/hoist_vars.js

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ issue_4487_1: {
151151
var b = a();
152152
}
153153
expect: {
154-
function a() {
154+
var a = function f() {
155155
var f = console.log(typeof f);
156-
}
156+
};
157157
a();
158158
}
159159
expect_stdout: "undefined"
@@ -175,6 +175,31 @@ issue_4487_2: {
175175
};
176176
var b = a();
177177
}
178+
expect: {
179+
function a() {
180+
var f = console.log(typeof f);
181+
}
182+
a();
183+
}
184+
expect_stdout: "undefined"
185+
}
186+
187+
issue_4487_3: {
188+
options = {
189+
functions: true,
190+
hoist_vars: true,
191+
keep_fnames: true,
192+
passes: 3,
193+
reduce_vars: true,
194+
toplevel: true,
195+
unused: true,
196+
}
197+
input: {
198+
var a = function f() {
199+
var f = console.log(typeof f);
200+
};
201+
var b = a();
202+
}
178203
expect: {
179204
(function a() {
180205
console.log(typeof void 0);
@@ -256,9 +281,8 @@ issue_4736: {
256281
expect: {
257282
(function() {
258283
(function() {
259-
var b = 1 << 30;
260284
0,
261-
console.log(b);
285+
console.log(1073741824);
262286
})();
263287
})();
264288
}
@@ -275,18 +299,18 @@ issue_4839: {
275299
unused: true,
276300
}
277301
input: {
278-
var o = function(a, b) {
302+
var log = console.log, o = function(a, b) {
279303
return b && b;
280304
}("foo");
281305
for (var k in o)
282306
throw "FAIL";
283-
console.log("PASS");
307+
log("PASS");
284308
}
285309
expect: {
286-
var k, o = void 0;
287-
for (k in o)
310+
var k, log = console.log;
311+
for (k in void 0)
288312
throw "FAIL";
289-
console.log("PASS");
313+
log("PASS");
290314
}
291315
expect_stdout: "PASS"
292316
}
@@ -312,8 +336,7 @@ issue_4859: {
312336
}
313337
expect: {
314338
(function f(a) {
315-
var d = 1 / 0, d = Infinity;
316-
console.log(d);
339+
console.log(Infinity);
317340
return f;
318341
})();
319342
}
@@ -448,3 +471,32 @@ issue_5187: {
448471
}
449472
expect_stdout: "42"
450473
}
474+
475+
issue_5195: {
476+
options = {
477+
hoist_props: true,
478+
hoist_vars: true,
479+
reduce_vars: true,
480+
side_effects: true,
481+
toplevel: true,
482+
unused: true,
483+
}
484+
input: {
485+
function f() {
486+
var a;
487+
do {
488+
var b = { p: a };
489+
} while (console.log(b += ""));
490+
}
491+
f();
492+
}
493+
expect: {
494+
(function() {
495+
var a, b;
496+
do {
497+
b = { p: a };
498+
} while (console.log(b += ""));
499+
})();
500+
}
501+
expect_stdout: "[object Object]"
502+
}

0 commit comments

Comments
 (0)