Skip to content

Commit

Permalink
fix corner case in collapse_vars (#5639)
Browse files Browse the repository at this point in the history
fixes #5638
  • Loading branch information
alexlamsl authored Aug 31, 2022
1 parent d530f93 commit f63b7b0
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
52 changes: 52 additions & 0 deletions test/compress/collapse_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
54 changes: 54 additions & 0 deletions test/compress/hoist_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit f63b7b0

Please sign in to comment.