From 34b6143306474b5dfc4a4f5bbce855db4fd3bafd Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Wed, 5 Jun 2024 22:05:16 +0300 Subject: [PATCH] fix scope assignment to `switch` expressions (#5826) fixes #5787 fixes #5792 --- lib/scope.js | 7 ++++++ test/compress/const.js | 27 +++++++++++++++++++++ test/compress/let.js | 29 +++++++++++++++++++++++ test/compress/rename.js | 52 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+) 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..7205090cec0 100644 --- a/test/compress/let.js +++ b/test/compress/let.js @@ -2537,3 +2537,32 @@ issue_5759: { ] node_version: ">=4" } + +issue_5787: { + options = { + unused: true, + } + input: { + console.log(function() { + let a = 42; + switch (a) { + case 42: + // Node.js v4 (vm): SyntaxError: Identifier 'a' has already been declared + let a = "PASS"; + return a; + } + }()); + } + expect: { + console.log(function() { + let a = 42; + switch (a) { + case 42: + let a = "PASS"; + return a; + } + }()); + } + expect_stdout: "PASS" + node_version: ">=6" +} diff --git a/test/compress/rename.js b/test/compress/rename.js index 1d553f060b0..1fcf33a50f8 100644 --- a/test/compress/rename.js +++ b/test/compress/rename.js @@ -739,3 +739,55 @@ 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: { + console.log(function() { + let a = 42; + switch (a) { + case 42: + // Node.js v4 (vm): SyntaxError: Identifier 'a' has already been declared + let a = "PASS"; + return a; + } + }()); + } + expect: { + console.log(function() { + let a = 42; + switch (a) { + case 42: + let b = "PASS"; + return b; + } + }()); + } + expect_stdout: "PASS" + node_version: ">=6" +}