7
7
* @flow
8
8
*/
9
9
10
- import { __PERFORMANCE_PROFILE__ } from 'react-devtools-shared/src/constants ' ;
10
+ import { withSyncPerformanceMark } from 'react-devtools-shared/src/PerformanceMarks ' ;
11
11
import traverse , { NodePath , Node } from '@babel/traverse' ;
12
12
import { File } from '@babel/types' ;
13
13
@@ -28,15 +28,6 @@ export type SourceFileASTWithHookDetails = {
28
28
29
29
export const NO_HOOK_NAME = '<no-hook>' ;
30
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
-
40
31
const AST_NODE_TYPES = Object . freeze ( {
41
32
PROGRAM : 'Program' ,
42
33
CALL_EXPRESSION : 'CallExpression' ,
@@ -141,13 +132,10 @@ export function getHookName(
141
132
originalSourceLineNumber : number ,
142
133
originalSourceColumnNumber : number ,
143
134
) : string | null {
144
- if ( __PERFORMANCE_PROFILE__ ) {
145
- mark ( 'getPotentialHookDeclarationsFromAST(originalSourceAST)' ) ;
146
- }
147
- const hooksFromAST = getPotentialHookDeclarationsFromAST ( originalSourceAST ) ;
148
- if ( __PERFORMANCE_PROFILE__ ) {
149
- measure ( 'getPotentialHookDeclarationsFromAST(originalSourceAST)' ) ;
150
- }
135
+ const hooksFromAST = withSyncPerformanceMark (
136
+ 'getPotentialHookDeclarationsFromAST(originalSourceAST)' ,
137
+ ( ) => getPotentialHookDeclarationsFromAST ( originalSourceAST ) ,
138
+ ) ;
151
139
152
140
let potentialReactHookASTNode = null ;
153
141
if ( originalSourceColumnNumber === 0 ) {
@@ -188,29 +176,23 @@ export function getHookName(
188
176
// nodesAssociatedWithReactHookASTNode could directly be used to obtain the hook variable name
189
177
// depending on the type of potentialReactHookASTNode
190
178
try {
191
- if ( __PERFORMANCE_PROFILE__ ) {
192
- mark ( 'getFilteredHookASTNodes()' ) ;
193
- }
194
- const nodesAssociatedWithReactHookASTNode = getFilteredHookASTNodes (
195
- potentialReactHookASTNode ,
196
- hooksFromAST ,
197
- originalSourceCode ,
179
+ const nodesAssociatedWithReactHookASTNode = withSyncPerformanceMark (
180
+ 'getFilteredHookASTNodes()' ,
181
+ ( ) =>
182
+ getFilteredHookASTNodes (
183
+ potentialReactHookASTNode ,
184
+ hooksFromAST ,
185
+ originalSourceCode ,
186
+ ) ,
198
187
) ;
199
- if ( __PERFORMANCE_PROFILE__ ) {
200
- measure ( 'getFilteredHookASTNodes()' ) ;
201
- }
202
188
203
- if ( __PERFORMANCE_PROFILE__ ) {
204
- mark ( 'getHookNameFromNode()' ) ;
205
- }
206
- const name = getHookNameFromNode (
207
- hook ,
208
- nodesAssociatedWithReactHookASTNode ,
209
- potentialReactHookASTNode ,
189
+ const name = withSyncPerformanceMark ( 'getHookNameFromNode()' , ( ) =>
190
+ getHookNameFromNode (
191
+ hook ,
192
+ nodesAssociatedWithReactHookASTNode ,
193
+ potentialReactHookASTNode ,
194
+ ) ,
210
195
) ;
211
- if ( __PERFORMANCE_PROFILE__ ) {
212
- measure ( 'getHookNameFromNode()' ) ;
213
- }
214
196
215
197
return name ;
216
198
} catch ( error ) {
@@ -315,19 +297,15 @@ function getHookVariableName(
315
297
316
298
function getPotentialHookDeclarationsFromAST ( sourceAST : File ) : NodePath [ ] {
317
299
const potentialHooksFound : NodePath [ ] = [ ] ;
318
- if ( __PERFORMANCE_PROFILE__ ) {
319
- mark ( 'traverse(sourceAST)' ) ;
320
- }
321
- traverse ( sourceAST , {
322
- enter ( path ) {
323
- if ( path . isVariableDeclarator ( ) && isPotentialHookDeclaration ( path ) ) {
324
- potentialHooksFound . push ( path ) ;
325
- }
326
- } ,
327
- } ) ;
328
- if ( __PERFORMANCE_PROFILE__ ) {
329
- measure ( 'traverse(sourceAST)' ) ;
330
- }
300
+ withSyncPerformanceMark ( 'traverse(sourceAST)' , ( ) =>
301
+ traverse ( sourceAST , {
302
+ enter ( path ) {
303
+ if ( path . isVariableDeclarator ( ) && isPotentialHookDeclaration ( path ) ) {
304
+ potentialHooksFound . push ( path ) ;
305
+ }
306
+ } ,
307
+ } ) ,
308
+ ) ;
331
309
return potentialHooksFound ;
332
310
}
333
311
0 commit comments