fix(codegen): wrong escape string (#3514)

close: #3492
This commit is contained in:
Dunqing 2024-06-03 15:48:01 +08:00 committed by GitHub
parent 079d42f68b
commit d8063b6210
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 2 deletions

View file

@ -1339,7 +1339,9 @@ fn print_unquoted_str<const MINIFY: bool>(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());
}
}
}
}

View file

@ -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);
}