Repository: swc-project/swc (rust)

Issue:
Minifier evaluates the code point of the character instead of char code for `charCodeAt`.
### Describe the bug

Code point is differ from char code which is returned by `String.prototype.charCodeAt`.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt#description

### Input code

```typescript
"use strict";
const k = (() => {
    return "👨‍👩‍👦".charCodeAt(0);
})
```


### Config

```json
{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false
    },
    "target": "es5",
    "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.5&code=H4sIAAAAAAAAA1MqLU5VKC4pykwuUbLmSs7PKy5RyFawVdDQ0FSwtVOo5lIAgqLUktKiPAWlD%2FMnrnjU0AukVkKoZUp6yRmJRc75KamOJRoGmtZctZoAAHg2xlIAAAA%3D&config=H4sIAAAAAAAAA32UO5LbMAyG%2Bz2FR3XaNDlAupyBQ5OgTIckNATotWbHdw%2F08CNrSJ2EDz9AAiC%2BPg6H7kyu%2B3X4kk%2F5GWwlqI9%2FsdBY2F7F0oHLllyNA3c%2F7vRMEwo2Ecym20I6trUHnlX0c3XvEiLB3X215VhiGF8TOsxDBaIXm1glXstQmP7Xr6zi5wS4tlf7ETGBLTvEWDKxMPRQtcAOU7IDgbnYqkSZTmprJNRSTLAxeDNUHFRefOSIRXK%2BUw%2FWG4ceFBQrOI4X0GSSS2SF5HrKfWbs4dj6fm7yNzVcbGqWlZxwnVsip1WinjASm9CKVsIFbtRggWtxvytjMBW41fKuO2MsGz35CyAVSJao2Axa3NkjyDxtqcOuMpYgI8ujwmW%2BtVsW6KWoJsagVHaqDFSOWjcr%2BOZgqqzTjrPijfJR9GAgBJkVJTR9RnYnLSmPA2BQgPTXBm2qFmAer3CDTw9iB%2F%2BWW7I%2BYKtHtnzapjTmI6adBBn4hH7HQVrBuI2rbInrsM1b8SCjAV51aTSD9yUgD4DRpHlZvs2GPA%2BJaPqEx%2BeaWB1ujyWcbemf733Zwx%2BrQ5fRtxmuG37q77SXpR0Zy5m6p%2Bd9FT9O30X6c5cvsW%2F%2FAHrujcQzBgAA

### SWC Info output

_No response_

### Expected behavior

the char code of the first character of "👨‍👩‍👦" is 55357.

### Actual behavior

minifier evaluates it as 128104, which is the code point of the first character.

### Version

1.5.5

### Additional context

_No response_

Pull request:
fix(es/minifier): Fix evaluation of `String.charCodeAt`
**Description:**


**Related issue:**

 - Closes #8943

Patch:
diff --git a/crates/swc_ecma_minifier/src/compress/pure/evaluate.rs b/crates/swc_ecma_minifier/src/compress/pure/evaluate.rs
index ee3e60ba70..d6f6e1db14 100644
--- a/crates/swc_ecma_minifier/src/compress/pure/evaluate.rs
+++ b/crates/swc_ecma_minifier/src/compress/pure/evaluate.rs
@@ -711,8 +711,13 @@ impl Pure<'_> {
 
                     let idx = value.round() as i64 as usize;
                     let c = s.value.chars().nth(idx);
+
                     match c {
                         Some(v) => {
+                            let mut b = [0; 2];
+                            v.encode_utf16(&mut b);
+                            let v = b[0];
+
                             self.changed = true;
                             report_change!(
                                 "evaluate: Evaluated `charCodeAt` of a string literal as `{}`",
@@ -738,6 +743,44 @@ impl Pure<'_> {
                 }
                 return;
             }
+            "codePointAt" => {
+                if call.args.len() != 1 {
+                    return;
+                }
+                if let Expr::Lit(Lit::Num(Number { value, .. })) = &*call.args[0].expr {
+                    if value.fract() != 0.0 {
+                        return;
+                    }
+
+                    let idx = value.round() as i64 as usize;
+                    let c = s.value.chars().nth(idx);
+                    match c {
+                        Some(v) => {
+                            self.changed = true;
+                            report_change!(
+                                "evaluate: Evaluated `codePointAt` of a string literal as `{}`",
+                                v
+                            );
+                            *e = Expr::Lit(Lit::Num(Number {
+                                span: call.span,
+                                value: v as usize as f64,
+                                raw: None,
+                            }))
+                        }
+                        None => {
+                            self.changed = true;
+                            report_change!(
+                                "evaluate: Evaluated `codePointAt` of a string literal as `NaN`",
+                            );
+                            *e = Expr::Ident(Ident::new(
+                                "NaN".into(),
+                                e.span().with_ctxt(SyntaxContext::empty()),
+                            ))
+                        }
+                    }
+                }
+                return;
+            }
             _ => return,
         };
 


Test patch:
diff --git a/crates/swc_ecma_minifier/tests/exec.rs b/crates/swc_ecma_minifier/tests/exec.rs
index e0c250035b..d3224adc01 100644
--- a/crates/swc_ecma_minifier/tests/exec.rs
+++ b/crates/swc_ecma_minifier/tests/exec.rs
@@ -11250,3 +11250,16 @@ fn issue_8937() {
         ",
     );
 }
+
+#[test]
+fn issue_8943() {
+    run_default_exec_test(
+        "
+        'use strict';
+        const k = (() => {
+            return '👨‍👩‍👦'.charCodeAt(0);
+        });
+        console.log(k());
+        ",
+    );
+}