Skip to content

Commit 8bad4cf

Browse files
committed
Fix extra
1 parent 5ea5f77 commit 8bad4cf

File tree

16 files changed

+126
-96
lines changed

16 files changed

+126
-96
lines changed

.changeset/empty-rules-double.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
swc_core: patch
3+
swc_ecma_codegen: patch
4+
swc_ecma_minifier: patch
5+
---
6+
7+
fix(es/codegen): Fix escape of unicode characters
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export default{a:"֑-ۯۺ-ࣿ‏\ud802-\ud803\ud83a-\ud83bיִ-﷿ﹰ-ﻼ",b:"A-Za-z\xc0-\xd6\xd8-\xf6\xf8-ʸ̀-֐ऀ-῿‎Ⰰ-\ud801\ud804-\ud839\ud83c-\udbff豈-﬜︀-﹯﻽-"};
1+
export default{a:"֑-ۯۺ-ࣿ‏\\ud802-\\ud803\\ud83a-\\ud83bיִ-﷿ﹰ-ﻼ",b:"A-Za-z\xc0-\xd6\xd8-\xf6\xf8-ʸ̀-֐ऀ-῿‎Ⰰ-\\ud801\\ud804-\\ud839\\ud83c-\\udbff豈-﬜︀-﹯﻽-"};

