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. import type {Fiber} from './ReactInternalTypes';
    
  11. 
    
  12. import {
    
  13.   HostComponent,
    
  14.   HostHoistable,
    
  15.   HostSingleton,
    
  16.   LazyComponent,
    
  17.   SuspenseComponent,
    
  18.   SuspenseListComponent,
    
  19.   FunctionComponent,
    
  20.   IndeterminateComponent,
    
  21.   ForwardRef,
    
  22.   SimpleMemoComponent,
    
  23.   ClassComponent,
    
  24. } from './ReactWorkTags';
    
  25. import {
    
  26.   describeBuiltInComponentFrame,
    
  27.   describeFunctionComponentFrame,
    
  28.   describeClassComponentFrame,
    
  29. } from 'shared/ReactComponentStackFrame';
    
  30. 
    
  31. function describeFiber(fiber: Fiber): string {
    
  32.   const owner: null | Function = __DEV__
    
  33.     ? fiber._debugOwner
    
  34.       ? fiber._debugOwner.type
    
  35.       : null
    
  36.     : null;
    
  37.   const source = __DEV__ ? fiber._debugSource : null;
    
  38.   switch (fiber.tag) {
    
  39.     case HostHoistable:
    
  40.     case HostSingleton:
    
  41.     case HostComponent:
    
  42.       return describeBuiltInComponentFrame(fiber.type, source, owner);
    
  43.     case LazyComponent:
    
  44.       return describeBuiltInComponentFrame('Lazy', source, owner);
    
  45.     case SuspenseComponent:
    
  46.       return describeBuiltInComponentFrame('Suspense', source, owner);
    
  47.     case SuspenseListComponent:
    
  48.       return describeBuiltInComponentFrame('SuspenseList', source, owner);
    
  49.     case FunctionComponent:
    
  50.     case IndeterminateComponent:
    
  51.     case SimpleMemoComponent:
    
  52.       return describeFunctionComponentFrame(fiber.type, source, owner);
    
  53.     case ForwardRef:
    
  54.       return describeFunctionComponentFrame(fiber.type.render, source, owner);
    
  55.     case ClassComponent:
    
  56.       return describeClassComponentFrame(fiber.type, source, owner);
    
  57.     default:
    
  58.       return '';
    
  59.   }
    
  60. }
    
  61. 
    
  62. export function getStackByFiberInDevAndProd(workInProgress: Fiber): string {
    
  63.   try {
    
  64.     let info = '';
    
  65.     let node: Fiber = workInProgress;
    
  66.     do {
    
  67.       info += describeFiber(node);
    
  68.       // $FlowFixMe[incompatible-type] we bail out when we get a null
    
  69.       node = node.return;
    
  70.     } while (node);
    
  71.     return info;
    
  72.   } catch (x) {
    
  73.     return '\nError generating stack: ' + x.message + '\n' + x.stack;
    
  74.   }
    
  75. }