7
7
* @flow
8
8
*/
9
9
10
+ import { __PERFORMANCE_PROFILE__ } from 'react-devtools-shared/src/constants' ;
10
11
import traverse , { NodePath , Node } from '@babel/traverse' ;
11
12
import { File } from '@babel/types' ;
12
13
@@ -27,6 +28,15 @@ export type SourceFileASTWithHookDetails = {
27
28
28
29
export const NO_HOOK_NAME = '<no-hook>' ;
29
30
31
+ function mark ( markName : string ) : void {
32
+ performance . mark ( markName + '-start' ) ;
33
+ }
34
+
35
+ function measure ( markName : string ) : void {
36
+ performance . mark ( markName + '-end' ) ;
37
+ performance . measure ( markName , markName + '-start' , markName + '-end' ) ;
38
+ }
39
+
30
40
const AST_NODE_TYPES = Object . freeze ( {
31
41
PROGRAM : 'Program' ,
32
42
CALL_EXPRESSION : 'CallExpression' ,
@@ -131,7 +141,13 @@ export function getHookName(
131
141
originalSourceLineNumber : number ,
132
142
originalSourceColumnNumber : number ,
133
143
) : string | null {
144
+ if ( __PERFORMANCE_PROFILE__ ) {
145
+ mark ( 'getPotentialHookDeclarationsFromAST(originalSourceAST)' ) ;
146
+ }
134
147
const hooksFromAST = getPotentialHookDeclarationsFromAST ( originalSourceAST ) ;
148
+ if ( __PERFORMANCE_PROFILE__ ) {
149
+ measure ( 'getPotentialHookDeclarationsFromAST(originalSourceAST)' ) ;
150
+ }
135
151
136
152
let potentialReactHookASTNode = null ;
137
153
if ( originalSourceColumnNumber === 0 ) {
@@ -144,6 +160,7 @@ export function getHookName(
144
160
node ,
145
161
originalSourceLineNumber ,
146
162
) ;
163
+
147
164
const hookDeclaractionCheck = isConfirmedHookDeclaration ( node ) ;
148
165
return nodeLocationCheck && hookDeclaractionCheck ;
149
166
} ) ;
@@ -158,6 +175,7 @@ export function getHookName(
158
175
originalSourceLineNumber ,
159
176
originalSourceColumnNumber ,
160
177
) ;
178
+
161
179
const hookDeclaractionCheck = isConfirmedHookDeclaration ( node ) ;
162
180
return nodeLocationCheck && hookDeclaractionCheck ;
163
181
} ) ;
@@ -170,17 +188,31 @@ export function getHookName(
170
188
// nodesAssociatedWithReactHookASTNode could directly be used to obtain the hook variable name
171
189
// depending on the type of potentialReactHookASTNode
172
190
try {
191
+ if ( __PERFORMANCE_PROFILE__ ) {
192
+ mark ( 'getFilteredHookASTNodes()' ) ;
193
+ }
173
194
const nodesAssociatedWithReactHookASTNode = getFilteredHookASTNodes (
174
195
potentialReactHookASTNode ,
175
196
hooksFromAST ,
176
197
originalSourceCode ,
177
198
) ;
199
+ if ( __PERFORMANCE_PROFILE__ ) {
200
+ measure ( 'getFilteredHookASTNodes()' ) ;
201
+ }
178
202
179
- return getHookNameFromNode (
203
+ if ( __PERFORMANCE_PROFILE__ ) {
204
+ mark ( 'getHookNameFromNode()' ) ;
205
+ }
206
+ const name = getHookNameFromNode (
180
207
hook ,
181
208
nodesAssociatedWithReactHookASTNode ,
182
209
potentialReactHookASTNode ,
183
210
) ;
211
+ if ( __PERFORMANCE_PROFILE__ ) {
212
+ measure ( 'getHookNameFromNode()' ) ;
213
+ }
214
+
215
+ return name ;
184
216
} catch ( error ) {
185
217
console . error ( error ) ;
186
218
return null ;
@@ -283,13 +315,19 @@ function getHookVariableName(
283
315
284
316
function getPotentialHookDeclarationsFromAST ( sourceAST : File ) : NodePath [ ] {
285
317
const potentialHooksFound : NodePath [ ] = [ ] ;
318
+ if ( __PERFORMANCE_PROFILE__ ) {
319
+ mark ( 'traverse(sourceAST)' ) ;
320
+ }
286
321
traverse ( sourceAST , {
287
322
enter ( path ) {
288
323
if ( path . isVariableDeclarator ( ) && isPotentialHookDeclaration ( path ) ) {
289
324
potentialHooksFound . push ( path ) ;
290
325
}
291
326
} ,
292
327
} ) ;
328
+ if ( __PERFORMANCE_PROFILE__ ) {
329
+ measure ( 'traverse(sourceAST)' ) ;
330
+ }
293
331
return potentialHooksFound ;
294
332
}
295
333
0 commit comments