Skip to content

Commit

Permalink
fix(es/codegen): Fix codegen of a property key in ascii-only mode (#8493
Browse files Browse the repository at this point in the history
)

**Related issue:**

 - Closes #8491
  • Loading branch information
kdy1 authored Jan 14, 2024
1 parent 0c28f72 commit 8d9bf4c
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 10 deletions.
4 changes: 2 additions & 2 deletions crates/swc/tests/fixture/issues-7xxx/7805/output/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ var SUPPORTED_LOCALE = {
I: "i\u0307",
J: "j\u0307",
\u012E: "\u012F\u0307",
\u00CC: "i\u0307\u0300",
\u00CD: "i\u0307\u0301",
"\xcc": "i\u0307\u0300",
"\xcd": "i\u0307\u0301",
\u0128: "i\u0307\u0303"
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-8xxx/8260/output/1.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
function p(\u00B5, \u03C3) {}
function p(\xb5, \u03C3) {}
console.log(p);
21 changes: 21 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8491/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript",
"jsx": false
},
"target": "es5",
"loose": false,
"minify": {
"compress": true,
"format": {
"asciiOnly": true
}
}
},
"module": {
"type": "es6"
},
"minify": false,
"isModule": true
}
3 changes: 3 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8491/input/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const a = { aób: 'ó' }

console.log(a)
3 changes: 3 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8491/output/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
console.log({
"a\xf3b": "\xf3"
});
55 changes: 48 additions & 7 deletions crates/swc_ecma_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1721,7 +1721,33 @@ where
fn emit_prop_name(&mut self, node: &PropName) -> Result {
match node {
PropName::Ident(ident) => {
emit!(ident)
// TODO: Use write_symbol when ident is a symbol.
self.emit_leading_comments_of_span(ident.span, false)?;

// Source map
self.wr.commit_pending_semi()?;

srcmap!(ident, true);

if self.cfg.ascii_only {
if self.wr.can_ignore_invalid_unicodes() {
self.wr.write_symbol(
DUMMY_SP,
&get_ascii_only_ident(&ident.sym, true, self.cfg.target),
)?;
} else {
self.wr.write_symbol(
DUMMY_SP,
&get_ascii_only_ident(
&handle_invalid_unicodes(&ident.sym),
true,
self.cfg.target,
),
)?;
}
} else {
emit!(ident);
}
}
PropName::Str(ref n) => emit!(n),
PropName::Num(ref n) => emit!(n),
Expand Down Expand Up @@ -2259,12 +2285,18 @@ where

if self.cfg.ascii_only {
if self.wr.can_ignore_invalid_unicodes() {
self.wr
.write_symbol(DUMMY_SP, &get_ascii_only_ident(&ident.sym, self.cfg.target))?;
self.wr.write_symbol(
DUMMY_SP,
&get_ascii_only_ident(&ident.sym, false, self.cfg.target),
)?;
} else {
self.wr.write_symbol(
DUMMY_SP,
&get_ascii_only_ident(&handle_invalid_unicodes(&ident.sym), self.cfg.target),
&get_ascii_only_ident(
&handle_invalid_unicodes(&ident.sym),
false,
self.cfg.target,
),
)?;
}
} else if self.wr.can_ignore_invalid_unicodes() {
Expand Down Expand Up @@ -3751,14 +3783,15 @@ fn get_template_element_from_raw(s: &str, ascii_only: bool) -> String {
buf
}

fn get_ascii_only_ident(sym: &str, target: EsVersion) -> Cow<str> {
fn get_ascii_only_ident(sym: &str, may_need_quote: bool, target: EsVersion) -> Cow<str> {
if sym.chars().all(|c| c.is_ascii()) {
return Cow::Borrowed(sym);
}

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

while let Some(c) = iter.next() {
match c {
Expand Down Expand Up @@ -3859,7 +3892,11 @@ fn get_ascii_only_ident(sym: &str, target: EsVersion) -> Cow<str> {
'\x20'..='\x7e' => {
buf.push(c);
}
'\u{7f}'..='\u{ff}' if !first => {
'\u{7f}'..='\u{ff}' => {
if may_need_quote {
need_quote = true;
}

let _ = write!(buf, "\\x{:x}", c as u8);
}
'\u{2028}' => {
Expand Down Expand Up @@ -3897,7 +3934,11 @@ fn get_ascii_only_ident(sym: &str, target: EsVersion) -> Cow<str> {
first = false;
}

Cow::Owned(buf)
if need_quote {
Cow::Owned(format!("\"{}\"", buf))
} else {
Cow::Owned(buf)
}
}

fn get_quoted_utf16(v: &str, ascii_only: bool, target: EsVersion) -> String {
Expand Down

0 comments on commit 8d9bf4c

Please sign in to comment.