fix corner case in collapse_vars (#5639)

fixes #5638
This commit is contained in:
Alex Lam S.L
2022-08-31 01:44:04 +01:00
committed by GitHub
parent d530f9332c
commit f63b7b079d
3 changed files with 115 additions and 3 deletions

View File

@@ -3416,9 +3416,15 @@ Compressor.prototype.compress = function(node) {
if (def.references.length - def.replaced == referenced) return true;
if (!def.fixed) return false;
if (!lhs.fixed) return false;
if (def.references.filter(function(ref) {
return ref.fixed === lhs.fixed;
}).length != referenced) return false;
var matched = 0;
if (!all(def.references, function(ref, index) {
var fixed = ref.fixed;
if (!fixed) return false;
if (fixed.to_binary || fixed.to_prefix) return false;
if (fixed === lhs.fixed) matched++;
return true;
})) return false;
if (matched != referenced) return false;
verify_ref = true;
return true;
}

View File

@@ -10000,3 +10000,55 @@ issue_5568: {
}
expect_stdout: "PASS"
}
issue_5638_1: {
options = {
collapse_vars: true,
pure_getters: "strict",
reduce_vars: true,
toplevel: true,
}
input: {
var log = console.log;
var a = { foo: 42 }, b;
for (var k in a) {
b = a[k];
log(k || b, b++);
}
}
expect: {
var log = console.log;
var a = { foo: 42 }, b;
for (var k in a) {
b = a[k];
log(k || b, b++);
}
}
expect_stdout: "foo 42"
}
issue_5638_2: {
options = {
collapse_vars: true,
pure_getters: "strict",
reduce_vars: true,
toplevel: true,
}
input: {
var log = console.log;
var a = { foo: 6 }, b;
for (var k in a) {
b = a[k];
log(k || b, b *= 7);
}
}
expect: {
var log = console.log;
var a = { foo: 6 }, b;
for (var k in a) {
b = a[k];
log(k || b, b *= 7);
}
}
expect_stdout: "foo 42"
}

View File

@@ -701,3 +701,57 @@ issue_5626: {
}
expect_stdout: "PASS"
}
issue_5638_1: {
options = {
collapse_vars: true,
hoist_vars: true,
pure_getters: "strict",
reduce_vars: true,
toplevel: true,
}
input: {
var log = console.log;
var o = { foo: 42 };
for (var k in o) {
var v = o[k];
log(k || v, v++);
}
}
expect: {
var log, o, k, v;
log = console.log;
for (k in o = { foo: 42 }) {
v = o[k];
log(k || v, v++);
}
}
expect_stdout: "foo 42"
}
issue_5638_2: {
options = {
collapse_vars: true,
hoist_vars: true,
pure_getters: "strict",
reduce_vars: true,
toplevel: true,
}
input: {
var log = console.log;
var o = { foo: 6 };
for (var k in o) {
var v = o[k];
log(k || v, v *= 7);
}
}
expect: {
var log, o, k, v;
log = console.log;
for (k in o = { foo: 6 }) {
v = o[k];
log(k || v, v *= 7);
}
}
expect_stdout: "foo 42"
}