Skip to content

Commit

Permalink
fix(es/codegen): Fix get_ascii_only_ident (#8287)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #8260
  • Loading branch information
kdy1 authored Nov 16, 2023
1 parent 9e8fe38 commit 07c8935
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 27 deletions.
4 changes: 2 additions & 2 deletions crates/swc/tests/fixture/issues-7xxx/7240/output/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ Object.defineProperty(exports, "__esModule", {
}
});
var _obj, _default = ((_obj = {
"\u3131": "\u11B0",
"\u3141": "\u11B1"
\u3131: "\u11B0",
\u3141: "\u11B1"
})["\u3142"] = "\u11B2", _obj);
16 changes: 8 additions & 8 deletions crates/swc/tests/fixture/issues-7xxx/7805/output/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@ var SUPPORTED_LOCALE = {
tr: {
regexp: /\u0130|\u0049|\u0049\u0307/g,
map: {
"\u0130": "i",
\u0130: "i",
I: "\u0131",
"I\u0307": "i"
I\u0307: "i"
}
},
az: {
regexp: /\u0130/g,
map: {
"\u0130": "i",
\u0130: "i",
I: "\u0131",
"I\u0307": "i"
I\u0307: "i"
}
},
lt: {
regexp: /\u0049|\u004A|\u012E|\u00CC|\u00CD|\u0128/g,
map: {
I: "i\u0307",
J: "j\u0307",
"\u012E": "\u012F\u0307",
"\xcc": "i\u0307\u0300",
"\xcd": "i\u0307\u0301",
"\u0128": "i\u0307\u0303"
\u012E: "\u012F\u0307",
\u00CC: "i\u0307\u0300",
\u00CD: "i\u0307\u0301",
\u0128: "i\u0307\u0303"
}
}
};
Expand Down
22 changes: 22 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8260/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript",
"jsx": false
},
"target": "es5",
"loose": false,
"minify": {
"compress": false,
"mangle": false,
"format": {
"ascii_only": true
}
},
},
"module": {
"type": "es6"
},
"minify": false,
"isModule": true
}
2 changes: 2 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8260/input/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
function p(µ, σ) { }
console.log(p);
2 changes: 2 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8260/output/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
function p(\u00B5, \u03C3) {}
console.log(p);
22 changes: 5 additions & 17 deletions crates/swc_ecma_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1657,20 +1657,6 @@ where
fn emit_prop_name(&mut self, node: &PropName) -> Result {
match node {
PropName::Ident(ident) => {
if self.cfg.ascii_only && !ident.sym.is_ascii() {
punct!("\"");
self.wr.write_symbol(
DUMMY_SP,
&get_ascii_only_ident(
&handle_invalid_unicodes(&ident.sym),
self.cfg.target,
),
)?;
punct!("\"");

return Ok(());
}

emit!(ident)
}
PropName::Str(ref n) => emit!(n),
Expand Down Expand Up @@ -3705,6 +3691,7 @@ fn get_ascii_only_ident(sym: &str, target: EsVersion) -> Cow<str> {
return Cow::Borrowed(sym);
}

let mut first = true;
let mut buf = String::with_capacity(sym.len() + 8);
let mut iter = sym.chars().peekable();

Expand Down Expand Up @@ -3798,16 +3785,16 @@ fn get_ascii_only_ident(sym: &str, target: EsVersion) -> Cow<str> {
'"' => {
buf.push('"');
}
'\x01'..='\x0f' => {
'\x01'..='\x0f' if !first => {
let _ = write!(buf, "\\x0{:x}", c as u8);
}
'\x10'..='\x1f' => {
'\x10'..='\x1f' if !first => {
let _ = write!(buf, "\\x{:x}", c as u8);
}
'\x20'..='\x7e' => {
buf.push(c);
}
'\u{7f}'..='\u{ff}' => {
'\u{7f}'..='\u{ff}' if !first => {
let _ = write!(buf, "\\x{:x}", c as u8);
}
'\u{2028}' => {
Expand Down Expand Up @@ -3842,6 +3829,7 @@ fn get_ascii_only_ident(sym: &str, target: EsVersion) -> Cow<str> {
}
}
}
first = false;
}

Cow::Owned(buf)
Expand Down

0 comments on commit 07c8935

Please sign in to comment.