@@ -1721,7 +1721,33 @@ where
1721
1721
fn emit_prop_name ( & mut self , node : & PropName ) -> Result {
1722
1722
match node {
1723
1723
PropName :: Ident ( ident) => {
1724
- emit ! ( ident)
1724
+ // TODO: Use write_symbol when ident is a symbol.
1725
+ self . emit_leading_comments_of_span ( ident. span , false ) ?;
1726
+
1727
+ // Source map
1728
+ self . wr . commit_pending_semi ( ) ?;
1729
+
1730
+ srcmap ! ( ident, true ) ;
1731
+
1732
+ if self . cfg . ascii_only {
1733
+ if self . wr . can_ignore_invalid_unicodes ( ) {
1734
+ self . wr . write_symbol (
1735
+ DUMMY_SP ,
1736
+ & get_ascii_only_ident ( & ident. sym , true , self . cfg . target ) ,
1737
+ ) ?;
1738
+ } else {
1739
+ self . wr . write_symbol (
1740
+ DUMMY_SP ,
1741
+ & get_ascii_only_ident (
1742
+ & handle_invalid_unicodes ( & ident. sym ) ,
1743
+ true ,
1744
+ self . cfg . target ,
1745
+ ) ,
1746
+ ) ?;
1747
+ }
1748
+ } else {
1749
+ emit ! ( ident) ;
1750
+ }
1725
1751
}
1726
1752
PropName :: Str ( ref n) => emit ! ( n) ,
1727
1753
PropName :: Num ( ref n) => emit ! ( n) ,
@@ -2259,12 +2285,18 @@ where
2259
2285
2260
2286
if self . cfg . ascii_only {
2261
2287
if self . wr . can_ignore_invalid_unicodes ( ) {
2262
- self . wr
2263
- . write_symbol ( DUMMY_SP , & get_ascii_only_ident ( & ident. sym , self . cfg . target ) ) ?;
2288
+ self . wr . write_symbol (
2289
+ DUMMY_SP ,
2290
+ & get_ascii_only_ident ( & ident. sym , false , self . cfg . target ) ,
2291
+ ) ?;
2264
2292
} else {
2265
2293
self . wr . write_symbol (
2266
2294
DUMMY_SP ,
2267
- & get_ascii_only_ident ( & handle_invalid_unicodes ( & ident. sym ) , self . cfg . target ) ,
2295
+ & get_ascii_only_ident (
2296
+ & handle_invalid_unicodes ( & ident. sym ) ,
2297
+ false ,
2298
+ self . cfg . target ,
2299
+ ) ,
2268
2300
) ?;
2269
2301
}
2270
2302
} else if self . wr . can_ignore_invalid_unicodes ( ) {
@@ -3751,14 +3783,15 @@ fn get_template_element_from_raw(s: &str, ascii_only: bool) -> String {
3751
3783
buf
3752
3784
}
3753
3785
3754
- fn get_ascii_only_ident ( sym : & str , target : EsVersion ) -> Cow < str > {
3786
+ fn get_ascii_only_ident ( sym : & str , may_need_quote : bool , target : EsVersion ) -> Cow < str > {
3755
3787
if sym. chars ( ) . all ( |c| c. is_ascii ( ) ) {
3756
3788
return Cow :: Borrowed ( sym) ;
3757
3789
}
3758
3790
3759
3791
let mut first = true ;
3760
3792
let mut buf = String :: with_capacity ( sym. len ( ) + 8 ) ;
3761
3793
let mut iter = sym. chars ( ) . peekable ( ) ;
3794
+ let mut need_quote = false ;
3762
3795
3763
3796
while let Some ( c) = iter. next ( ) {
3764
3797
match c {
@@ -3859,7 +3892,11 @@ fn get_ascii_only_ident(sym: &str, target: EsVersion) -> Cow<str> {
3859
3892
'\x20' ..='\x7e' => {
3860
3893
buf. push ( c) ;
3861
3894
}
3862
- '\u{7f}' ..='\u{ff}' if !first => {
3895
+ '\u{7f}' ..='\u{ff}' => {
3896
+ if may_need_quote {
3897
+ need_quote = true ;
3898
+ }
3899
+
3863
3900
let _ = write ! ( buf, "\\ x{:x}" , c as u8 ) ;
3864
3901
}
3865
3902
'\u{2028}' => {
@@ -3897,7 +3934,11 @@ fn get_ascii_only_ident(sym: &str, target: EsVersion) -> Cow<str> {
3897
3934
first = false ;
3898
3935
}
3899
3936
3900
- Cow :: Owned ( buf)
3937
+ if need_quote {
3938
+ Cow :: Owned ( format ! ( "\" {}\" " , buf) )
3939
+ } else {
3940
+ Cow :: Owned ( buf)
3941
+ }
3901
3942
}
3902
3943
3903
3944
fn get_quoted_utf16 ( v : & str , ascii_only : bool , target : EsVersion ) -> String {
0 commit comments