Skip to content

Commit e130c08

Browse files
authored
[compiler] Avoid empty switch cases (#33625)
Small cosmetic win, found this when i was looking at some code internally with lots of cases that all share the same logic. Previously, all the but last one would have an empty block. --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/33625). * #33643 * #33642 * #33640 * __->__ #33625 * #33624
1 parent 9894c48 commit e130c08

File tree

6 files changed

+11
-21
lines changed

6 files changed

+11
-21
lines changed

compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1181,7 +1181,7 @@ function codegenTerminal(
11811181
? codegenPlaceToExpression(cx, case_.test)
11821182
: null;
11831183
const block = codegenBlock(cx, case_.block!);
1184-
return t.switchCase(test, [block]);
1184+
return t.switchCase(test, block.body.length === 0 ? [] : [block]);
11851185
}),
11861186
);
11871187
}

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/block-scoping-switch-variable-scoping.expect.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ function Component(props) {
5050
console.log(handlers.value);
5151
break bb0;
5252
}
53-
default: {
54-
}
53+
default:
5554
}
5655

5756
t0 = handlers;

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dominator.expect.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ function Component(props) {
6767
case "b": {
6868
break bb1;
6969
}
70-
case "c": {
71-
}
70+
case "c":
7271
default: {
7372
x = 6;
7473
}

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reverse-postorder.expect.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,8 @@ function Component(props) {
5050
case 1: {
5151
break bb0;
5252
}
53-
case 2: {
54-
}
55-
default: {
56-
}
53+
case 2:
54+
default:
5755
}
5856
} else {
5957
if (props.cond2) {

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/ssa-switch.expect.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ function foo() {
4141
case 2: {
4242
break bb0;
4343
}
44-
default: {
45-
}
44+
default:
4645
}
4746
}
4847

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/switch-with-fallthrough.expect.md

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,17 @@ export const FIXTURE_ENTRYPOINT = {
4343
```javascript
4444
function foo(x) {
4545
bb0: switch (x) {
46-
case 0: {
47-
}
48-
case 1: {
49-
}
46+
case 0:
47+
case 1:
5048
case 2: {
5149
break bb0;
5250
}
5351
case 3: {
5452
break bb0;
5553
}
56-
case 4: {
57-
}
58-
case 5: {
59-
}
60-
default: {
61-
}
54+
case 4:
55+
case 5:
56+
default:
6257
}
6358
}
6459

0 commit comments

Comments
 (0)