From 68091dbf699bc575f73d60fd202c6da3eae4b272 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sun, 1 Nov 2020 06:34:07 +0000 Subject: fix corner case in `merge_vars` (#4256) fixes #4255 --- lib/compress.js | 9 +++++++-- test/compress/merge_vars.js | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 8eb36191..0e8b495f 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -4542,7 +4542,7 @@ merge(Compressor.prototype, { } if (node instanceof AST_LabeledStatement) { push(); - segment.block = node.body; + segment.block = node; node.body.walk(tw); pop(); return true; @@ -4737,7 +4737,12 @@ merge(Compressor.prototype, { function insert(target) { var stack = []; - while (!HOP(segment, "block") || segment.block !== target) { + while (true) { + if (HOP(segment, "block")) { + var block = segment.block; + if (block instanceof AST_LabeledStatement) block = block.body; + if (block === target) break; + } stack.push(segment); pop(); } diff --git a/test/compress/merge_vars.js b/test/compress/merge_vars.js index b61b486d..ec0410fa 100644 --- a/test/compress/merge_vars.js +++ b/test/compress/merge_vars.js @@ -3124,3 +3124,25 @@ issue_4253: { } expect_stdout: "undefined" } + +issue_4255: { + options = { + dead_code: true, + loops: true, + merge_vars: true, + toplevel: true, + } + input: { + L: for (var a = 2; --a;) + for (var b = 0; console.log(b); --b) + break L; + } + expect: { + L: for (var a = 2; --a;) { + var b = 0; + if (console.log(b)) + break L; + } + } + expect_stdout: "0" +} -- cgit v1.2.3