Skip to content

fix(es/minifier): Preserve unicode escape sequences in string literals #10829

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
76 changes: 1 addition & 75 deletions crates/swc_ecma_codegen/src/lit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,81 +401,7 @@ pub fn get_quoted_utf16(v: &str, ascii_only: bool, target: EsVersion) -> (AsciiC
let next = iter.peek();
match next {
Some('u') => {
let mut inner_iter = iter.clone();
inner_iter.next();

let mut is_curly = false;
let mut next = inner_iter.peek();

if next == Some(&'{') {
is_curly = true;
inner_iter.next();
next = inner_iter.peek();
} else if next != Some(&'D') && next != Some(&'d') {
buf.push('\\');
}

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

if is_curly {
inner_buf.push('{');
}

inner_buf.push(*c);
inner_iter.next();

let mut is_valid = true;
for _ in 0..3 {
match inner_iter.next() {
Some(c @ '0'..='9') | Some(c @ 'a'..='f')
| Some(c @ 'A'..='F') => {
inner_buf.push(c);
}
_ => {
is_valid = false;
break;
}
}
}

if is_curly {
inner_buf.push('}');
}

let range = if is_curly {
3..(inner_buf.len() - 1)
} else {
2..6
};

if is_valid {
let val_str = &inner_buf[range];
if let Ok(v) = u32::from_str_radix(val_str, 16) {
if v > 0xffff {
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) {
buf.push('\\');
} else {
buf.push_str("\\\\");
}
} else {
buf.push_str("\\\\");
}
} else {
buf.push_str("\\\\");
}
} else if is_curly {
buf.push_str("\\\\");
} else {
buf.push('\\');
}
buf.push_str("\\\\");
}
_ => buf.push_str("\\\\"),
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("\"\\uD83D\\uDE42\"");
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("\"\\uD83D\\uDE42\"");
Loading