@@ -13,7 +13,7 @@ use swc_ecma_ast::{
1313 Callee , ClassDecl , ClassMember , DefaultDecl , ExportDefaultDecl , ExportDefaultExpr , Expr , FnDecl ,
1414 Function , Ident , JSXAttr , JSXAttrName , JSXAttrOrSpread , JSXAttrValue , JSXElement ,
1515 JSXElementChild , JSXElementName , JSXExpr , KeyValueProp , Lit , MemberProp , Program , Prop , PropName ,
16- PropOrSpread , Stmt , Str ,
16+ PropOrSpread , Stmt , Str , JSXFragment , ImportDecl , ImportSpecifier ,
1717} ;
1818use swc_ecma_visit:: {
1919 noop_visit_mut_type, noop_visit_type, Visit , VisitMut , VisitMutWith , VisitWith ,
@@ -54,16 +54,18 @@ pub struct JSXVisitor<'a> {
5454 pub tree : & ' a mut Tree < Node > ,
5555 pub module : & ' a Program ,
5656 pub jsx_record : & ' a mut JSXRecord ,
57+ pub taro_components : & ' a [ String ] ,
5758 pub root_node : Option < NodeId > ,
5859 pub current_node : Option < NodeId > ,
5960}
6061
6162impl < ' a > JSXVisitor < ' a > {
62- pub fn new ( tree : & ' a mut Tree < Node > , module : & ' a Program , jsx_record : & ' a mut JSXRecord ) -> Self {
63+ pub fn new ( tree : & ' a mut Tree < Node > , module : & ' a Program , jsx_record : & ' a mut JSXRecord , taro_components : & ' a [ String ] ) -> Self {
6364 JSXVisitor {
6465 tree,
6566 module,
6667 jsx_record,
68+ taro_components,
6769 root_node : None ,
6870 current_node : None ,
6971 }
@@ -154,6 +156,18 @@ impl<'a> Visit for JSXVisitor<'a> {
154156 jsx. visit_children_with ( self )
155157 }
156158
159+ fn visit_jsx_fragment ( & mut self , n : & JSXFragment ) {
160+ if self . root_node . is_none ( ) {
161+ let node = self . create_fragment ( ) ;
162+ let mut root = self . tree . root_mut ( ) ;
163+ self . root_node = Some ( root. id ( ) ) ;
164+ let current = root. append ( node) ;
165+ self . current_node = Some ( current. id ( ) ) ;
166+ self . jsx_record . insert ( SpanKey ( n. span ) , current. id ( ) ) ;
167+ }
168+ n. visit_children_with ( self )
169+ }
170+
157171 fn visit_jsx_element_children ( & mut self , n : & [ JSXElementChild ] ) {
158172 let mut nodes = vec ! [ ] ;
159173 let mut elements: Vec < & JSXElementChild > = vec ! [ ] ;
@@ -162,10 +176,11 @@ impl<'a> Visit for JSXVisitor<'a> {
162176 JSXElementChild :: JSXElement ( element) => {
163177 if let JSXElementName :: Ident ( ident) = & element. opening . name {
164178 let name = ident. sym . to_string ( ) ;
165- if is_starts_with_uppercase ( name. as_str ( ) ) {
179+ if is_starts_with_uppercase ( name. as_str ( ) ) && ! self . taro_components . contains ( & name ) {
166180 let mut visitor = JSXFragmentVisitor :: new (
167181 self . module ,
168182 self . jsx_record ,
183+ self . taro_components ,
169184 name. as_str ( ) ,
170185 SearchType :: Normal ,
171186 ) ;
@@ -231,6 +246,7 @@ impl<'a> Visit for JSXVisitor<'a> {
231246 let mut visitor = JSXFragmentVisitor :: new (
232247 self . module ,
233248 self . jsx_record ,
249+ self . taro_components ,
234250 name. as_str ( ) ,
235251 SearchType :: Normal ,
236252 ) ;
@@ -250,6 +266,7 @@ impl<'a> Visit for JSXVisitor<'a> {
250266 let mut visitor = JSXFragmentVisitor :: new (
251267 self . module ,
252268 self . jsx_record ,
269+ self . taro_components ,
253270 name. as_str ( ) ,
254271 SearchType :: Class ,
255272 ) ;
@@ -280,7 +297,7 @@ impl<'a> Visit for JSXVisitor<'a> {
280297 }
281298 }
282299 for ( index, element) in elements. iter ( ) . enumerate ( ) {
283- let mut visitor = JSXVisitor :: new ( self . tree , self . module , self . jsx_record ) ;
300+ let mut visitor = JSXVisitor :: new ( self . tree , self . module , self . jsx_record , self . taro_components ) ;
284301 visitor. current_node = Some ( nodes[ index] ) ;
285302 visitor. root_node = self . root_node ;
286303 element. visit_with ( & mut visitor) ;
@@ -298,6 +315,7 @@ pub struct JSXFragmentVisitor<'a> {
298315 pub module : & ' a Program ,
299316 pub tree : Tree < Node > ,
300317 pub jsx_record : & ' a mut JSXRecord ,
318+ pub taro_components : & ' a [ String ] ,
301319 pub search_fn : & ' a str ,
302320 pub search_type : SearchType ,
303321}
@@ -306,12 +324,14 @@ impl<'a> JSXFragmentVisitor<'a> {
306324 pub fn new (
307325 module : & ' a Program ,
308326 jsx_record : & ' a mut JSXRecord ,
327+ taro_components : & ' a [ String ] ,
309328 search_fn : & ' a str ,
310329 search_type : SearchType ,
311330 ) -> Self {
312331 JSXFragmentVisitor {
313332 module,
314333 jsx_record,
334+ taro_components,
315335 tree : Tree :: new ( Node :: Fragment ( Fragment :: new ( Some ( create_qualname (
316336 search_fn,
317337 ) ) ) ) ) ,
@@ -333,7 +353,7 @@ impl<'a> Visit for JSXFragmentVisitor<'a> {
333353 for stmt in & body. stmts {
334354 match stmt {
335355 Stmt :: Return ( return_stmt) => {
336- let mut jsx_visitor = JSXVisitor :: new ( & mut self . tree , self . module , self . jsx_record ) ;
356+ let mut jsx_visitor = JSXVisitor :: new ( & mut self . tree , self . module , self . jsx_record , self . taro_components ) ;
337357 return_stmt. visit_with ( & mut jsx_visitor) ;
338358 }
339359 _ => { }
@@ -358,7 +378,7 @@ impl<'a> Visit for JSXFragmentVisitor<'a> {
358378 match stmt {
359379 Stmt :: Return ( return_stmt) => {
360380 let mut jsx_visitor =
361- JSXVisitor :: new ( & mut self . tree , self . module , self . jsx_record ) ;
381+ JSXVisitor :: new ( & mut self . tree , self . module , self . jsx_record , self . taro_components ) ;
362382 return_stmt. visit_with ( & mut jsx_visitor) ;
363383 }
364384 _ => { }
@@ -375,17 +395,59 @@ impl<'a> Visit for JSXFragmentVisitor<'a> {
375395 }
376396}
377397
378- pub struct AstVisitor < ' a > {
398+ pub struct CollectVisitor {
379399 pub export_default_name : Option < String > ,
400+ pub taro_components : Vec < String > ,
401+ }
402+
403+ impl CollectVisitor {
404+ pub fn new ( ) -> Self {
405+ CollectVisitor {
406+ export_default_name : None ,
407+ taro_components : vec ! [ ] ,
408+ }
409+ }
410+ }
411+
412+ impl Visit for CollectVisitor {
413+ fn visit_export_default_expr ( & mut self , n : & ExportDefaultExpr ) {
414+ match & * n. expr {
415+ Expr :: Ident ( ident) => {
416+ if self . export_default_name . is_none ( ) {
417+ self . export_default_name = Some ( ident. sym . to_string ( ) ) ;
418+ }
419+ }
420+ _ => { }
421+ }
422+ }
423+
424+ fn visit_import_decl ( & mut self , n : & ImportDecl ) {
425+ if n. src . value . to_string ( ) . starts_with ( "@tarojs/components" ) {
426+ for specifier in & n. specifiers {
427+ match specifier {
428+ ImportSpecifier :: Named ( named_specifier) => {
429+ self . taro_components . push ( named_specifier. local . sym . to_string ( ) )
430+ }
431+ _ => { }
432+ }
433+ }
434+ }
435+ }
436+ }
437+
438+ pub struct AstVisitor < ' a > {
439+ pub export_default_name : & ' a Option < String > ,
440+ pub taro_components : & ' a [ String ] ,
380441 pub module : & ' a Program ,
381442 pub tree : & ' a mut Tree < Node > ,
382443 pub jsx_record : & ' a mut JSXRecord ,
383444}
384445
385446impl < ' a > AstVisitor < ' a > {
386- pub fn new ( module : & ' a Program , tree : & ' a mut Tree < Node > , jsx_record : & ' a mut JSXRecord ) -> Self {
447+ pub fn new ( module : & ' a Program , tree : & ' a mut Tree < Node > , jsx_record : & ' a mut JSXRecord , export_default_name : & ' a Option < String > , taro_components : & ' a [ String ] ) -> Self {
387448 AstVisitor {
388- export_default_name : None ,
449+ export_default_name,
450+ taro_components,
389451 module,
390452 tree,
391453 jsx_record,
@@ -396,7 +458,7 @@ impl<'a> AstVisitor<'a> {
396458impl < ' a > Visit for AstVisitor < ' a > {
397459 noop_visit_type ! ( ) ;
398460
399- fn visit_fn_decl ( & mut self , n : & swc_ecma_ast :: FnDecl ) {
461+ fn visit_fn_decl ( & mut self , n : & FnDecl ) {
400462 match & self . export_default_name {
401463 Some ( name) => {
402464 if n. ident . sym . to_string ( ) == name. as_str ( ) {
@@ -407,7 +469,7 @@ impl<'a> Visit for AstVisitor<'a> {
407469 for stmt in & body. stmts {
408470 match stmt {
409471 Stmt :: Return ( return_stmt) => {
410- let mut jsx_visitor = JSXVisitor :: new ( self . tree , self . module , self . jsx_record ) ;
472+ let mut jsx_visitor = JSXVisitor :: new ( self . tree , self . module , self . jsx_record , self . taro_components ) ;
411473 return_stmt. visit_with ( & mut jsx_visitor) ;
412474 }
413475 _ => { }
@@ -439,7 +501,7 @@ impl<'a> Visit for AstVisitor<'a> {
439501 match stmt {
440502 Stmt :: Return ( return_stmt) => {
441503 let mut jsx_visitor =
442- JSXVisitor :: new ( self . tree , self . module , self . jsx_record ) ;
504+ JSXVisitor :: new ( self . tree , self . module , self . jsx_record , self . taro_components ) ;
443505 return_stmt. visit_with ( & mut jsx_visitor) ;
444506 }
445507 _ => { }
@@ -461,18 +523,6 @@ impl<'a> Visit for AstVisitor<'a> {
461523 }
462524 }
463525
464- fn visit_export_default_expr ( & mut self , n : & ExportDefaultExpr ) {
465- match & * n. expr {
466- Expr :: Ident ( ident) => {
467- if self . export_default_name . is_none ( ) {
468- self . export_default_name = Some ( ident. sym . to_string ( ) ) ;
469- self . module . visit_with ( self )
470- }
471- }
472- _ => { }
473- }
474- }
475-
476526 fn visit_export_default_decl ( & mut self , n : & ExportDefaultDecl ) {
477527 match & n. decl {
478528 DefaultDecl :: Fn ( n) => match & * n. function {
@@ -482,7 +532,7 @@ impl<'a> Visit for AstVisitor<'a> {
482532 for stmt in & body. stmts {
483533 match stmt {
484534 Stmt :: Return ( return_stmt) => {
485- let mut jsx_visitor = JSXVisitor :: new ( self . tree , self . module , self . jsx_record ) ;
535+ let mut jsx_visitor = JSXVisitor :: new ( self . tree , self . module , self . jsx_record , self . taro_components ) ;
486536 return_stmt. visit_with ( & mut jsx_visitor) ;
487537 }
488538 _ => { }
@@ -505,7 +555,7 @@ impl<'a> Visit for AstVisitor<'a> {
505555 match stmt {
506556 Stmt :: Return ( return_stmt) => {
507557 let mut jsx_visitor =
508- JSXVisitor :: new ( self . tree , self . module , self . jsx_record ) ;
558+ JSXVisitor :: new ( self . tree , self . module , self . jsx_record , self . taro_components ) ;
509559 return_stmt. visit_with ( & mut jsx_visitor) ;
510560 }
511561 _ => { }
0 commit comments