Repository: swc-project/swc (rust)

Issue:
minifier: invalid handling of exponentiate with `undefined` exponent 
### Describe the bug

An arithmetic operation with non-numeric value should result in `NaN`.

### Input code

```typescript
"use strict";
const k = (() => {
  let x = 1; x **= undefined; 
  return x;
})();
```


### Config

```json
{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false
    },
    "target": "es2022",
    "loose": false,
    "minify": {
      "compress": {
        "arguments": false,
        "arrows": true,
        "booleans": true,
        "booleans_as_integers": false,
        "collapse_vars": true,
        "comparisons": true,
        "computed_props": true,
        "conditionals": true,
        "dead_code": true,
        "directives": true,
        "drop_console": false,
        "drop_debugger": true,
        "evaluate": true,
        "expression": false,
        "hoist_funs": false,
        "hoist_props": true,
        "hoist_vars": false,
        "if_return": true,
        "join_vars": true,
        "keep_classnames": false,
        "keep_fargs": true,
        "keep_fnames": false,
        "keep_infinity": false,
        "loops": true,
        "negate_iife": true,
        "properties": true,
        "reduce_funcs": false,
        "reduce_vars": false,
        "side_effects": true,
        "switches": true,
        "typeofs": true,
        "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": true,
        "const_to_let": true,
        "pristine_globals": true
      },
      "mangle": false
    }
  },
  "module": {
    "type": "commonjs"
  },
  "minify": false,
  "isModule": false
}
```


### Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.5.2&code=H4sIAAAAAAAAAw3JMQrAIAxG4d1T%2FDhFt87BXkZTEEsKGkEovXudHnzPzyEY1ms2zy4%2FOgwNCUQB6cTrgFsMa9MB3o0xYWqRq6oUxv5dbHbFYvcFCvwDKVinUlIAAAA%3D&config=H4sIAAAAAAAAA32UO5LbMAyG%2Bz2FR3WKjMscIF3OwKFJUKZDEhoC9Fqz47sHeviRNeROwocfIAEQXx%2B7XXci1%2F3afcmn%2FAy2EtT7v1hoLGwvYunAZUuuxoG7Hzd6ogkFmwhm03UhHdvaA88q2v%2Fc71dFlxAJborVlmOJYXzO6TAPFYiebGKVkC1DYfpfv7KKnxPg2p7tB8QEtrwhxpKJhaGHqgV2mJIdCMzZViXKdFJbI6GWYoKNwZuh4qDy4iNHLJLzlXqw3jj0oKBYwXE8gyaTXCIrJNdT7jNjD4fW93Ofv6nhbFOzrOSEy9wSOa0S9YiR2IRWtBIucKMGC1yL%2B10Zg6nArZZX3Qlj2ejJXwCpQLJExWbQ4s4eQeZpSx3eKmMJMrI8KlzmW7tlgV6KamIMSmWnykDlqHWzgm8Opso67Tgr3igfRQ8GQpBZUULTZ2R31JLyOAAGBUh%2FbdCmagHm%2Fgo3%2BPQg3uDfckvWB2z1yJaP25TGfMD0JkEGPqJ%2F4yCtYNzGVbbEZdjmrXiQ0QCvujSawesSkAfAaNK8L19mQ56HRDR9wsNjTawO1%2Fsezrb0j%2Fe%2BrOKP1aHL6NsM1yU%2F9XdazdKOjOVE3cPztorvp%2B8i%2FbnJl9jXf3MIldo2BgAA

### SWC Info output

_No response_

### Expected behavior

`k = NaN`

### Actual behavior

minified to `k = 1`

### Version

1.5.2

### Additional context

_No response_

Pull request:
fix(es/minifier): Fix operand handling of `**`
**Description:**

**Related issue:**

 - Closes #8924

Patch:
diff --git a/crates/swc_ecma_minifier/src/compress/optimize/evaluate.rs b/crates/swc_ecma_minifier/src/compress/optimize/evaluate.rs
index b7052ee361..718cafff25 100644
--- a/crates/swc_ecma_minifier/src/compress/optimize/evaluate.rs
+++ b/crates/swc_ecma_minifier/src/compress/optimize/evaluate.rs
@@ -354,12 +354,20 @@ impl Optimizer<'_> {
                         self.changed = true;
                         report_change!("evaluate: Evaluated `{:?} ** {:?}`", l, r);
 
