Repository: swc-project/swc (rust)

Issue:
minifier: invalid parenthesizing when inlining switch statement with case clause expression contains optional chaining 
### Describe the bug

Invalid parenthesizing makes expression invalid after compression.

### Input code

```typescript
const k = (() => {
  let x;
  switch (x) {
    case x?.x?.():
    default:
  }
})();
```


### Config

_No response_

### Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.4.13&code=H4sIAAAAAAAAA0vOzysuUchWsFXQ0NBUsLVTqOZSUMhJLVGosAYyisszS5IzFDQqNMHiCgrJicWpChX2ekCkoWkFFkpJTUsszSkBcWq5ajU1NK0Bkqo9h1QAAAA%3D&config=H4sIAAAAAAAAA32UO3LjMAyG%2B5zCozrFjostcoDtcgYOLYIyvSShIUDHmozvvtDDj40hdRI%2B%2FAAJgPh%2B2%2B2aE7XNx%2B5bPuWnt4Wg3P%2FFQkNmexFLA22y1JbQc%2FN%2BoycakbeRYDJdZ9KwLR3wpKL9r%2F1%2BUTQRkeCmWGwp5OCH55wtpr4A0ZNNrBKyJshM%2F%2BsXVvBrBFzqs%2F2AGMHmDWIsmZAZOiha4BZjtD2BOduiRBlPaksg1FKMsDI40xfsVZ5d4IBZcr5SB9aZFh0oKBRoOZxBk0kukWWS6yn3mbCDQ%2B26qc8%2F1HC2sVpWcsJlaomcVol6xEBsfM1aCWe4UoMZLsX9qQzeFOBa8qvuhCGv9OQvgFQgWqJsE2hxJw8v87Sm9pvKkL2MLA8Kl%2FnWbpmhk6KaELxS2bEyUDho3SzgagtjZVvtOAteKR8FBwa8l1lRQtNX4PaoJeWhB%2FQKkP5ar03VDMz9Fa7w8UFs4D9yS9YHbPFIlo%2FrlIZ0wLiRIAEf0W04SCsY13GRLXHp13nNDmQ0wKkulSbwugTkATCaOO3Ll9mQ5yERTRfx8FgTi8P1voeTzd3jvc%2Br%2BG1xaBK6OsFlyY%2F9nVfz7%2BbhdNvC94M3gT5vyjns9R%2F7ntJqMQYAAA%3D%3D

### SWC Info output

_No response_

### Expected behavior

```javascript
let k = (()=>{
    let x;
    x?.x?.();
})();
```
or
```javascript
let k = (()=>{
})();
```

### Actual behavior

```javascript
let k = (()=>{
    let x;
    (x?.x)();
})();
```
results in function call with `undefined` caller. (`undefined` is not a function Error occurred.)

### Version

1.4.13

### Additional context

_No response_

Pull request description:
fix(es/utils): Preserve optional chain effect
<!-- 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 #8844 

Patch:
diff --git a/crates/swc_ecma_utils/src/lib.rs b/crates/swc_ecma_utils/src/lib.rs
index 0db4830d7c..8d92575df9 100644
--- a/crates/swc_ecma_utils/src/lib.rs
+++ b/crates/swc_ecma_utils/src/lib.rs
@@ -2520,11 +2520,6 @@ impl ExprCtx {
                 to.push(Box::new(Expr::New(e)))
             }
             Expr::Member(_) | Expr::SuperProp(_) => to.push(Box::new(expr)),
-            Expr::OptChain(OptChainExpr { ref base, .. })
-                if matches!(&**base, OptChainBase::Member(_)) =>
-            {
-                to.push(Box::new(expr))
-            }
 
             // We are at here because we could not determine value of test.
             //TODO: Drop values if it does not have side effects.
@@ -2664,9 +2659,7 @@ impl ExprCtx {
             | Expr::TsSatisfies(TsSatisfiesExpr { expr, .. }) => {
                 self.extract_side_effects_to(to, *expr)
             }
-            Expr::OptChain(OptChainExpr { base: child, .. }) => {
-                self.extract_side_effects_to(to, (*child).into())
-            }
+            Expr::OptChain(..) => to.push(Box::new(expr)),
 
             Expr::Invalid(..) => unreachable!(),
         }


Test patch:
diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/8844/input.js b/crates/swc_ecma_minifier/tests/fixture/issues/8844/input.js
new file mode 100644
index 0000000000..b74e9da5b4
--- /dev/null
+++ b/crates/swc_ecma_minifier/tests/fixture/issues/8844/input.js
@@ -0,0 +1,7 @@
+const k = (() => {
+    let x;
+    switch (x) {
+        case x?.x?.():
+        default:
+    }
+})();
diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/8844/output.js b/crates/swc_ecma_minifier/tests/fixture/issues/8844/output.js
new file mode 100644
index 0000000000..a6899ca322
--- /dev/null
+++ b/crates/swc_ecma_minifier/tests/fixture/issues/8844/output.js
@@ -0,0 +1,2 @@
+let x;
+x?.x?.();