1- use std:: collections:: HashMap ;
1+ use std:: { borrow :: Cow , collections:: HashMap } ;
22
33use lazy_static:: lazy_static;
44use regex:: { Captures , Regex } ;
@@ -15,19 +15,17 @@ mod string_to_object_style;
1515mod util;
1616
1717use self :: decode_xml:: * ;
18- use self :: mappings:: * ;
18+ use self :: mappings:: ATTR_MAPPINGS ;
1919use self :: string_to_object_style:: * ;
2020use self :: util:: * ;
2121
22- fn kebab_case ( str : & str ) -> String {
22+ fn kebab_case ( str : & str ) -> Cow < str > {
2323 lazy_static ! {
2424 static ref KEBAB_REGEX : Regex = Regex :: new( r"[A-Z\u00C0-\u00D6\u00D8-\u00DE]" ) . unwrap( ) ;
2525 }
26- KEBAB_REGEX
27- . replace_all ( str, |caps : & Captures | {
28- format ! ( "-{}" , & caps[ 0 ] . to_lowercase( ) )
29- } )
30- . to_string ( )
26+ KEBAB_REGEX . replace_all ( str, |caps : & Captures | {
27+ format ! ( "-{}" , & caps[ 0 ] . to_lowercase( ) )
28+ } )
3129}
3230
3331fn convert_aria_attribute ( kebab_key : & str ) -> String {
@@ -37,11 +35,11 @@ fn convert_aria_attribute(kebab_key: &str) -> String {
3735 format ! ( "{}-{}" , aria, lowercase_parts)
3836}
3937
40- fn replace_spaces ( s : & str ) -> String {
38+ fn replace_spaces ( s : & str ) -> Cow < str > {
4139 lazy_static ! {
4240 static ref SPACES_REGEX : Regex = Regex :: new( r"[\t\r\n\u0085\u2028\u2029]+" ) . unwrap( ) ;
4341 }
44- SPACES_REGEX . replace_all ( s, |_: & Captures | " " ) . to_string ( )
42+ SPACES_REGEX . replace_all ( s, |_: & Captures | " " )
4543}
4644
4745fn get_value ( attr_name : & str , value : & JsWord ) -> JSXAttrValue {
@@ -77,44 +75,44 @@ fn text(n: &swc_xml::ast::Text) -> Option<JSXElementChild> {
7775 static ref SPACE_REGEX : Regex = Regex :: new( r"^\s+$" ) . unwrap( ) ;
7876 }
7977
80- let value = n. data . to_string ( ) ;
81- if SPACE_REGEX . is_match ( & value) {
78+ let value = n. data . as_str ( ) ;
79+ if SPACE_REGEX . is_match ( value) {
8280 return None ;
8381 }
8482
8583 Some ( JSXElementChild :: JSXExprContainer ( JSXExprContainer {
8684 span : DUMMY_SP ,
8785 expr : JSXExpr :: Expr ( Box :: new ( Expr :: Lit ( Lit :: Str ( Str {
8886 span : DUMMY_SP ,
89- value : decode_xml ( & value) . into ( ) ,
87+ value : decode_xml ( value) . into ( ) ,
9088 raw : None ,
9189 } ) ) ) ) ,
9290 } ) )
9391}
9492
9593pub struct HastVisitor {
9694 jsx : Option < JSXElement > ,
97- attr_mappings : HashMap < & ' static str , & ' static str > ,
95+ attr_mappings : & ' static HashMap < & ' static str , & ' static str > ,
9896}
9997
10098impl HastVisitor {
10199 fn new ( ) -> Self {
102100 Self {
103101 jsx : None ,
104- attr_mappings : create_attr_mappings ( ) ,
102+ attr_mappings : & ATTR_MAPPINGS ,
105103 }
106104 }
107105
108- pub fn get_jsx ( & self ) -> Option < JSXElement > {
109- self . jsx . clone ( )
106+ pub fn take_jsx ( & mut self ) -> Option < JSXElement > {
107+ self . jsx . take ( )
110108 }
111109
112110 fn element ( & self , n : & swc_xml:: ast:: Element ) -> JSXElement {
113111 let attrs = n
114112 . attributes
115113 . iter ( )
116114 . map ( |attr| {
117- let value = attr. value . clone ( ) . map ( |v| get_value ( & attr. name , & v) ) ;
115+ let value = attr. value . as_ref ( ) . map ( |v| get_value ( & attr. name , v) ) ;
118116 JSXAttrOrSpread :: JSXAttr ( JSXAttr {
119117 span : DUMMY_SP ,
120118 name : JSXAttrName :: Ident ( self . get_key ( & attr. name , & n. tag_name ) . into ( ) ) ,
@@ -130,23 +128,23 @@ impl HastVisitor {
130128 ) ) ;
131129 let children = self . all ( & n. children ) ;
132130
133- let opening = JSXOpeningElement {
134- span : DUMMY_SP ,
135- name : name. clone ( ) ,
136- attrs,
137- self_closing : children. is_empty ( ) ,
138- type_args : None ,
139- } ;
140-
141131 let closing = if !children. is_empty ( ) {
142132 Some ( JSXClosingElement {
143133 span : DUMMY_SP ,
144- name,
134+ name : name . clone ( ) ,
145135 } )
146136 } else {
147137 None
148138 } ;
149139
140+ let opening = JSXOpeningElement {
141+ span : DUMMY_SP ,
142+ name,
143+ attrs,
144+ self_closing : children. is_empty ( ) ,
145+ type_args : None ,
146+ } ;
147+
150148 JSXElement {
151149 span : DUMMY_SP ,
152150 opening,
@@ -243,7 +241,7 @@ impl Visit for HastVisitor {
243241pub fn to_swc_ast ( hast : swc_xml:: ast:: Document ) -> Option < JSXElement > {
244242 let mut v = HastVisitor :: new ( ) ;
245243 hast. visit_with ( & mut v) ;
246- v. get_jsx ( )
244+ v. take_jsx ( )
247245}
248246
249247#[ cfg( test) ]
@@ -285,7 +283,7 @@ mod tests {
285283 } ) ) )
286284 . unwrap ( ) ;
287285
288- String :: from_utf8_lossy ( & buf) . to_string ( )
286+ unsafe { String :: from_utf8_unchecked ( buf) }
289287 }
290288
291289 fn document_test ( input : PathBuf ) {
0 commit comments