From 88dfc4968330e87b6c55ce3f160e1e4420c0f01e Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Fri, 9 Sep 2022 21:14:33 +0100 Subject: [PATCH] fix corner case in `merge_vars` (#5657) fixes #5656 --- lib/compress.js | 8 +++++--- test/compress/const.js | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 6144b6cdfe5..157511c9b25 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1720,10 +1720,10 @@ Compressor.prototype.compress = function(node) { } function find_scope(compressor) { - var level = 0, node; - while (node = compressor.parent(level++)) { + var level = 0, node = compressor.self(); + do { if (node.variables) return node; - } + } while (node = compressor.parent(level++)); } function find_try(compressor, level, node, scope, may_throw, sync) { @@ -6733,6 +6733,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 = find_scope(tw); refs.push(sym); refs.end = seg; if (def.id in prev) { @@ -6747,6 +6748,7 @@ Compressor.prototype.compress = function(node) { return references[def.id] = false; } else { var refs = declarations.get(def.id) || []; + sym.scope = find_scope(tw); refs.push(sym); references[def.id] = refs; if (!read) { diff --git a/test/compress/const.js b/test/compress/const.js index f275ef5cba3..1ab63894e8a 100644 --- a/test/compress/const.js +++ b/test/compress/const.js @@ -2144,3 +2144,28 @@ issue_5591: { ] node_version: ">=4" } + +issue_5656: { + options = { + collapse_vars: true, + merge_vars: true, + } + input: { + console.log(function(a) { + var b = a; + b++; + { + const a = b; + } + }()); + } + expect: { + console.log(function(a) { + var b = a; + { + const a = ++b; + } + }()); + } + expect_stdout: true +}