From d8063b621048bcf16959bf92b8a622541ccebaeb Mon Sep 17 00:00:00 2001 From: Dunqing Date: Mon, 3 Jun 2024 15:48:01 +0800 Subject: [PATCH] fix(codegen): wrong escape string (#3514) close: #3492 --- crates/oxc_codegen/src/gen.rs | 4 +++- crates/oxc_codegen/tests/mod.rs | 10 +++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/crates/oxc_codegen/src/gen.rs b/crates/oxc_codegen/src/gen.rs index 11b3d300f..b5101f9fd 100644 --- a/crates/oxc_codegen/src/gen.rs +++ b/crates/oxc_codegen/src/gen.rs @@ -1339,7 +1339,9 @@ fn print_unquoted_str(s: &str, quote: char, p: &mut Codegen< '\u{a0}' => { p.print_str(b"\\xA0"); } - _ => p.print_str(c.escape_default().to_string().as_bytes()), + _ => { + p.print_str(c.encode_utf8([0; 4].as_mut()).as_bytes()); + } } } } diff --git a/crates/oxc_codegen/tests/mod.rs b/crates/oxc_codegen/tests/mod.rs index 34aa8f0ae..a193e8ba6 100644 --- a/crates/oxc_codegen/tests/mod.rs +++ b/crates/oxc_codegen/tests/mod.rs @@ -44,7 +44,7 @@ fn string() { test("let x = ''", "let x = '';\n", None); test(r"let x = '\b'", "let x = '\\b';\n", None); test(r"let x = '\f'", "let x = '\\f';\n", None); - test("let x = '\t'", "let x = '\\t';\n", None); + test("let x = '\t'", "let x = '\t';\n", None); test(r"let x = '\v'", "let x = '\\v';\n", None); test("let x = '\\n'", "let x = '\\n';\n", None); test("let x = '\\''", "let x = \"'\";\n", None); @@ -390,3 +390,11 @@ const c2 = /* #__NO_SIDE_EFFECTS__ */ () => { ", ); } + +#[test] +fn unicode_escape() { + test("console.log('你好');", "console.log('你好');\n", None); + test("console.log('こんにちは');", "console.log('こんにちは');\n", None); + test("console.log('안녕하세요');", "console.log('안녕하세요');\n", None); + test("console.log('🧑‍🤝‍🧑');", "console.log('🧑‍🤝‍🧑');\n", None); +}