Repository: swc-project/swc (rust)

Issue:
Panic while compiling valid JavaScript string
### Describe the bug

If a JS string with the character sequence `\\ud` is encountered, the compiler panicks and throws the error: ```byte index 6 is out of bounds of `\ud` ```

### Input code

```typescript
const data = '\\ud';
console.log(data);
```


### Config

```json
```


### Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.3.94-nightly-20231016.1&code=H4sIAAAAAAAAA0vOzysuUUhJLElUsFVQj4kpTVG35uVKBorm56Tq5eSna4DkNIFiALR31IIqAAAA&config=H4sIAAAAAAAAA1WPSw7DIAwF9zkF8rrbdtE79BAWdSIifrKJVBTl7iUE0maH3xsz8jooBbNoeKq1PMsQkYX4nEsi2Sf8lARIOxTNJia49XaWvRrRCtVoOxpIyBOluiX3hoMNQajjLXPGmzH%2FC3VwkUnkCu4o%2BsnSVTc0JbjwXmrZDkk50qF%2FwA%2FqsvNjMPLqm4kXGrYvhlQioBQBAAA%3D

### SWC Info output

    Operating System:
        Platform: win32
        Arch: x64
        Machine Type: x86_64
        Version: Windows 11 Pro
        CPU: (12 cores)
            Models: AMD Ryzen 5 3600 6-Core Processor

    Binaries:
        Node: 20.8.0
        npm: 10.1.0
        Yarn: N/A
        pnpm: 8.6.10

    Relevant Packages:
        @swc/core: 1.3.99
        @swc/helpers: N/A
        @swc/types: 0.1.5


    SWC Config:
        output: N/A
        .swcrc path: N/A

    Next.js info:
        output: N/A

### Expected behavior

The code should be compiled without errors.

### Actual behavior

Panic

### Version

1.3.99

### Additional context

_No response_

Pull request description:
fix(es/codegen): Fix panic due to `\\ud`
**Description:**



**Related issue:**

 - Closes #8345

Patch:
diff --git a/crates/swc_ecma_codegen/src/lib.rs b/crates/swc_ecma_codegen/src/lib.rs
index 7691048a75..101c44b01d 100644
--- a/crates/swc_ecma_codegen/src/lib.rs
+++ b/crates/swc_ecma_codegen/src/lib.rs
@@ -3916,9 +3916,9 @@ fn get_quoted_utf16(v: &str, ascii_only: bool, target: EsVersion) -> String {
                                 2..6
                             };
 
-                            let val_str = &inner_buf[range];
-
                             if is_valid {
+                                let val_str = &inner_buf[range];
+
                                 let v = u32::from_str_radix(val_str, 16).unwrap_or_else(|err| {
                                     unreachable!(
                                         "failed to parse {} as a hex value: {:?}",
@@ -3939,6 +3939,8 @@ fn get_quoted_utf16(v: &str, ascii_only: bool, target: EsVersion) -> String {
                                 } else {
                                     buf.push_str("\\\\");
                                 }
+                            } else {
+                                buf.push_str("\\\\")
                             }
                         } else if is_curly {
                             buf.push_str("\\\\");


Test patch:
diff --git a/crates/swc/tests/fixture/issues-8xxx/8345/input/1.js b/crates/swc/tests/fixture/issues-8xxx/8345/input/1.js
new file mode 100644
index 0000000000..c6340e77af
--- /dev/null
+++ b/crates/swc/tests/fixture/issues-8xxx/8345/input/1.js
@@ -0,0 +1,2 @@
+const data = '\\ud';
+console.log(data);
diff --git a/crates/swc/tests/fixture/issues-8xxx/8345/output/1.js b/crates/swc/tests/fixture/issues-8xxx/8345/output/1.js
new file mode 100644
index 0000000000..37a65bc97c
--- /dev/null
+++ b/crates/swc/tests/fixture/issues-8xxx/8345/output/1.js
@@ -0,0 +1,2 @@
+var data = "\\ud";
+console.log(data);