Skip to content
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
7 changes: 7 additions & 0 deletions .changeset/empty-rules-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
swc_core: patch
swc_ecma_codegen: patch
swc_ecma_minifier: patch
---

fix(es/codegen): Fix escape of unicode characters
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default{a:"֑-ۯۺ-ࣿ‏\ud802-\ud803\ud83a-\ud83bיִ-﷿ﹰ-ﻼ",b:"A-Za-z\xc0-\xd6\xd8-\xf6\xf8-ʸ̀-֐ऀ-῿‎Ⰰ-\ud801\ud804-\ud839\ud83c-\udbff豈-﬜︀-﹯﻽-￿"};
export default{a:"֑-ۯۺ-ࣿ‏\\ud802-\\ud803\\ud83a-\\ud83bיִ-﷿ﹰ-ﻼ",b:"A-Za-z\xc0-\xd6\xd8-\xf6\xf8-ʸ̀-֐ऀ-῿‎Ⰰ-\\ud801\\ud804-\\ud839\\ud83c-\\udbff豈-﬜︀-﹯﻽-￿"};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"mappings": "AAUA,cAAe,CAAEA,EATb,0CASgBC,EALhB,kFAKkB,CAAE",
"mappings": "AAUA,cAAe,CAAEA,EATb,8CASgBC,EALhB,uFAKkB,CAAE",
"names": [
"a",
"b"
Expand Down
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-7xxx/7678/output/1.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
let str="\uD83D\uDC68\\u200D\uD83D\uDE80";let obj={"\uD83D\uDC68\\u200D\uD83D\uDE80":"wrong"};
let str="\\uD83D\\uDC68\\u200D\\uD83D\\uDE80";let obj={"\\uD83D\\uDC68\\u200D\\uD83D\\uDE80":"wrong"};
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
// 2. Let cu1 be floor((cp – 65536) / 1024) + 0xD800.
// Although we should just get back a single code point value of 0xD800,
// this is a useful edge-case test.
var x = "\u{D800}";
var x = "\\u{D800}";
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
// 2. Let cu2 be ((cp – 65536) modulo 1024) + 0xDC00.
// Although we should just get back a single code point value of 0xDC00,
// this is a useful edge-case test.
var x = "\u{DC00}";
var x = "\\u{DC00}";
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
// 2. Let cu1 be floor((cp – 65536) / 1024) + 0xD800.
// Although we should just get back a single code point value of 0xD800,
// this is a useful edge-case test.
var x = "\u{D800}";
var x = "\\u{D800}";
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
// 2. Let cu2 be ((cp – 65536) modulo 1024) + 0xDC00.
// Although we should just get back a single code point value of 0xDC00,
// this is a useful edge-case test.
var x = "\u{DC00}";
var x = "\\u{DC00}";
84 changes: 1 addition & 83 deletions crates/swc_ecma_codegen/src/lit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,89 +397,7 @@ pub fn get_quoted_utf16(v: &str, ascii_only: bool, target: EsVersion) -> (AsciiC
'\r' => buf.push_str("\\r"),
'\u{000b}' => buf.push_str("\\v"),
'\t' => buf.push('\t'),
'\\' => {
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("\\\\"),
c if c == escape_char => {
buf.push('\\');
buf.push(c);
Expand Down
30 changes: 30 additions & 0 deletions crates/swc_ecma_codegen/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,36 @@ fn issue_9630() {
);
}

#[test]
fn issue_10353_1() {
test_from_to_custom_config(
r#"console.log("\\uD83D");"#,
r#"console.log("\\uD83D")"#,
Config {
ascii_only: false,
target: EsVersion::Es2020,
minify: true,
..Default::default()
},
Syntax::default(),
);
}

#[test]
fn issue_10353_2() {
test_from_to_custom_config(
r#"console.log("\\uD83D\\uDE42");"#,
r#"console.log("\\uD83D\\uDE42")"#,
Config {
ascii_only: false,
target: EsVersion::Es2020,
minify: true,
..Default::default()
},
Syntax::default(),
);
}

#[testing::fixture("tests/str-lits/**/*.txt")]
fn test_str_lit(input: PathBuf) {
test_str_lit_inner(input)
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_codegen/tests/fixture/string/output.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading