1. /**
    
  2.  * Copyright (c) Meta Platforms, Inc. and affiliates.
    
  3.  *
    
  4.  * This source code is licensed under the MIT license found in the
    
  5.  * LICENSE file in the root directory of this source tree.
    
  6.  *
    
  7.  * @flow
    
  8.  */
    
  9. 
    
  10. // This is a DevTools fork of ReactFiberComponentStack.
    
  11. // This fork enables DevTools to use the same "native" component stack format,
    
  12. // while still maintaining support for multiple renderer versions
    
  13. // (which use different values for ReactTypeOfWork).
    
  14. 
    
  15. import type {Fiber} from 'react-reconciler/src/ReactInternalTypes';
    
  16. import type {CurrentDispatcherRef, WorkTagMap} from './types';
    
  17. 
    
  18. import {
    
  19.   describeBuiltInComponentFrame,
    
  20.   describeFunctionComponentFrame,
    
  21.   describeClassComponentFrame,
    
  22. } from './DevToolsComponentStackFrame';
    
  23. 
    
  24. export function describeFiber(
    
  25.   workTagMap: WorkTagMap,
    
  26.   workInProgress: Fiber,
    
  27.   currentDispatcherRef: CurrentDispatcherRef,
    
  28. ): string {
    
  29.   const {
    
  30.     HostComponent,
    
  31.     LazyComponent,
    
  32.     SuspenseComponent,
    
  33.     SuspenseListComponent,
    
  34.     FunctionComponent,
    
  35.     IndeterminateComponent,
    
  36.     SimpleMemoComponent,
    
  37.     ForwardRef,
    
  38.     ClassComponent,
    
  39.   } = workTagMap;
    
  40. 
    
  41.   const owner: null | Function = __DEV__
    
  42.     ? workInProgress._debugOwner
    
  43.       ? workInProgress._debugOwner.type
    
  44.       : null
    
  45.     : null;
    
  46.   switch (workInProgress.tag) {
    
  47.     case HostComponent:
    
  48.       return describeBuiltInComponentFrame(workInProgress.type, owner);
    
  49.     case LazyComponent:
    
  50.       return describeBuiltInComponentFrame('Lazy', owner);
    
  51.     case SuspenseComponent:
    
  52.       return describeBuiltInComponentFrame('Suspense', owner);
    
  53.     case SuspenseListComponent:
    
  54.       return describeBuiltInComponentFrame('SuspenseList', owner);
    
  55.     case FunctionComponent:
    
  56.     case IndeterminateComponent:
    
  57.     case SimpleMemoComponent:
    
  58.       return describeFunctionComponentFrame(
    
  59.         workInProgress.type,
    
  60.         owner,
    
  61.         currentDispatcherRef,
    
  62.       );
    
  63.     case ForwardRef:
    
  64.       return describeFunctionComponentFrame(
    
  65.         workInProgress.type.render,
    
  66.         owner,
    
  67.         currentDispatcherRef,
    
  68.       );
    
  69.     case ClassComponent:
    
  70.       return describeClassComponentFrame(
    
  71.         workInProgress.type,
    
  72.         owner,
    
  73.         currentDispatcherRef,
    
  74.       );
    
  75.     default:
    
  76.       return '';
    
  77.   }
    
  78. }
    
  79. 
    
  80. export function getStackByFiberInDevAndProd(
    
  81.   workTagMap: WorkTagMap,
    
  82.   workInProgress: Fiber,
    
  83.   currentDispatcherRef: CurrentDispatcherRef,
    
  84. ): string {
    
  85.   try {
    
  86.     let info = '';
    
  87.     let node: Fiber = workInProgress;
    
  88.     do {
    
  89.       info += describeFiber(workTagMap, node, currentDispatcherRef);
    
  90.       // $FlowFixMe[incompatible-type] we bail out when we get a null
    
  91.       node = node.return;
    
  92.     } while (node);
    
  93.     return info;
    
  94.   } catch (x) {
    
  95.     return '\nError generating stack: ' + x.message + '\n' + x.stack;
    
  96.   }
    
  97. }