crates/swc/tests/fixture/issues-4xxx/4120/1/output/index.map

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"mappings": "AAUA,cAAe,CAAEA,EATb,0CASgBC,EALhB,kFAKkB,CAAE",
2+
"mappings": "AAUA,cAAe,CAAEA,EATb,8CASgBC,EALhB,uFAKkB,CAAE",
33
"names": [
44
"a",
55
"b"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
let str="\uD83D\uDC68\\u200D\uD83D\uDE80";let obj={"\uD83D\uDC68\\u200D\uD83D\uDE80":"wrong"};
1+
let str="\\uD83D\\uDC68\\u200D\\uD83D\\uDE80";let obj={"\\uD83D\\uDC68\\u200D\\uD83D\\uDE80":"wrong"};

crates/swc/tests/tsc-references/unicodeExtendedEscapesInStrings10_ES5.1.normal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
// 2. Let cu1 be floor((cp – 65536) / 1024) + 0xD800.
44
// Although we should just get back a single code point value of 0xD800,
55
// this is a useful edge-case test.
6-
var x = "\u{D800}";
6+
var x = "\\u{D800}";

crates/swc/tests/tsc-references/unicodeExtendedEscapesInStrings11_ES5.1.normal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
// 2. Let cu2 be ((cp – 65536) modulo 1024) + 0xDC00.
44
// Although we should just get back a single code point value of 0xDC00,
55
// this is a useful edge-case test.
6-
var x = "\u{DC00}";
6+
var x = "\\u{DC00}";

crates/swc/tests/tsc-references/unicodeExtendedEscapesInTemplates10_ES5.1.normal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
// 2. Let cu1 be floor((cp – 65536) / 1024) + 0xD800.
44
// Although we should just get back a single code point value of 0xD800,
55
// this is a useful edge-case test.
6-
var x = "\u{D800}";
6+
var x = "\\u{D800}";

crates/swc/tests/tsc-references/unicodeExtendedEscapesInTemplates11_ES5.1.normal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
// 2. Let cu2 be ((cp – 65536) modulo 1024) + 0xDC00.
44
// Although we should just get back a single code point value of 0xDC00,
55
// this is a useful edge-case test.
6-
var x = "\u{DC00}";
6+
var x = "\\u{DC00}";

crates/swc_ecma_codegen/src/lit.rs

Lines changed: 1 addition & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -397,89 +397,7 @@ pub fn get_quoted_utf16(v: &str, ascii_only: bool, target: EsVersion) -> (AsciiC
397397
'\r' => buf.push_str("\\r"),
398398
'\u{000b}' => buf.push_str("\\v"),
399399
'\t' => buf.push('\t'),
400-
'\\' => {
401-
let next = iter.peek();
402-
match next {
403-
Some('u') => {
404-
let mut inner_iter = iter.clone();
405-
inner_iter.next();
406-
407-
let mut is_curly = false;
408-
let mut next = inner_iter.peek();
409-
410-
if next == Some(&'{') {
411-
is_curly = true;
412-
inner_iter.next();
413-
next = inner_iter.peek();
414-
} else if next != Some(&'D') && next != Some(&'d') {
415-
buf.push('\\');
416-
}
417-
418-
if let Some(c @ 'D' | c @ 'd') = next {
419-
let mut inner_buf = String::with_capacity(8);
420-
inner_buf.push('\\');
421-
inner_buf.push('u');
422-
423-
if is_curly {
424-
inner_buf.push('{');
425-
}
426-
427-
inner_buf.push(*c);
428-
inner_iter.next();
429-
430-
let mut is_valid = true;
431-
for _ in 0..3 {
432-
match inner_iter.next() {
433-
Some(c @ '0'..='9') | Some(c @ 'a'..='f')
434-
| Some(c @ 'A'..='F') => {
435-
inner_buf.push(c);
436-
}
437-
_ => {
438-
is_valid = false;
439-
break;
440-
}
441-
}
442-
}
443-
444-
if is_curly {
445-
inner_buf.push('}');
446-
}
447-
448-
let range = if is_curly {
449-
3..(inner_buf.len() - 1)
450-
} else {
451-
2..6
452-
};
453-
454-
if is_valid {
455-
let val_str = &inner_buf[range];
456-
if let Ok(v) = u32::from_str_radix(val_str, 16) {
457-
if v > 0xffff {
458-
buf.push_str(&inner_buf);
459-
let end = if is_curly { 7 } else { 5 };
460-
for _ in 0..end {
461-
iter.next();
462-
}
463-
} else if (0xd800..=0xdfff).contains(&v) {
464-
buf.push('\\');
465-
} else {
466-
buf.push_str("\\\\");
467-
}
468-
} else {
469-
buf.push_str("\\\\");
470-
}
471-
} else {
472-
buf.push_str("\\\\");
473-
}
474-
} else if is_curly {
475-
buf.push_str("\\\\");
476-
} else {
477-
buf.push('\\');
478-
}
479-
}
480-
_ => buf.push_str("\\\\"),
481-
}
482-
}
400+
'\\' => buf.push_str("\\\\"),
483401
c if c == escape_char => {
484402
buf.push('\\');
485403
buf.push(c);

crates/swc_ecma_codegen/src/tests.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,36 @@ fn issue_9630() {
988988
);
989989
}
990990

991+
#[test]
992+
fn issue_10353_1() {
993+
test_from_to_custom_config(
994+
r#"console.log("\\uD83D");"#,
995+
r#"console.log("\\uD83D")"#,
996+
Config {
997+
ascii_only: false,
998+
target: EsVersion::Es2020,
999+
minify: true,
1000+
..Default::default()
1001+
},
1002+
Syntax::default(),
1003+
);
1004+
}
1005+
1006+
#[test]
1007+
fn issue_10353_2() {
1008+
test_from_to_custom_config(
1009+
r#"console.log("\\uD83D\\uDE42");"#,
1010+
r#"console.log("\\uD83D\\uDE42")"#,
1011+
Config {
1012+
ascii_only: false,
1013+
target: EsVersion::Es2020,
1014+
minify: true,
1015+
..Default::default()
1016+
},
1017+
Syntax::default(),
1018+
);
1019+
}
1020+
9911021
#[testing::fixture("tests/str-lits/**/*.txt")]
9921022
fn test_str_lit(input: PathBuf) {
9931023
test_str_lit_inner(input)

0 commit comments

Comments
 (0)