Skip to content

fix(es/codegen): Handle unicode escapes correctly #10423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/beige-walls-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_core: patch
swc_ecma_codegen: patch
---

fix(es/codegen): Handle unicode escapes correctly
13 changes: 11 additions & 2 deletions crates/swc_ecma_codegen/src/lit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,8 @@ pub fn get_quoted_utf16(v: &str, ascii_only: bool, target: EsVersion) -> (AsciiC
}

if let Some(c @ 'D' | c @ 'd') = next {
let mut inner_buf = String::with_capacity(8);
let mut inner_buf = String::with_capacity(9);
inner_buf.push('\\');
inner_buf.push('\\');
inner_buf.push('u');

Expand Down Expand Up @@ -451,24 +452,32 @@ pub fn get_quoted_utf16(v: &str, ascii_only: bool, target: EsVersion) -> (AsciiC
let range = if is_curly {
3..(inner_buf.len() - 1)
} else {
2..6
3..7
};

if is_valid {
let val_str = &inner_buf[range];
dbg!(&val_str);
if let Ok(v) = u32::from_str_radix(val_str, 16) {
dbg!(&v);

if v > 0xffff {
dbg!("push");
buf.push_str(&inner_buf);
let end = if is_curly { 7 } else { 5 };
for _ in 0..end {
iter.next();
}
} else if (0xd800..=0xdfff).contains(&v) {
dbg!("push: \\");
buf.push('\\');
} else {
dbg!("push: \\\\");
buf.push_str("\\\\");
}
} else {
dbg!("push: \\\\ (invalid)");

buf.push_str("\\\\");
}
} else {
Expand Down
45 changes: 45 additions & 0 deletions crates/swc_ecma_minifier/tests/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9790,6 +9790,26 @@ fn try_catch_5() {
run_default_exec_test(src);
}

#[test]
fn issue_4120_1() {
run_default_exec_test(
r#"
const a = "\u0591-\u06ef\u06fa-\u08ff\u200f\ud802-\ud803\ud83a-\ud83b\ufb1d-\ufdff\ufe70-\ufefc";
console.log(a);
"#,
);
}

#[test]
fn issue_4120_2() {
run_default_exec_test(
r#"
const b = "A-Za-z\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u02b8\u0300-\u0590\u0900-\u1fff\u200e\u2c00-\ud801\ud804-\ud839\ud83c-\udbff\uf900-\ufb1c\ufe00-\ufe6f\ufefd-\uffff";
console.log(b);
"#,
);
}

#[test]
fn issue_4444_1() {
let src = r#"
Expand Down Expand Up @@ -10946,6 +10966,22 @@ fn issue_7274() {
);
}

#[test]
fn issue_7678() {
run_default_exec_test(
r#"
let str = "\\uD83D\\uDC68\\u200D\\uD83D\\uDE80";

let obj = {
"\\uD83D\\uDC68\\u200D\\uD83D\\uDE80": "wrong"
};

console.log(str);
console.log(obj);
"#,
);
}

#[test]
fn issue_8119_1() {
run_exec_test(
Expand Down Expand Up @@ -11477,3 +11513,12 @@ fn issue_10133() {
",
);
}

#[test]
fn issue_10353() {
run_default_exec_test(
r#"
console.log("\\uD83D\\uDE42");
"#,
);
}
4 changes: 2 additions & 2 deletions crates/swc_ecma_minifier/tests/libs-size.snapshot.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
| File | Original Size | Compressed Size | Gzipped Size |
| --- | --- | --- | --- |
| antd.js | 6.38 MiB | 2.06 MiB | 445.44 KiB |
| antd.js | 6.38 MiB | 2.06 MiB | 445.45 KiB |
| d3.js | 542.74 KiB | 261.63 KiB | 85.57 KiB |
| echarts.js | 3.41 MiB | 977.96 KiB | 314.33 KiB |
| jquery.js | 280.89 KiB | 87.80 KiB | 30.21 KiB |
| lodash.js | 531.35 KiB | 68.91 KiB | 24.60 KiB |
| lodash.js | 531.35 KiB | 68.92 KiB | 24.60 KiB |
| moment.js | 169.83 KiB | 57.40 KiB | 18.26 KiB |
| react.js | 70.45 KiB | 22.44 KiB | 8.04 KiB |
| terser.js | 1.08 MiB | 446.78 KiB | 120.52 KiB |
Expand Down
Loading