Skip to content

Commit

Permalink
fix scope assignment to switch expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Jun 5, 2024
1 parent ffe0fe7 commit 553503b
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
27 changes: 27 additions & 0 deletions test/compress/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
30 changes: 30 additions & 0 deletions test/compress/let.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
53 changes: 53 additions & 0 deletions test/compress/rename.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit 553503b

Please sign in to comment.