Skip to content

Commit 34b6143

Browse files
authoredJun 5, 2024··
fix scope assignment to switch expressions (#5826)
fixes #5787 fixes #5792
1 parent ffe0fe7 commit 34b6143

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed
 

‎lib/scope.js

+7
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,13 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) {
178178
});
179179
return true;
180180
}
181+
if (node instanceof AST_Switch) {
182+
node.expression.walk(tw);
183+
walk_scope(function() {
184+
walk_body(node, tw);
185+
});
186+
return true;
187+
}
181188
if (node instanceof AST_SwitchBranch) {
182189
node.init_vars(scope);
183190
descend();

‎test/compress/const.js

+27
Original file line numberDiff line numberDiff line change
@@ -2204,3 +2204,30 @@ issue_5660: {
22042204
}
22052205
expect_stdout: true
22062206
}
2207+
2208+
issue_5787: {
2209+
options = {
2210+
unused: true,
2211+
}
2212+
input: {
2213+
console.log(function() {
2214+
const a = 42;
2215+
switch (a) {
2216+
case 42:
2217+
const a = "PASS";
2218+
return a;
2219+
}
2220+
}());
2221+
}
2222+
expect: {
2223+
console.log(function() {
2224+
const a = 42;
2225+
switch (a) {
2226+
case 42:
2227+
const a = "PASS";
2228+
return a;
2229+
}
2230+
}());
2231+
}
2232+
expect_stdout: true
2233+
}

‎test/compress/let.js

+29
Original file line numberDiff line numberDiff line change
@@ -2537,3 +2537,32 @@ issue_5759: {
25372537
]
25382538
node_version: ">=4"
25392539
}
2540+
2541+
issue_5787: {
2542+
options = {
2543+
unused: true,
2544+
}
2545+
input: {
2546+
console.log(function() {
2547+
let a = 42;
2548+
switch (a) {
2549+
case 42:
2550+
// Node.js v4 (vm): SyntaxError: Identifier 'a' has already been declared
2551+
let a = "PASS";
2552+
return a;
2553+
}
2554+
}());
2555+
}
2556+
expect: {
2557+
console.log(function() {
2558+
let a = 42;
2559+
switch (a) {
2560+
case 42:
2561+
let a = "PASS";
2562+
return a;
2563+
}
2564+
}());
2565+
}
2566+
expect_stdout: "PASS"
2567+
node_version: ">=6"
2568+
}

‎test/compress/rename.js

+52
Original file line numberDiff line numberDiff line change
@@ -739,3 +739,55 @@ issue_3480_ie8_toplevel: {
739739
}
740740
expect_stdout: "PASS"
741741
}
742+
743+
issue_5787_1: {
744+
rename = true
745+
input: {
746+
console.log(function() {
747+
const a = 42;
748+
switch (a) {
749+
case 42:
750+
const a = "PASS";
751+
return a;
752+
}
753+
}());
754+
}
755+
expect: {
756+
console.log(function() {
757+
const a = 42;
758+
switch (a) {
759+
case 42:
760+
const a = "PASS";
761+
return a;
762+
}
763+
}());
764+
}
765+
expect_stdout: true
766+
}
767+
768+
issue_5787_2: {
769+
rename = true
770+
input: {
771+
console.log(function() {
772+
let a = 42;
773+
switch (a) {
774+
case 42:
775+
// Node.js v4 (vm): SyntaxError: Identifier 'a' has already been declared
776+
let a = "PASS";
777+
return a;
778+
}
779+
}());
780+
}
781+
expect: {
782+
console.log(function() {
783+
let a = 42;
784+
switch (a) {
785+
case 42:
786+
let b = "PASS";
787+
return b;
788+
}
789+
}());
790+
}
791+
expect_stdout: "PASS"
792+
node_version: ">=6"
793+
}

0 commit comments

Comments
 (0)
Please sign in to comment.