diff --git a/lib/compress.js b/lib/compress.js index 41e5d4cd..19b58fc4 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -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; } diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js index 4c705abc..16dcd81d 100644 --- a/test/compress/collapse_vars.js +++ b/test/compress/collapse_vars.js @@ -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" +} diff --git a/test/compress/hoist_vars.js b/test/compress/hoist_vars.js index d0a73d49..73f4a69f 100644 --- a/test/compress/hoist_vars.js +++ b/test/compress/hoist_vars.js @@ -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" +}