@@ -4,7 +4,7 @@ use swc_ecma_ast::{ImportSpecifier, Module, ModuleDecl};
44use swc_ecma_parser:: { Parser , StringInput , Syntax , TsSyntax } ;
55use swc_ecma_visit:: { Visit , VisitWith } ;
66
7- pub fn module_parser ( tsx_code : & str ) -> ( Module , Lrc < SourceMap > ) {
7+ pub fn module_parser ( tsx_code : & str ) -> anyhow :: Result < ( Module , Lrc < SourceMap > ) > {
88 let cm: Lrc < SourceMap > = Default :: default ( ) ;
99 let fm = cm. new_source_file ( FileName :: Custom ( "input.tsx" . into ( ) ) . into ( ) , tsx_code. into ( ) ) ;
1010
@@ -18,7 +18,10 @@ pub fn module_parser(tsx_code: &str) -> (Module, Lrc<SourceMap>) {
1818
1919 let mut parser = Parser :: new ( syntax, StringInput :: from ( & * fm) , None ) ;
2020
21- ( parser. parse_module ( ) . expect ( "Failed to parse" ) , cm)
21+ let parse_res = parser
22+ . parse_module ( )
23+ . map_err ( |e| anyhow:: Error :: msg ( e. kind ( ) . msg ( ) ) ) ?;
24+ Ok ( ( parse_res, cm) )
2225}
2326
2427#[ derive( Eq , PartialEq , Hash , Debug ) ]
@@ -60,8 +63,8 @@ pub fn extract_used_classes(
6063 tsx_code : & str ,
6164 variable_name : & str ,
6265 file_name : String ,
63- ) -> HashSet < UsedClassName > {
64- let ( module, source_map) = module_parser ( tsx_code) ;
66+ ) -> anyhow :: Result < HashSet < UsedClassName > > {
67+ let ( module, source_map) = module_parser ( tsx_code) ? ;
6568
6669 let mut finder = PropertyFinder {
6770 variable_name : variable_name. to_string ( ) ,
@@ -72,7 +75,7 @@ pub fn extract_used_classes(
7275
7376 module. visit_with ( & mut finder) ;
7477
75- finder. properties
78+ Ok ( finder. properties )
7679}
7780
7881struct DefaultCssImportFinder {
@@ -98,14 +101,14 @@ impl Visit for DefaultCssImportFinder {
98101 }
99102}
100103
101- pub fn extract_default_css_imports ( tsx_code : & str ) -> HashSet < ( String , String ) > {
102- let ( module, _) = module_parser ( tsx_code) ;
104+ pub fn extract_default_css_imports ( tsx_code : & str ) -> anyhow :: Result < HashSet < ( String , String ) > > {
105+ let ( module, _) = module_parser ( tsx_code) ? ;
103106
104107 let mut finder = DefaultCssImportFinder {
105108 imported_variables : HashSet :: new ( ) ,
106109 } ;
107110
108111 module. visit_with ( & mut finder) ;
109112
110- finder. imported_variables
113+ Ok ( finder. imported_variables )
111114}
0 commit comments