From 1891afa2ad27f183e56adcd288dd3a1ae0c5b367 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Mon, 27 Nov 2023 10:54:45 +0900 Subject: [PATCH] fix(es/codegen): Fix panic due to `\\ud` (#8346) **Related issue:** - Closes #8345 --- crates/swc/tests/fixture/issues-8xxx/8345/input/1.js | 2 ++ crates/swc/tests/fixture/issues-8xxx/8345/output/1.js | 2 ++ crates/swc_ecma_codegen/src/lib.rs | 6 ++++-- 3 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 crates/swc/tests/fixture/issues-8xxx/8345/input/1.js create mode 100644 crates/swc/tests/fixture/issues-8xxx/8345/output/1.js 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 00000000000..c6340e77afc --- /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 00000000000..37a65bc97cb --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8345/output/1.js @@ -0,0 +1,2 @@ +var data = "\\ud"; +console.log(data); diff --git a/crates/swc_ecma_codegen/src/lib.rs b/crates/swc_ecma_codegen/src/lib.rs index 7691048a754..101c44b01dc 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("\\\\");