Repository: swc-project/swc (rust)

Issue:
compress typeof Statement bug
### Describe the bug

typeof identifier != void some
->
identifier

### Input code

```typescript
typeof __dirname != void 0
```


### Config

```json
{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false
    },
    "target": "es2022",
    "loose": false,
    "minify": {
      "mangle": false,
      "compress": {
        "arguments": false,
        "arrows": false,
        "booleans": false,
        "booleans_as_integers": false,
        "collapse_vars": false,
        "comparisons": false,
        "computed_props": false,
        "conditionals": false,
        "dead_code": false,
        "directives": false,
        "drop_console": false,
        "drop_debugger": false,
        "evaluate": false,
        "expression": false,
        "hoist_funs": false,
        "hoist_props": false,
        "hoist_vars": false,
        "if_return": false,
        "join_vars": false,
        "keep_classnames": false,
        "keep_fargs": false,
        "keep_fnames": false,
        "keep_infinity": false,
        "loops": false,
        "negate_iife": false,
        "properties": false,
        "reduce_funcs": false,
        "reduce_vars": false,
        "side_effects": false,
        "switches": false,
        "typeofs": false,
        "unsafe": false,
        "unsafe_arrows": false,
        "unsafe_comps": false,
        "unsafe_Function": false,
        "unsafe_math": false,
        "unsafe_symbols": false,
        "unsafe_methods": false,
        "unsafe_proto": false,
        "unsafe_regexp": false,
        "unsafe_undefined": false,
        "unused": false,
        "const_to_let": false,
        "pristine_globals": false
      },
      "format": {
        "wrapFuncArgs":true
      }
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": false
}
```


### Playground link

https://play.swc.rs/?version=1.3.74&code=H4sIAAAAAAAAAyupLEjNT1OIj0%2FJLMpLzE1VULRVKMvPTFEwAADFELHVGgAAAA%3D%3D&config=H4sIAAAAAAAAA32Vva7bMAyF9%2FsUgecORYYOd%2BvSrc8gKBblKJVFQ6SSGBd599J%2FaZpQWYKY36EpHVH018du15yobT53X%2FJXHgabCfL9WSI0JrZXiTTQ9pbaHAZuvm30RBPyNhLModtCGra5A56zaP99v18zmohIsGWssT6k4MfHmr1NXXySSbjFfshA9CCVqFQqPSSmZ%2F3MMl40cECMYNM7ZCyZkBg6yJqsxRjtQGDOtsJ7MTMQqkUmWhicGTIOuiC5wAGTxBXswDrTonsxaWIhQ8vhDGqi1JPERPhq8IYdHErXzW3wzOFsY7GspcJ1PhxZsgKPGIiNL6oZC6w5sdCKy8GbDFyyVvOEIdXy%2FgCIDdESJdurRs0KL81Vh29TQ%2FLS1zwqXC6ButMEnVhrQvCav5M%2FkDmoJTO40sJkcPsGV8yg4MCA99I1Kr4Ebo9qWR4HQK8ROWmrbmMBpnozVz5dkDf4l2yU9VZbFb3lY53S2B9QvVlbOvAR3RuBHAdjHWeZG9ehzktyIA0CTpUUUsF0b9kwmjjP1tcOkZsi7zRdxMPD3FgVt%2Fsg9ZjFnv%2FH6CXbYXL159zxnMs97WP7nV%2FQ9OjKPDvWT8bUA8ug%2F9H8E20z%2Fb7GJtDvLXNZ1e0vty2fLH8GAAA%3D

### Expected behavior

Whenever it would no crash

### Actual behavior

when __dirname no define ， it crash

### Version

1.3.74

### Additional context

_No response_

Pull request description:
fix(es/utils): Ignore `typeof Id` while preserving side effects
**Description:**

**Related issue:**

 - Closes #7750

