Skip to content
Merged
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/rotten-frogs-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_core: patch
hstr: patch
---

fix(atom): skip only unicode \u
66 changes: 64 additions & 2 deletions crates/hstr/src/wtf8_atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,43 @@ impl serde::ser::Serialize for Wtf8Atom {
// By escaping literal '\u' to '\\u', we ensure:
// - Unpaired surrogates serialize as '\uXXXX'
// - Literal '\u' text serializes as '\\uXXXX'
//
// However, we should only escape '\u' if it's followed by exactly 4 hex digits,
// which would indicate a Unicode escape sequence. Otherwise, '\u' followed by
// non-hex characters (like '\util') should not be escaped.
if c == '\\' && iter.peek().map(|cp| cp.to_u32()) == Some('u' as u32) {
iter.next(); // skip 'u'
result.push_str("\\\\u");
// Look ahead to see if this is followed by exactly 4 hex digits
let mut lookahead = iter.clone();
lookahead.next(); // skip 'u'

let mut hex_count = 0;
let mut all_hex = true;
for _ in 0..4 {
if let Some(next_cp) = lookahead.next() {
if let Some(next_c) = next_cp.to_char() {
if next_c.is_ascii_hexdigit() {
hex_count += 1;
} else {
all_hex = false;
break;
}
} else {
all_hex = false;
break;
}
} else {
all_hex = false;
break;
}
}

// Only escape if we have exactly 4 hex digits after '\u'
if hex_count == 4 && all_hex {
iter.next(); // skip 'u'
result.push_str("\\\\u");
} else {
result.push(c);
}
} else {
result.push(c)
}
Expand Down Expand Up @@ -553,4 +587,32 @@ mod tests {
let err_atom = result.unwrap_err();
assert_eq!(err_atom.to_string_lossy(), "\u{FFFD}");
}

#[test]
fn test_backslash_util_issue_11214() {
let atom =
Wtf8Atom::from("C:\\github\\swc-plugin-coverage-instrument\\spec\\util\\verifier.ts");
let serialized = serde_json::to_string(&atom).unwrap();

assert!(
!serialized.contains("spec\\\\\\\\util"),
"Found quadruple backslashes in spec segment! Serialized: {serialized}"
);

assert!(
serialized.contains("spec\\\\util"),
"Expected double backslashes in spec segment not found! Serialized: {serialized}",
);

// The expected serialized value should have consistent escaping
let expected = r#""C:\\github\\swc-plugin-coverage-instrument\\spec\\util\\verifier.ts""#;
assert_eq!(
serialized, expected,
"Serialized value should have consistent backslash escaping"
);

// Test round-trip
let deserialized: Wtf8Atom = serde_json::from_str(&serialized).unwrap();
assert_eq!(atom, deserialized);
}
}
3 changes: 3 additions & 0 deletions crates/swc_ecma_parser/tests/js/issue-11214/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var coverageData = {
path: "C:\\github\\swc-plugin-coverage-instrument\\spec\\util\\verifier.ts",
}
70 changes: 70 additions & 0 deletions crates/swc_ecma_parser/tests/js/issue-11214/input.js.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"type": "Script",
"span": {
"start": 1,
"end": 107
},
"body": [
{
"type": "VariableDeclaration",
"span": {
"start": 1,
"end": 107
},
"ctxt": 0,
"kind": "var",
"declare": false,
"declarations": [
{
"type": "VariableDeclarator",
"span": {
"start": 5,
"end": 107
},
"id": {
"type": "Identifier",
"span": {
"start": 5,
"end": 17
},
"ctxt": 0,
"value": "coverageData",
"optional": false,
"typeAnnotation": null
},
"init": {
"type": "ObjectExpression",
"span": {
"start": 20,
"end": 107
},
"properties": [
{
"type": "KeyValueProperty",
"key": {
"type": "Identifier",
"span": {
"start": 26,
"end": 30
},
"value": "path"
},
"value": {
"type": "StringLiteral",
"span": {
"start": 32,
"end": 101
},
"value": "C:\\github\\swc-plugin-coverage-instrument\\spec\\util\\verifier.ts",
"raw": "\"C:\\\\github\\\\swc-plugin-coverage-instrument\\\\spec\\\\util\\\\verifier.ts\""
}
}
]
},
"definite": false
}
]
}
],
"interpreter": null
}
Loading