Repository: swc-project/swc (rust)

Issue:
[Bug]: Nullish coalescing in case clause in strict mode throws `ReferenceError`
### Describe the bug

`input.js` is expected to be terminated without error but the transpiled code throws `ReferenceError`.

### Input code

```typescript
"use strict";
switch ( 0 ) { case 0 ?? 0 : }
```


### Config

_No response_

### Playground link

https://play.swc.rs/?version=1.3.11&code=H4sIAAAAAAAAA1MqLU5VKC4pykwuUbLmKi7PLEnOUNBQMFDQVKhWSE4EShoo2NsDCSuFWgBB29PhLAAAAA%3D%3D&config=H4sIAAAAAAAAA0WMTQrEIAxG75K1286id5hDBCctFv9IUhgR714tlu7C915ehUMsrBUyshCPS0pU%2FMMKZAOKZZcVTNf6tKEXagYUeScdiiyd%2BZSEJjUQXHRbGSWbQmYSeRHG3T9m66GQfucYKmjJdAc%2F0N7G%2FHPynaLySe0Cj9ke9LUAAAA%3D

### Expected behavior

The input code terminates without error:
```console
$ node input.js
// terminates without error
```

### Actual behavior

The transpiled code throws `ReferenceError`:
```console
$ node output.js
output.js:3
    case (ref = 0) !== null && ref !== void 0 ? ref : 0:
              ^

ReferenceError: ref is not defined
    at Object.<anonymous> (/Users/p51lee/dev/esmeta-plrg/output.js:3:15)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

Node.js v18.11.0
```

### Version

1.3.11 (default SWC playground)

### Additional context

_No response_

Pull request:
fix(es/compat): Nullish coalescing in case condition with empty body
<!-- Note: CI script will automatically rebase your PR so please do not rebase unless required -->

<!-- Thanks for submitting a pull request! Please provide enough information so that others can review your pull request. -->

**Description:**

<!-- Explain the **motivation** for making this change. What existing problem does the pull request solve? -->

**BREAKING CHANGE:**

<!--
If this PR introduces a breaking change, it must contain a notice for it to be included in the CHANGELOG. Add description or remove entirely if not breaking.

You may need to update `MIGRATION.md` for the breaking changes.
-->

**Related issue (if exists):**
closes #6328 

Patch:
diff --git a/crates/swc_ecma_transforms_compat/src/es2020/nullish_coalescing/mod.rs b/crates/swc_ecma_transforms_compat/src/es2020/nullish_coalescing/mod.rs
index 0555316843..ece24839d6 100644
--- a/crates/swc_ecma_transforms_compat/src/es2020/nullish_coalescing/mod.rs
+++ b/crates/swc_ecma_transforms_compat/src/es2020/nullish_coalescing/mod.rs
@@ -74,8 +74,10 @@ impl VisitMut for NullishCoalescing {
 
     /// Prevents #1123
     fn visit_mut_switch_case(&mut self, s: &mut SwitchCase) {
+        // Prevents #6328
+        s.test.visit_mut_with(self);
         let old_vars = self.vars.take();
-        s.visit_mut_children_with(self);
+        s.cons.visit_mut_with(self);
         self.vars = old_vars;
     }
 


Test patch:
diff --git a/crates/swc_ecma_transforms_compat/src/es2020/nullish_coalescing/tests.rs b/crates/swc_ecma_transforms_compat/src/es2020/nullish_coalescing/tests.rs
index d4623f1b45..13a3c615c3 100644
--- a/crates/swc_ecma_transforms_compat/src/es2020/nullish_coalescing/tests.rs
+++ b/crates/swc_ecma_transforms_compat/src/es2020/nullish_coalescing/tests.rs
@@ -166,3 +166,14 @@ function foo(opts) {
 }
 "#
 );
+
+test!(
+    syntax(),
+    |_| tr(Default::default()),
+    issue_6328,
+    "switch ( 0 ) { case 0 ?? 0 : }",
+    r#"
+    var ref;
+    switch ( 0 ) { case (ref = 0) !== null && ref !== void 0 ? ref : 0: }
+"#
+);