77 * @flow
88 */
99
10+ import { __PERFORMANCE_PROFILE__ } from 'react-devtools-shared/src/constants' ;
1011import traverse , { NodePath , Node } from '@babel/traverse' ;
1112import { File } from '@babel/types' ;
1213
@@ -27,6 +28,15 @@ export type SourceFileASTWithHookDetails = {
2728
2829export const NO_HOOK_NAME = '<no-hook>' ;
2930
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+
3040const AST_NODE_TYPES = Object . freeze ( {
3141 PROGRAM : 'Program' ,
3242 CALL_EXPRESSION : 'CallExpression' ,
@@ -131,7 +141,13 @@ export function getHookName(
131141 originalSourceLineNumber : number ,
132142 originalSourceColumnNumber : number ,
133143) : string | null {
144+ if ( __PERFORMANCE_PROFILE__ ) {
145+ mark ( 'getPotentialHookDeclarationsFromAST(originalSourceAST)' ) ;
146+ }
134147 const hooksFromAST = getPotentialHookDeclarationsFromAST ( originalSourceAST ) ;
148+ if ( __PERFORMANCE_PROFILE__ ) {
149+ measure ( 'getPotentialHookDeclarationsFromAST(originalSourceAST)' ) ;
150+ }
135151
136152 let potentialReactHookASTNode = null ;
137153 if ( originalSourceColumnNumber === 0 ) {
@@ -144,6 +160,7 @@ export function getHookName(
144160 node ,
145161 originalSourceLineNumber ,
146162 ) ;
163+
147164 const hookDeclaractionCheck = isConfirmedHookDeclaration ( node ) ;
148165 return nodeLocationCheck && hookDeclaractionCheck ;
149166 } ) ;
@@ -158,6 +175,7 @@ export function getHookName(
158175 originalSourceLineNumber ,
159176 originalSourceColumnNumber ,
160177 ) ;
178+
161179 const hookDeclaractionCheck = isConfirmedHookDeclaration ( node ) ;
162180 return nodeLocationCheck && hookDeclaractionCheck ;
163181 } ) ;
@@ -170,17 +188,31 @@ export function getHookName(
170188 // nodesAssociatedWithReactHookASTNode could directly be used to obtain the hook variable name
171189 // depending on the type of potentialReactHookASTNode
172190 try {
191+ if ( __PERFORMANCE_PROFILE__ ) {
192+ mark ( 'getFilteredHookASTNodes()' ) ;
193+ }
173194 const nodesAssociatedWithReactHookASTNode = getFilteredHookASTNodes (
174195 potentialReactHookASTNode ,
175196 hooksFromAST ,
176197 originalSourceCode ,
177198 ) ;
199+ if ( __PERFORMANCE_PROFILE__ ) {
200+ measure ( 'getFilteredHookASTNodes()' ) ;
201+ }
178202
179- return getHookNameFromNode (
203+ if ( __PERFORMANCE_PROFILE__ ) {
204+ mark ( 'getHookNameFromNode()' ) ;
205+ }
206+ const name = getHookNameFromNode (
180207 hook ,
181208 nodesAssociatedWithReactHookASTNode ,
182209 potentialReactHookASTNode ,
183210 ) ;
211+ if ( __PERFORMANCE_PROFILE__ ) {
212+ measure ( 'getHookNameFromNode()' ) ;
213+ }
214+
215+ return name ;
184216 } catch ( error ) {
185217 console . error ( error ) ;
186218 return null ;
@@ -283,13 +315,19 @@ function getHookVariableName(
283315
284316function getPotentialHookDeclarationsFromAST ( sourceAST : File ) : NodePath [ ] {
285317 const potentialHooksFound : NodePath [ ] = [ ] ;
318+ if ( __PERFORMANCE_PROFILE__ ) {
319+ mark ( 'traverse(sourceAST)' ) ;
320+ }
286321 traverse ( sourceAST , {
287322 enter ( path ) {
288323 if ( path . isVariableDeclarator ( ) && isPotentialHookDeclaration ( path ) ) {
289324 potentialHooksFound . push ( path ) ;
290325 }
291326 } ,
292327 } ) ;
328+ if ( __PERFORMANCE_PROFILE__ ) {
329+ measure ( 'traverse(sourceAST)' ) ;
330+ }
293331 return potentialHooksFound ;
294332}
295333
0 commit comments