@samitouri / QOS-React-1 / commits / e130c08b06

[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

Joseph Savona committed Jun 25, 2025 at 11:10 UTC e130c08b06470b5fc4ec8095310d19e782924427
6 files changed +11 -21
compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts
+1 -1
@@ -1181,7 +1181,7 @@ function codegenTerminal(
1181 ? codegenPlaceToExpression(cx, case_.test)
1182 : null;
1183 const block = codegenBlock(cx, case_.block!);
1184 - return t.switchCase(test, [block]);
1184 + return t.switchCase(test, block.body.length === 0 ? [] : [block]);
1185 }),
1186 );
1187 }
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/block-scoping-switch-variable-scoping.expect.md
+1 -2
@@ -50,8 +50,7 @@ function Component(props) {
50 console.log(handlers.value);
51 break bb0;
52 }
53 - default: {
54 - }
53 + default:
54 }
55
56 t0 = handlers;
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dominator.expect.md
+1 -2
@@ -67,8 +67,7 @@ function Component(props) {
67 case "b": {
68 break bb1;
69 }
70 - case "c": {
71 - }
70 + case "c":
71 default: {
72 x = 6;
73 }
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reverse-postorder.expect.md
+2 -4
@@ -50,10 +50,8 @@ function Component(props) {
50 case 1: {
51 break bb0;
52 }
53 - case 2: {
54 - }
55 - default: {
56 - }
53 + case 2:
54 + default:
55 }
56 } else {
57 if (props.cond2) {
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/ssa-switch.expect.md
+1 -2
@@ -41,8 +41,7 @@ function foo() {
41 case 2: {
42 break bb0;
43 }
44 - default: {
45 - }
44 + default:
45 }
46 }
47
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/switch-with-fallthrough.expect.md
+5 -10
@@ -43,22 +43,17 @@ export const FIXTURE_ENTRYPOINT = {
43 ```javascript
44 function foo(x) {
45 bb0: switch (x) {
46 - case 0: {
47 - }
48 - case 1: {
49 - }
46 + case 0:
47 + case 1:
48 case 2: {
49 break bb0;
50 }
51 case 3: {
52 break bb0;
53 }
56 - case 4: {
57 - }
58 - case 5: {
59 - }
60 - default: {
61 - }
54 + case 4:
55 + case 5:
56 + default:
57 }
58 }
59