5
5
use swc_core:: ecma:: ast:: { FnDecl , FnExpr , Function } ;
6
6
7
7
#[ derive( Debug , Clone ) ]
8
- pub enum FunctionType {
8
+ pub ( crate ) enum FunctionType {
9
9
FunctionDeclaration ,
10
10
FunctionExpression ,
11
11
Method ,
@@ -15,45 +15,161 @@ pub enum FunctionType {
15
15
pub enum FunctionKind {
16
16
Sync ,
17
17
Async ,
18
- Generator ,
19
- AsyncGenerator ,
20
18
}
21
19
22
20
impl FunctionKind {
23
21
#[ must_use]
24
22
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 )
31
24
}
32
25
33
26
#[ must_use]
34
27
pub fn matches ( & self , func : & Function ) -> bool {
35
28
match self {
36
29
FunctionKind :: Sync => !func. is_async && !func. is_generator ,
37
30
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" ,
40
39
}
41
40
}
42
41
}
43
42
44
43
#[ 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
+ } ,
51
70
}
52
71
53
72
impl 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
+
54
170
fn maybe_increment_count ( & self , matches_except_count : bool , count : & mut usize ) -> bool {
55
171
if matches_except_count {
56
- if self . index == * count {
172
+ if self . index ( ) == * count {
57
173
true
58
174
} else {
59
175
* count += 1 ;
@@ -65,23 +181,23 @@ impl FunctionQuery {
65
181
}
66
182
67
183
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 ( ) ;
71
187
self . maybe_increment_count ( matches_except_count, count)
72
188
}
73
189
74
190
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 ( ) ;
78
194
self . maybe_increment_count ( matches_except_count, count)
79
195
}
80
196
81
197
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 ( ) ;
85
201
self . maybe_increment_count ( matches_except_count, count)
86
202
}
87
203
}
0 commit comments