55use swc_core:: ecma:: ast:: { FnDecl , FnExpr , Function } ;
66
77#[ derive( Debug , Clone ) ]
8- pub enum FunctionType {
8+ pub ( crate ) enum FunctionType {
99 FunctionDeclaration ,
1010 FunctionExpression ,
1111 Method ,
@@ -15,45 +15,161 @@ pub enum FunctionType {
1515pub enum FunctionKind {
1616 Sync ,
1717 Async ,
18- Generator ,
19- AsyncGenerator ,
2018}
2119
2220impl FunctionKind {
2321 #[ must_use]
2422 pub fn is_async ( & self ) -> bool {
25- matches ! ( self , FunctionKind :: Async | FunctionKind :: AsyncGenerator )
26- }
27-
28- #[ must_use]
29- pub fn is_generator ( & self ) -> bool {
30- matches ! ( self , FunctionKind :: Generator | FunctionKind :: AsyncGenerator )
23+ matches ! ( self , FunctionKind :: Async )
3124 }
3225
3326 #[ must_use]
3427 pub fn matches ( & self , func : & Function ) -> bool {
3528 match self {
3629 FunctionKind :: Sync => !func. is_async && !func. is_generator ,
3730 FunctionKind :: Async => func. is_async && !func. is_generator ,
38- FunctionKind :: Generator => !func. is_async && func. is_generator ,
39- FunctionKind :: AsyncGenerator => func. is_async && func. is_generator ,
31+ }
32+ }
33+
34+ #[ must_use]
35+ pub fn tracing_operator ( & self ) -> & ' static str {
36+ match self {
37+ FunctionKind :: Sync => "traceSync" ,
38+ FunctionKind :: Async => "tracePromise" ,
4039 }
4140 }
4241}
4342
4443#[ derive( Debug , Clone ) ]
45- pub struct FunctionQuery {
46- pub name : String ,
47- pub class : Option < String > ,
48- pub typ : FunctionType ,
49- pub kind : FunctionKind ,
50- pub index : usize ,
44+ pub enum FunctionQuery {
45+ ClassConstructor {
46+ class_name : String ,
47+ index : usize ,
48+ } ,
49+ ClassMethod {
50+ class_name : String ,
51+ method_name : String ,
52+ kind : FunctionKind ,
53+ index : usize ,
54+ } ,
55+ ObjectMethod {
56+ method_name : String ,
57+ kind : FunctionKind ,
58+ index : usize ,
59+ } ,
60+ FunctionDeclaration {
61+ function_name : String ,
62+ kind : FunctionKind ,
63+ index : usize ,
64+ } ,
65+ FunctionExpression {
66+ expression_name : String ,
67+ kind : FunctionKind ,
68+ index : usize ,
69+ } ,
5170}
5271
5372impl FunctionQuery {
73+ #[ must_use]
74+ pub fn class_constructor ( class_name : & str ) -> Self {
75+ FunctionQuery :: ClassConstructor {
76+ class_name : class_name. to_string ( ) ,
77+ index : 0 ,
78+ }
79+ }
80+
81+ #[ must_use]
82+ pub fn class_method ( class_name : & str , method_name : & str , kind : FunctionKind ) -> Self {
83+ FunctionQuery :: ClassMethod {
84+ class_name : class_name. to_string ( ) ,
85+ method_name : method_name. to_string ( ) ,
86+ kind,
87+ index : 0 ,
88+ }
89+ }
90+
91+ #[ must_use]
92+ pub fn object_method ( method_name : & str , kind : FunctionKind ) -> Self {
93+ FunctionQuery :: ObjectMethod {
94+ method_name : method_name. to_string ( ) ,
95+ kind,
96+ index : 0 ,
97+ }
98+ }
99+
100+ #[ must_use]
101+ pub fn function_declaration ( function_name : & str , kind : FunctionKind ) -> Self {
102+ FunctionQuery :: FunctionDeclaration {
103+ function_name : function_name. to_string ( ) ,
104+ kind,
105+ index : 0 ,
106+ }
107+ }
108+
109+ #[ must_use]
110+ pub fn function_expression ( expression_name : & str , kind : FunctionKind ) -> Self {
111+ FunctionQuery :: FunctionExpression {
112+ expression_name : expression_name. to_string ( ) ,
113+ kind,
114+ index : 0 ,
115+ }
116+ }
117+
118+ pub ( crate ) fn kind ( & self ) -> & FunctionKind {
119+ match self {
120+ FunctionQuery :: ClassConstructor { .. } => & FunctionKind :: Sync ,
121+ FunctionQuery :: ClassMethod { kind, .. }
122+ | FunctionQuery :: ObjectMethod { kind, .. }
123+ | FunctionQuery :: FunctionDeclaration { kind, .. }
124+ | FunctionQuery :: FunctionExpression { kind, .. } => kind,
125+ }
126+ }
127+
128+ pub ( crate ) fn name ( & self ) -> & str {
129+ match self {
130+ FunctionQuery :: ClassConstructor { .. } => "constructor" ,
131+ FunctionQuery :: ClassMethod { method_name, .. }
132+ | FunctionQuery :: ObjectMethod { method_name, .. } => method_name,
133+ FunctionQuery :: FunctionDeclaration { function_name, .. } => function_name,
134+ FunctionQuery :: FunctionExpression {
135+ expression_name, ..
136+ } => expression_name,
137+ }
138+ }
139+
140+ pub ( crate ) fn typ ( & self ) -> FunctionType {
141+ match self {
142+ FunctionQuery :: ClassConstructor { .. }
143+ | FunctionQuery :: ClassMethod { .. }
144+ | FunctionQuery :: ObjectMethod { .. } => FunctionType :: Method ,
145+ FunctionQuery :: FunctionDeclaration { .. } => FunctionType :: FunctionDeclaration ,
146+ FunctionQuery :: FunctionExpression { .. } => FunctionType :: FunctionExpression ,
147+ }
148+ }
149+
150+ #[ must_use]
151+ pub ( crate ) fn index ( & self ) -> usize {
152+ match self {
153+ FunctionQuery :: ClassConstructor { index, .. }
154+ | FunctionQuery :: ClassMethod { index, .. }
155+ | FunctionQuery :: ObjectMethod { index, .. }
156+ | FunctionQuery :: FunctionDeclaration { index, .. }
157+ | FunctionQuery :: FunctionExpression { index, .. } => * index,
158+ }
159+ }
160+
161+ #[ must_use]
162+ pub ( crate ) fn class_name ( & self ) -> Option < & str > {
163+ match self {
164+ FunctionQuery :: ClassConstructor { class_name, .. }
165+ | FunctionQuery :: ClassMethod { class_name, .. } => Some ( class_name) ,
166+ _ => None ,
167+ }
168+ }
169+
54170 fn maybe_increment_count ( & self , matches_except_count : bool , count : & mut usize ) -> bool {
55171 if matches_except_count {
56- if self . index == * count {
172+ if self . index ( ) == * count {
57173 true
58174 } else {
59175 * count += 1 ;
@@ -65,23 +181,23 @@ impl FunctionQuery {
65181 }
66182
67183 pub fn matches_decl ( & self , func : & FnDecl , count : & mut usize ) -> bool {
68- let matches_except_count = matches ! ( self . typ, FunctionType :: FunctionDeclaration )
69- && self . kind . matches ( & func. function )
70- && func. ident . sym == self . name ;
184+ let matches_except_count = matches ! ( self . typ( ) , FunctionType :: FunctionDeclaration )
185+ && self . kind ( ) . matches ( & func. function )
186+ && func. ident . sym == self . name ( ) ;
71187 self . maybe_increment_count ( matches_except_count, count)
72188 }
73189
74190 pub fn matches_expr ( & self , func : & FnExpr , count : & mut usize , name : & str ) -> bool {
75- let matches_except_count = matches ! ( self . typ, FunctionType :: FunctionExpression )
76- && self . kind . matches ( & func. function )
77- && name == self . name ;
191+ let matches_except_count = matches ! ( self . typ( ) , FunctionType :: FunctionExpression )
192+ && self . kind ( ) . matches ( & func. function )
193+ && name == self . name ( ) ;
78194 self . maybe_increment_count ( matches_except_count, count)
79195 }
80196
81197 pub fn matches_method ( & self , func : & Function , count : & mut usize , name : & str ) -> bool {
82- let matches_except_count = matches ! ( self . typ, FunctionType :: Method )
83- && self . kind . matches ( func)
84- && name == self . name ;
198+ let matches_except_count = matches ! ( self . typ( ) , FunctionType :: Method )
199+ && self . kind ( ) . matches ( func)
200+ && name == self . name ( ) ;
85201 self . maybe_increment_count ( matches_except_count, count)
86202 }
87203}
0 commit comments