@@ -4,7 +4,7 @@ use swc_ecma_ast::{ImportSpecifier, Module, ModuleDecl};
4
4
use swc_ecma_parser:: { Parser , StringInput , Syntax , TsSyntax } ;
5
5
use swc_ecma_visit:: { Visit , VisitWith } ;
6
6
7
- pub fn module_parser ( tsx_code : & str ) -> ( Module , Lrc < SourceMap > ) {
7
+ pub fn module_parser ( tsx_code : & str ) -> anyhow :: Result < ( Module , Lrc < SourceMap > ) > {
8
8
let cm: Lrc < SourceMap > = Default :: default ( ) ;
9
9
let fm = cm. new_source_file ( FileName :: Custom ( "input.tsx" . into ( ) ) . into ( ) , tsx_code. into ( ) ) ;
10
10
@@ -18,7 +18,10 @@ pub fn module_parser(tsx_code: &str) -> (Module, Lrc<SourceMap>) {
18
18
19
19
let mut parser = Parser :: new ( syntax, StringInput :: from ( & * fm) , None ) ;
20
20
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) )
22
25
}
23
26
24
27
#[ derive( Eq , PartialEq , Hash , Debug ) ]
@@ -60,8 +63,8 @@ pub fn extract_used_classes(
60
63
tsx_code : & str ,
61
64
variable_name : & str ,
62
65
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) ? ;
65
68
66
69
let mut finder = PropertyFinder {
67
70
variable_name : variable_name. to_string ( ) ,
@@ -72,7 +75,7 @@ pub fn extract_used_classes(
72
75
73
76
module. visit_with ( & mut finder) ;
74
77
75
- finder. properties
78
+ Ok ( finder. properties )
76
79
}
77
80
78
81
struct DefaultCssImportFinder {
@@ -98,14 +101,14 @@ impl Visit for DefaultCssImportFinder {
98
101
}
99
102
}
100
103
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) ? ;
103
106
104
107
let mut finder = DefaultCssImportFinder {
105
108
imported_variables : HashSet :: new ( ) ,
106
109
} ;
107
110
108
111
module. visit_with ( & mut finder) ;
109
112
110
- finder. imported_variables
113
+ Ok ( finder. imported_variables )
111
114
}
0 commit comments