From 684c4e3b0649c08c64d8202f9165435570b42d36 Mon Sep 17 00:00:00 2001 From: alexlamsl Date: Sat, 25 Mar 2017 19:54:45 +0800 Subject: [PATCH] fix `reduce_vars` on `AST_Switch` Take conditional nature of switch branches into account. fixes #1670 --- lib/compress.js | 2 +- test/compress/reduce_vars.js | 118 +++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 1 deletion(-) diff --git a/lib/compress.js b/lib/compress.js index e12c5eb0..bea9c9a4 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -341,7 +341,7 @@ merge(Compressor.prototype, { pop(); return true; } - if (node instanceof AST_Catch) { + if (node instanceof AST_Catch || node instanceof AST_SwitchBranch) { push(); descend(); pop(); diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js index 3d5612cf..b6717a93 100644 --- a/test/compress/reduce_vars.js +++ b/test/compress/reduce_vars.js @@ -1364,3 +1364,121 @@ issue_1606: { } } } + +issue_1670_1: { + options = { + comparisons: true, + conditionals: true, + evaluate: true, + dead_code: true, + reduce_vars: true, + unused: true, + } + input: { + (function f() { + switch (1) { + case 0: + var a = true; + break; + default: + if (typeof a === "undefined") console.log("PASS"); + else console.log("FAIL"); + } + })(); + } + expect: { + (function() { + var a; + void 0 === a ? console.log("PASS") : console.log("FAIL"); + })(); + } + expect_stdout: true +} + +issue_1670_2: { + options = { + conditionals: true, + evaluate: true, + dead_code: true, + passes: 2, + reduce_vars: true, + unused: true, + } + input: { + (function f() { + switch (1) { + case 0: + var a = true; + break; + default: + if (typeof a === "undefined") console.log("PASS"); + else console.log("FAIL"); + } + })(); + } + expect: { + (function() { + console.log("PASS"); + })(); + } + expect_stdout: true +} + +issue_1670_3: { + options = { + comparisons: true, + conditionals: true, + evaluate: true, + dead_code: true, + reduce_vars: true, + unused: true, + } + input: { + (function f() { + switch (1) { + case 0: + var a = true; + break; + case 1: + if (typeof a === "undefined") console.log("PASS"); + else console.log("FAIL"); + } + })(); + } + expect: { + (function() { + var a; + void 0 === a ? console.log("PASS") : console.log("FAIL"); + })(); + } + expect_stdout: true +} + +issue_1670_4: { + options = { + conditionals: true, + evaluate: true, + dead_code: true, + passes: 2, + reduce_vars: true, + unused: true, + } + input: { + (function f() { + switch (1) { + case 0: + var a = true; + break; + case 1: + if (typeof a === "undefined") console.log("PASS"); + else console.log("FAIL"); + } + })(); + } + expect: { + (function() { + console.log("PASS"); + })(); + } + expect_stdout: true +}