diff --git a/lib/scope.js b/lib/scope.js index 359c4f97da3..3e18b6d3fd4 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -178,6 +178,13 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) { }); return true; } + if (node instanceof AST_Switch) { + node.expression.walk(tw); + walk_scope(function() { + walk_body(node, tw); + }); + return true; + } if (node instanceof AST_SwitchBranch) { node.init_vars(scope); descend(); diff --git a/test/compress/const.js b/test/compress/const.js index 8c56980fe17..df948ac4d9e 100644 --- a/test/compress/const.js +++ b/test/compress/const.js @@ -2204,3 +2204,30 @@ issue_5660: { } expect_stdout: true } + +issue_5787: { + options = { + unused: true, + } + input: { + console.log(function() { + const a = 42; + switch (a) { + case 42: + const a = "PASS"; + return a; + } + }()); + } + expect: { + console.log(function() { + const a = 42; + switch (a) { + case 42: + const a = "PASS"; + return a; + } + }()); + } + expect_stdout: true +} diff --git a/test/compress/let.js b/test/compress/let.js index ba6c4bc74ae..44c58431bf3 100644 --- a/test/compress/let.js +++ b/test/compress/let.js @@ -2537,3 +2537,33 @@ issue_5759: { ] node_version: ">=4" } + +issue_5787: { + options = { + unused: true, + } + input: { + "use strict"; + console.log(function() { + let a = 42; + switch (a) { + case 42: + let a = "PASS"; + return a; + } + }()); + } + expect: { + "use strict"; + console.log(function() { + let a = 42; + switch (a) { + case 42: + let a = "PASS"; + return a; + } + }()); + } + expect_stdout: true + node_version: ">=4" +} diff --git a/test/compress/rename.js b/test/compress/rename.js index 1d553f060b0..2f68b505f5b 100644 --- a/test/compress/rename.js +++ b/test/compress/rename.js @@ -739,3 +739,56 @@ issue_3480_ie8_toplevel: { } expect_stdout: "PASS" } + +issue_5787_1: { + rename = true + input: { + console.log(function() { + const a = 42; + switch (a) { + case 42: + const a = "PASS"; + return a; + } + }()); + } + expect: { + console.log(function() { + const a = 42; + switch (a) { + case 42: + const a = "PASS"; + return a; + } + }()); + } + expect_stdout: true +} + +issue_5787_2: { + rename = true + input: { + "use strict"; + console.log(function() { + let a = 42; + switch (a) { + case 42: + let a = "PASS"; + return a; + } + }()); + } + expect: { + "use strict"; + console.log(function() { + let a = 42; + switch (a) { + case 42: + let b = "PASS"; + return b; + } + }()); + } + expect_stdout: true + node_version: ">=4" +}