From 4291ecc77c6573a97ae151e9d49b4898a888a233 Mon Sep 17 00:00:00 2001 From: alexlamsl Date: Sun, 26 Mar 2017 15:48:10 +0800 Subject: [PATCH] case expression with side effects can only be de-duplicated at the tail --- lib/compress.js | 15 ++++++++------- test/compress/switch.js | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 6fa0f7b3..1d009726 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -2542,22 +2542,23 @@ merge(Compressor.prototype, { continue; } } + var case_side_effects = branch instanceof AST_Case && branch.expression.has_side_effects(compressor); if (aborts(branch)) { var key = make_node(AST_BlockStatement, branch, branch).print_to_string(); var block; if (!fallthrough && (block = blocks[key])) { - block.body = []; - body.splice(body.indexOf(block) + 1, 0, branch); - } else { - body.push(branch); - } + var insert = body.indexOf(block) + 1; + if (insert == body.length || !case_side_effects) { + block.body = []; + body.splice(insert, 0, branch); + } else body.push(branch); + } else body.push(branch); fallthrough = false; } else { body.push(branch); fallthrough = true; } - if (branch instanceof AST_Case && branch.expression.has_side_effects(compressor)) - blocks = Object.create(null); + if (case_side_effects) blocks = Object.create(null); if (!fallthrough) blocks[key] = branch; } for (; i < len && fallthrough; i++) { diff --git a/test/compress/switch.js b/test/compress/switch.js index 758a80ad..546e1613 100644 --- a/test/compress/switch.js +++ b/test/compress/switch.js @@ -478,7 +478,7 @@ issue_1679: { expect_stdout: true } -issue_1680: { +issue_1680_1: { options = { dead_code: true, evaluate: true, @@ -518,3 +518,38 @@ issue_1680: { "5", ] } + +issue_1680_2: { + options = { + dead_code: true, + } + input: { + var a = 100, b = 10; + switch (b) { + case a--: + break; + case b: + var c; + break; + case a: + break; + case a--: + break; + } + console.log(a, b); + } + expect: { + var a = 100, b = 10; + switch (b) { + case a--: + case a: + break; + case b: + var c; + break; + case a--: + } + console.log(a, b); + } + expect_stdout: true +}