diff --git a/lib/compress.js b/lib/compress.js index 918366e2..cd8dd459 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -763,6 +763,22 @@ Compressor.prototype.compress = function(node) { }; } + function make_fixed_default(compressor, node, save) { + var prev_save, prev_seq; + return function() { + var current = save(); + var ev; + if (!is_undefined(current, compressor) && (ev = fuzzy_eval(compressor, current, true)) !== undefined) { + return ev instanceof AST_Node ? node : current; + } + if (prev_save !== current) { + prev_save = current; + prev_seq = make_sequence(node, [ current, node.value ]); + } + return prev_seq; + }; + } + function scan_declaration(tw, compressor, lhs, fixed, visit) { var scanner = new TreeWalker(function(node) { if (node instanceof AST_DefaultValue) { @@ -771,14 +787,7 @@ Compressor.prototype.compress = function(node) { node.value.walk(tw); pop(tw); var save = fixed; - if (save) fixed = make_fixed(save, function(value) { - var ev; - if (is_undefined(value, compressor) - || (ev = fuzzy_eval(compressor, value, true)) === undefined) { - return make_sequence(node, [ value, node.value ]); - } - return ev instanceof AST_Node ? node : value; - }); + if (save) fixed = make_fixed_default(compressor, node, save); node.name.walk(scanner); fixed = save; return true; diff --git a/test/compress/default-values.js b/test/compress/default-values.js index 27aacbb6..0dcbd862 100644 --- a/test/compress/default-values.js +++ b/test/compress/default-values.js @@ -2209,3 +2209,33 @@ issue_5340_3: { expect_stdout: "undefined" node_version: ">=6" } + +issue_5407: { + options = { + evaluate: true, + reduce_vars: true, + } + input: { + (function(a) { + for (var i = 0; i < 2; i++) + (function(b = 4) { + console.log(b); + a = 2; + })(a); + })(); + } + expect: { + (function(a) { + for (var i = 0; i < 2; i++) + (function(b = 4) { + console.log(b); + a = 2; + })(a); + })(); + } + expect_stdout: [ + "4", + "2", + ] + node_version: ">=6" +}