@@ -332,12 +332,20 @@ const Dispatcher: DispatcherType = {
332
332
333
333
// Inspect
334
334
335
+ export type HookSource = {
336
+ lineNumber : number | null ,
337
+ columnNumber : number | null ,
338
+ fileName : string | null ,
339
+ functionName : string | null ,
340
+ } ;
341
+
335
342
export type HooksNode = {
336
343
id : number | null ,
337
344
isStateEditable : boolean ,
338
345
name : string ,
339
346
value : mixed ,
340
347
subHooks : Array < HooksNode > ,
348
+ hookSource ? : HookSource ,
341
349
...
342
350
} ;
343
351
export type HooksTree = Array< HooksNode > ;
@@ -473,7 +481,11 @@ function parseCustomHookName(functionName: void | string): string {
473
481
return functionName . substr ( startIndex ) ;
474
482
}
475
483
476
- function buildTree ( rootStack , readHookLog ) : HooksTree {
484
+ function buildTree (
485
+ rootStack ,
486
+ readHookLog ,
487
+ includeHooksSource : boolean ,
488
+ ) : HooksTree {
477
489
const rootChildren = [ ] ;
478
490
let prevStack = null ;
479
491
let levelChildren = rootChildren ;
@@ -508,13 +520,25 @@ function buildTree(rootStack, readHookLog): HooksTree {
508
520
// to the tree.
509
521
for ( let j = stack . length - commonSteps - 1 ; j >= 1 ; j -- ) {
510
522
const children = [ ] ;
511
- levelChildren . push ( {
523
+ const stackFrame = stack [ j ] ;
524
+ const levelChild : HooksNode = {
512
525
id : null ,
513
526
isStateEditable : false ,
514
527
name : parseCustomHookName ( stack [ j - 1 ] . functionName ) ,
515
528
value : undefined ,
516
529
subHooks : children ,
517
- } ) ;
530
+ } ;
531
+
532
+ if ( includeHooksSource ) {
533
+ levelChild . hookSource = {
534
+ lineNumber : stackFrame . lineNumber ,
535
+ columnNumber : stackFrame . columnNumber ,
536
+ functionName : stackFrame . functionName ,
537
+ fileName : stackFrame . fileName ,
538
+ } ;
539
+ }
540
+
541
+ levelChildren . push ( levelChild ) ;
518
542
stackOfChildren . push ( levelChildren ) ;
519
543
levelChildren = children ;
520
544
}
@@ -531,14 +555,33 @@ function buildTree(rootStack, readHookLog): HooksTree {
531
555
532
556
// For the time being, only State and Reducer hooks support runtime overrides.
533
557
const isStateEditable = primitive === 'Reducer' || primitive === 'State' ;
534
-
535
- levelChildren . push ( {
558
+ const levelChild : HooksNode = {
536
559
id,
537
560
isStateEditable,
538
561
name : primitive ,
539
562
value : hook . value ,
540
563
subHooks : [ ] ,
541
- } ) ;
564
+ } ;
565
+
566
+ if ( includeHooksSource ) {
567
+ const hookSource : HookSource = {
568
+ lineNumber : null ,
569
+ functionName : null ,
570
+ fileName : null ,
571
+ columnNumber : null ,
572
+ } ;
573
+ if ( stack && stack . length >= 1 ) {
574
+ const stackFrame = stack [ 0 ] ;
575
+ hookSource . lineNumber = stackFrame . lineNumber ;
576
+ hookSource . functionName = stackFrame . functionName ;
577
+ hookSource . fileName = stackFrame . fileName ;
578
+ hookSource . columnNumber = stackFrame . columnNumber ;
579
+ }
580
+
581
+ levelChild . hookSource = hookSource ;
582
+ }
583
+
584
+ levelChildren . push ( levelChild ) ;
542
585
}
543
586
544
587
// Associate custom hook values (useDebugValue() hook entries) with the correct hooks.
@@ -585,6 +628,7 @@ export function inspectHooks<Props>(
585
628
renderFunction : Props => React$Node ,
586
629
props : Props ,
587
630
currentDispatcher : ?CurrentDispatcherRef ,
631
+ includeHooksSource ? : boolean = false ,
588
632
) : HooksTree {
589
633
// DevTools will pass the current renderer's injected dispatcher.
590
634
// Other apps might compile debug hooks as part of their app though.
@@ -605,7 +649,7 @@ export function inspectHooks<Props>(
605
649
currentDispatcher . current = previousDispatcher ;
606
650
}
607
651
const rootStack = ErrorStackParser . parse ( ancestorStackError ) ;
608
- return buildTree ( rootStack , readHookLog ) ;
652
+ return buildTree ( rootStack , readHookLog , includeHooksSource ) ;
609
653
}
610
654
611
655
function setupContexts ( contextMap : Map < ReactContext < any > , any > , fiber : Fiber ) {
@@ -634,6 +678,7 @@ function inspectHooksOfForwardRef<Props, Ref>(
634
678
props : Props ,
635
679
ref : Ref ,
636
680
currentDispatcher : CurrentDispatcherRef ,
681
+ includeHooksSource : boolean ,
637
682
) : HooksTree {
638
683
const previousDispatcher = currentDispatcher . current ;
639
684
let readHookLog ;
@@ -648,7 +693,7 @@ function inspectHooksOfForwardRef<Props, Ref>(
648
693
currentDispatcher . current = previousDispatcher ;
649
694
}
650
695
const rootStack = ErrorStackParser.parse(ancestorStackError);
651
- return buildTree(rootStack, readHookLog);
696
+ return buildTree(rootStack, readHookLog, includeHooksSource );
652
697
}
653
698
654
699
function resolveDefaultProps ( Component , baseProps ) {
@@ -669,6 +714,7 @@ function resolveDefaultProps(Component, baseProps) {
669
714
export function inspectHooksOfFiber (
670
715
fiber : Fiber ,
671
716
currentDispatcher : ?CurrentDispatcherRef ,
717
+ includeHooksSource ?: boolean = false ,
672
718
) {
673
719
// DevTools will pass the current renderer's injected dispatcher.
674
720
// Other apps might compile debug hooks as part of their app though.
@@ -706,9 +752,10 @@ export function inspectHooksOfFiber(
706
752
props ,
707
753
fiber . ref ,
708
754
currentDispatcher ,
755
+ includeHooksSource ,
709
756
) ;
710
757
}
711
- return inspectHooks(type, props, currentDispatcher);
758
+ return inspectHooks(type, props, currentDispatcher, includeHooksSource );
712
759
} finally {
713
760
currentHook = null ;
714
761
restoreContexts ( contextMap ) ;
0 commit comments