diff --git a/lib/compress.js b/lib/compress.js index 8df9f4b2..1fd72d6d 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -320,7 +320,7 @@ merge(Compressor.prototype, { d.fixed = false; } else { var value = node.fixed_value(); - if (unused) { + if (unused && !compressor.exposed(d)) { d.single_use = value && d.references.length == 1 && loop_ids[d.id] === in_loop diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js index d78f1174..d84d9a4d 100644 --- a/test/compress/reduce_vars.js +++ b/test/compress/reduce_vars.js @@ -3111,3 +3111,88 @@ const_expr_2: { } expect_stdout: "2 2" } + +issue_2406_1: { + options = { + reduce_vars: true, + toplevel: false, + unused: true, + } + input: { + const c = { + fn: function() { + return this; + } + }; + let l = { + fn: function() { + return this; + } + }; + var v = { + fn: function() { + return this; + } + }; + console.log(c.fn(), l.fn(), v.fn()); + } + expect: { + const c = { + fn: function() { + return this; + } + }; + let l = { + fn: function() { + return this; + } + }; + var v = { + fn: function() { + return this; + } + }; + console.log(c.fn(), l.fn(), v.fn()); + } +} + +issue_2406_2: { + options = { + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + const c = { + fn: function() { + return this; + } + }; + let l = { + fn: function() { + return this; + } + }; + var v = { + fn: function() { + return this; + } + }; + console.log(c.fn(), l.fn(), v.fn()); + } + expect: { + console.log({ + fn: function() { + return this; + } + }.fn(), { + fn: function() { + return this; + } + }.fn(), { + fn: function() { + return this; + } + }.fn()); + } +}