Patch:
diff --git a/crates/swc_ecma_utils/src/lib.rs b/crates/swc_ecma_utils/src/lib.rs
index c43f0d7d3c..7d3f16be3b 100644
--- a/crates/swc_ecma_utils/src/lib.rs
+++ b/crates/swc_ecma_utils/src/lib.rs
@@ -2484,6 +2484,22 @@ impl ExprCtx {
             //TODO: Drop values if it does not have side effects.
             Expr::Cond(_) => to.push(Box::new(expr)),
 
+            Expr::Unary(UnaryExpr {
+                op: op!("typeof"),
+                arg,
+                ..
+            }) => {
+                // We should ignore side effect of `__dirname` in
+                //
+                // typeof __dirname != void 0
+                //
+                // https://github.com/swc-project/swc/pull/7763
+                if arg.is_ident() {
+                    return;
+                }
+                self.extract_side_effects_to(to, *arg)
+            }
+
             Expr::Unary(UnaryExpr { arg, .. }) => self.extract_side_effects_to(to, *arg),
 
             Expr::Bin(BinExpr { op, .. }) if op.may_short_circuit() => {


Test patch:
diff --git a/crates/swc/tests/fixture/issues-7xxx/7750/input/.swcrc b/crates/swc/tests/fixture/issues-7xxx/7750/input/.swcrc
new file mode 100644
index 0000000000..bd6c29e8cc
--- /dev/null
+++ b/crates/swc/tests/fixture/issues-7xxx/7750/input/.swcrc
@@ -0,0 +1,67 @@
+{
+    "jsc": {
+        "parser": {
+            "syntax": "ecmascript",
+            "jsx": false
+        },
+        "target": "es2022",
+        "loose": false,
+        "minify": {
+            "mangle": false,
+            "compress": {
+                "arguments": false,
+                "arrows": false,
+                "booleans": false,
+                "booleans_as_integers": false,
+                "collapse_vars": false,
+                "comparisons": false,
+                "computed_props": false,
+                "conditionals": false,
+                "dead_code": false,
+                "directives": false,
+                "drop_console": false,
+                "drop_debugger": false,
+                "evaluate": false,
+                "expression": false,
+                "hoist_funs": false,
+                "hoist_props": false,
+                "hoist_vars": false,
+                "if_return": false,
+                "join_vars": false,
+                "keep_classnames": false,
+                "keep_fargs": false,
+                "keep_fnames": false,
+                "keep_infinity": false,
+                "loops": false,
+                "negate_iife": false,
+                "properties": false,
+                "reduce_funcs": false,
+                "reduce_vars": false,
+                "side_effects": false,
+                "switches": false,
+                "typeofs": false,
+                "unsafe": false,
+                "unsafe_arrows": false,
+                "unsafe_comps": false,
+                "unsafe_Function": false,
+                "unsafe_math": false,
+                "unsafe_symbols": false,
+                "unsafe_methods": false,
+                "unsafe_proto": false,
+                "unsafe_regexp": false,
+                "unsafe_undefined": false,
+                "unused": false,
+                "const_to_let": false,
+                "pristine_globals": false
+            },
+            "format": {
+                "wrapFuncArgs": true
+            }
+        }
+    },
+    "module": {
+        "type": "es6"
+    },
+    "minify": false,
+    "isModule": false
+}
\ No newline at end of file
diff --git a/crates/swc/tests/fixture/issues-7xxx/7750/input/1.js b/crates/swc/tests/fixture/issues-7xxx/7750/input/1.js
new file mode 100644
index 0000000000..e40ca058f4
--- /dev/null
+++ b/crates/swc/tests/fixture/issues-7xxx/7750/input/1.js
@@ -0,0 +1,1 @@
+typeof __dirname != void 0
diff --git a/crates/swc/tests/fixture/issues-7xxx/7750/output/1.js b/crates/swc/tests/fixture/issues-7xxx/7750/output/1.js
new file mode 100644
index 0000000000..4203d4b4a3
--- /dev/null
+++ b/crates/swc/tests/fixture/issues-7xxx/7750/output/1.js
@@ -0,0 +1,1 @@
+true;