Skip to content
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

fix(es/codegen): Fix get_ascii_only_ident #8287

Merged
merged 4 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -1675,20 +1675,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 @@ -3723,6 +3709,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 @@ -3816,16 +3803,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 @@ -3860,6 +3847,7 @@ fn get_ascii_only_ident(sym: &str, target: EsVersion) -> Cow<str> {
}
}
}
first = false;
}

Cow::Owned(buf)
Expand Down