diff --git a/lib/compress.js b/lib/compress.js index 2a8977a1..c9357d29 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -5927,6 +5927,16 @@ Compressor.prototype.compress = function(node) { var references = Object.create(null); var prev = Object.create(null); var tw = new TreeWalker(function(node, descend) { + if (node.variables) { + if (node instanceof AST_BlockStatement) { + var save_scope = segment.scope; + segment.scope = node; + descend(); + segment.scope = save_scope; + return true; + } + segment.scope = node; + } if (node instanceof AST_Assign) { var lhs = node.left; var rhs = node.right; @@ -6298,6 +6308,7 @@ Compressor.prototype.compress = function(node) { var refs = references[def.id]; if (!refs) return; if (refs.start.block !== seg.block) return references[def.id] = false; + sym.scope = seg.scope; refs.push(sym); refs.end = seg; if (def.id in prev) { @@ -6312,6 +6323,7 @@ Compressor.prototype.compress = function(node) { return references[def.id] = false; } else { var refs = declarations.get(def.id) || []; + sym.scope = seg.scope; refs.push(sym); references[def.id] = refs; if (!read) { diff --git a/lib/output.js b/lib/output.js index b35fdb97..ea233d37 100644 --- a/lib/output.js +++ b/lib/output.js @@ -103,6 +103,7 @@ function OutputStream(options) { function make_indent(value) { if (typeof value == "number") return new Array(value + 1).join(" "); + if (!value) return ""; if (!/^\s*$/.test(value)) throw new Error("unsupported indentation: " + JSON.stringify("" + value)); return value; } diff --git a/test/compress/const.js b/test/compress/const.js index 92071056..55263da8 100644 --- a/test/compress/const.js +++ b/test/compress/const.js @@ -1813,3 +1813,33 @@ issue_5260: { ] node_version: ">=4" } + +issue_5319: { + options = { + collapse_vars: true, + merge_vars: true, + } + input: { + (function(a, c) { + var b = a, c = b; + { + const a = c; + console.log(c()); + } + })(function() { + return "PASS"; + }); + } + expect: { + (function(a, c) { + var b = a, c; + { + const a = c = b; + console.log(c()); + } + })(function() { + return "PASS"; + }); + } + expect_stdout: true +} diff --git a/test/compress/let.js b/test/compress/let.js index dba0b6ac..81940a92 100644 --- a/test/compress/let.js +++ b/test/compress/let.js @@ -1973,3 +1973,36 @@ issue_5260: { ] node_version: ">=4" } + +issue_5319: { + options = { + collapse_vars: true, + merge_vars: true, + } + input: { + "use strict"; + (function(a, c) { + var b = a, c = b; + { + let a = c; + console.log(c()); + } + })(function() { + return "PASS"; + }); + } + expect: { + "use strict"; + (function(a, c) { + var b = a, c; + { + let a = c = b; + console.log(c()); + } + })(function() { + return "PASS"; + }); + } + expect_stdout: "PASS" + node_version: ">=4" +}