-                        let value = l.powf(r);
-                        *e = Expr::Lit(Lit::Num(Number {
-                            span: bin.span,
-                            value,
-                            raw: None,
-                        }));
+                        if l.is_nan() || r.is_nan() {
+                            *e = Expr::Ident(Ident::new(
+                                "NaN".into(),
+                                bin.span.with_ctxt(
+                                    SyntaxContext::empty().apply_mark(self.marks.unresolved_mark),
+                                ),
+                            ));
+                        } else {
+                            *e = Expr::Lit(Lit::Num(Number {
+                                span: bin.span,
+                                value: l.powf(r),
+                                raw: None,
+                            }));
+                        };
                     }
                 }
             }
diff --git a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs
index b1422fcc87..415b21eb98 100644
--- a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs
+++ b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs
@@ -1552,7 +1552,7 @@ impl Optimizer<'_> {
             }
         }
 
-        if match &*b {
+        match &*b {
             Expr::Arrow(..)
             | Expr::Fn(..)
             | Expr::Class(..)
@@ -1560,13 +1560,27 @@ impl Optimizer<'_> {
             | Expr::Await(..)
             | Expr::Yield(..)
             | Expr::Tpl(..)
-            | Expr::TaggedTpl(..) => true,
+            | Expr::TaggedTpl(..) => return Ok(false),
+
+            Expr::Assign(AssignExpr {
+                op: op!("**="),
+                right,
+                ..
+            })
+            | Expr::Bin(BinExpr {
+                op: op!("**"),
+                right,
+                ..
+            }) => {
+                if is_pure_undefined(&self.expr_ctx, right) {
+                    return Ok(false);
+                }
+            }
+
             Expr::Unary(UnaryExpr {
                 op: op!("delete"), ..
-            }) => true,
-            _ => false,
-        } {
-            return Ok(false);
+            }) => return Ok(false),
+            _ => {}
         }
 
         match a {


Test patch:
diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/8924/config.json b/crates/swc_ecma_minifier/tests/fixture/issues/8924/config.json
new file mode 100644
index 0000000000..7e144b291f
--- /dev/null
+++ b/crates/swc_ecma_minifier/tests/fixture/issues/8924/config.json
@@ -0,0 +1,47 @@
+{
+    "defaults": true,
+    "arguments": false,
+    "arrows": true,
+    "booleans": true,
+    "booleans_as_integers": false,
+    "collapse_vars": true,
+    "comparisons": true,
+    "computed_props": true,
+    "conditionals": true,
+    "dead_code": true,
+    "directives": true,
+    "drop_console": false,
+    "drop_debugger": true,
+    "evaluate": true,
+    "expression": false,
+    "hoist_funs": false,
+    "hoist_props": true,
+    "hoist_vars": false,
+    "if_return": true,
+    "join_vars": true,
+    "keep_classnames": false,
+    "keep_fargs": true,
+    "keep_fnames": false,
+    "keep_infinity": false,
+    "loops": true,
+    "negate_iife": true,
+    "properties": true,
+    "reduce_funcs": false,
+    "reduce_vars": false,
+    "side_effects": true,
+    "switches": true,
+    "typeofs": true,
+    "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": true,
+    "const_to_let": true,
+    "pristine_globals": true
+}
diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/8924/input.js b/crates/swc_ecma_minifier/tests/fixture/issues/8924/input.js
new file mode 100644
index 0000000000..0881567c83
--- /dev/null
+++ b/crates/swc_ecma_minifier/tests/fixture/issues/8924/input.js
@@ -0,0 +1,5 @@
+"use strict";
+const k = (() => {
+    let x = 1; x **= undefined;
+    return x;
+})();
\ No newline at end of file
diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/8924/output.js b/crates/swc_ecma_minifier/tests/fixture/issues/8924/output.js
new file mode 100644
index 0000000000..f010cc298f
--- /dev/null
+++ b/crates/swc_ecma_minifier/tests/fixture/issues/8924/output.js
@@ -0,0 +1,2 @@
+"use strict";
+const k = NaN;