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 {
    
  11.   describeBuiltInComponentFrame,
    
  12.   describeFunctionComponentFrame,
    
  13.   describeClassComponentFrame,
    
  14. } from 'shared/ReactComponentStackFrame';
    
  15. 
    
  16. // DEV-only reverse linked list representing the current component stack
    
  17. type BuiltInComponentStackNode = {
    
  18.   tag: 0,
    
  19.   parent: null | ComponentStackNode,
    
  20.   type: string,
    
  21. };
    
  22. type FunctionComponentStackNode = {
    
  23.   tag: 1,
    
  24.   parent: null | ComponentStackNode,
    
  25.   type: Function,
    
  26. };
    
  27. type ClassComponentStackNode = {
    
  28.   tag: 2,
    
  29.   parent: null | ComponentStackNode,
    
  30.   type: Function,
    
  31. };
    
  32. export type ComponentStackNode =
    
  33.   | BuiltInComponentStackNode
    
  34.   | FunctionComponentStackNode
    
  35.   | ClassComponentStackNode;
    
  36. 
    
  37. export function getStackByComponentStackNode(
    
  38.   componentStack: ComponentStackNode,
    
  39. ): string {
    
  40.   try {
    
  41.     let info = '';
    
  42.     let node: ComponentStackNode = componentStack;
    
  43.     do {
    
  44.       switch (node.tag) {
    
  45.         case 0:
    
  46.           info += describeBuiltInComponentFrame(node.type, null, null);
    
  47.           break;
    
  48.         case 1:
    
  49.           info += describeFunctionComponentFrame(node.type, null, null);
    
  50.           break;
    
  51.         case 2:
    
  52.           info += describeClassComponentFrame(node.type, null, null);
    
  53.           break;
    
  54.       }
    
  55.       // $FlowFixMe[incompatible-type] we bail out when we get a null
    
  56.       node = node.parent;
    
  57.     } while (node);
    
  58.     return info;
    
  59.   } catch (x) {
    
  60.     return '\nError generating stack: ' + x.message + '\n' + x.stack;
    
  61.   }
    
  62. }