Repository: facebook/react
File: packages/react-devtools-shared/src/backend/fiber/renderer.js (JavaScript)

Code before:
      ));
    }
    if (typeof unresolvedSource === 'string') {
      const idx = unresolvedSource.lastIndexOf('\n');
      const lastLine =
        idx === -1 ? unresolvedSource : unresolvedSource.slice(idx + 1);
      return (instance.source = parseSourceFromComponentStack(lastLine));
    }

    // $FlowFixMe: refined.
    return unresolvedSource;
  }

  function internal_only_getComponentTree(): string {
    let treeString = '';

    function buildTreeString(
      instance: DevToolsInstance,
      prefix: string = '',
      isLastChild: boolean = true,
    ): void {
      if (!instance) return;

      const name =
        (instance.kind !== VIRTUAL_INSTANCE
          ? getDisplayNameForFiber(instance.data)
          : instance.data.name) || 'Unknown';

      const id = instance.id !== undefined ? instance.id : 'unknown';

      if (name !== 'createRoot()') {
        treeString +=
          prefix +
          (isLastChild ? '└── ' : '├── ') +
          name +
          ' (id: ' +
          id +
          ')\n';
      }

      const childPrefix = prefix + (isLastChild ? '    ' : '│   ');

      let childCount = 0;
      let tempChild = instance.firstChild;
      while (tempChild !== null) {
        childCount++;
        tempChild = tempChild.nextSibling;
      }

      let child = instance.firstChild;
      let currentChildIndex = 0;

Review comment (performance):
There is a custom logic for Compiler, whereas every Fiber that has a trace of `useMemoCache` would have a `Forget(...)` prefix. Also for `React.memo` and HOC.

You kinda creating a dependency here between RDT and MCP, because if next time we decide to change `Forget` to anything else like `Compiled`, it would require updating MCP prompt or whatever.

I am not against keeping it like this for now, but maybe worth forking the `getDisplayNameForFiber` function and adding some customisation. 

Code after:
      ));
    }
    if (typeof unresolvedSource === 'string') {
      const idx = unresolvedSource.lastIndexOf('\n');
      const lastLine =
        idx === -1 ? unresolvedSource : unresolvedSource.slice(idx + 1);
      return (instance.source = parseSourceFromComponentStack(lastLine));
    }

    // $FlowFixMe: refined.
    return unresolvedSource;
  }

  type InternalMcpFunctions = {
    __internal_only_getComponentTree?: Function,
  };

  const internalMcpFunctions: InternalMcpFunctions = {};
  if (__IS_INTERNAL_MCP_BUILD__) {
    // eslint-disable-next-line no-inner-declarations
    function __internal_only_getComponentTree(): string {
      let treeString = '';

      function buildTreeString(
        instance: DevToolsInstance,
        prefix: string = '',
        isLastChild: boolean = true,
      ): void {
        if (!instance) return;

        const name =
          (instance.kind !== VIRTUAL_INSTANCE
            ? getDisplayNameForFiber(instance.data)
            : instance.data.name) || 'Unknown';

        const id = instance.id !== undefined ? instance.id : 'unknown';

        if (name !== 'createRoot()') {
          treeString +=
            prefix +
            (isLastChild ? '└── ' : '├── ') +
            name +
            ' (id: ' +
            id +
            ')\n';
        }

        const childPrefix = prefix + (isLastChild ? '    ' : '│   ');

        let childCount = 0;
        let tempChild = instance.firstChild;