diff --git a/lib/compress.js b/lib/compress.js index 5328b517e0..6b2c936a46 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1089,6 +1089,7 @@ merge(Compressor.prototype, { for (var i = stat_index; !abort && i < statements.length; i++) { statements[i].transform(multi_replacer); } + value_def.single_use = false; } } if (replaced && !remove_candidate(candidate)) statements.splice(stat_index, 1); @@ -3958,13 +3959,14 @@ merge(Compressor.prototype, { && (exp === fn ? !fn.name : compressor.option("unused") && (def = exp.definition()).references.length == 1 - && !recursive_ref(compressor, def)) + && !recursive_ref(compressor, def) + && fn.is_constant_expression(exp.scope)) && !self.has_pure_annotation(compressor) && !fn.contains_this() && (scope = can_flatten_args(fn)) && (value = flatten_body(stat))) { var expressions = flatten_args(fn, scope); - expressions.push(value); + expressions.push(value.clone(true)); return make_sequence(self, expressions).optimize(compressor); } if (compressor.option("side_effects") && all(fn.body, is_empty)) { diff --git a/test/compress/functions.js b/test/compress/functions.js index 4c22652ee9..07147663e1 100644 --- a/test/compress/functions.js +++ b/test/compress/functions.js @@ -1258,3 +1258,168 @@ issue_2620_4: { } expect_stdout: "PASS" } + +issue_2630_1: { + options = { + collapse_vars: true, + inline: true, + passes: 2, + reduce_funcs: true, + reduce_vars: true, + sequences: true, + side_effects: true, + unused: true, + } + input: { + var c = 0; + (function() { + while (f()); + function f() { + var a = function() { + var b = c++, d = c = 1 + c; + }(); + } + })(); + console.log(c); + } + expect: { + var c = 0; + (function() { + while (c++, void (c = 1 + c)); + })(), + console.log(c); + } + expect_stdout: "2" +} + +issue_2630_2: { + options = { + collapse_vars: true, + inline: true, + passes: 2, + reduce_vars: true, + sequences: true, + unused: true, + } + input: { + var c = 0; + !function() { + while (f()) {} + function f() { + var not_used = function() { + c = 1 + c; + }(c = c + 1); + } + }(); + console.log(c); + } + expect: { + var c = 0; + !function() { + while (c += 1, void (c = 1 + c)); + }(), console.log(c); + } + expect_stdout: "2" +} + +issue_2630_3: { + options = { + inline: true, + reduce_vars: true, + unused: true, + } + input: { + var x = 2, a = 1; + (function() { + function f1(a) { + f2(); + --x >= 0 && f1({}); + } + f1(a++); + function f2() { + a++; + } + })(); + console.log(a); + } + expect: { + var x = 2, a = 1; + (function() { + function f1(a) { + f2(); + --x >= 0 && f1({}); + } + f1(a++); + function f2() { + a++; + } + })(); + console.log(a); + } + expect_stdout: "5" +} + +issue_2630_4: { + options = { + collapse_vars: true, + inline: true, + reduce_vars: true, + side_effects: true, + unused: true, + } + input: { + var x = 3, a = 1, b = 2; + (function() { + (function f1() { + while (--x >= 0 && f2()); + }()); + function f2() { + a++ + (b += a); + } + })(); + console.log(a); + } + expect: { + var x = 3, a = 1, b = 2; + (function() { + (function() { + while (--x >= 0 && void (a++, b += a)); + })(); + })(); + console.log(a); + } + expect_stdout: "2" +} + +issue_2630_5: { + options = { + collapse_vars: true, + inline: true, + reduce_vars: true, + unused: true, + } + input: { + var c = 1; + !function() { + do { + c *= 10; + } while (f()); + function f() { + return function() { + return (c = 2 + c) < 100; + }(c = c + 3); + } + }(); + console.log(c); + } + expect: { + var c = 1; + !function() { + do { + c *= 10; + } while (c += 3, (c = 2 + c) < 100); + }(); + console.log(c); + } + expect_stdout: "155" +}