From a5861172c6f25a85117aa5e96dd07b8243d2bc84 Mon Sep 17 00:00:00 2001 From: Jorge Cabiedes <57368278+jorge-cab@users.noreply.github.com> Date: Mon, 2 Jun 2025 11:09:42 -0700 Subject: [PATCH] Gate __internal_only_getComponentTree definition --- .../src/backend/fiber/renderer.js | 118 +++++++++--------- 1 file changed, 62 insertions(+), 56 deletions(-) diff --git a/packages/react-devtools-shared/src/backend/fiber/renderer.js b/packages/react-devtools-shared/src/backend/fiber/renderer.js index bdb0d85b39..4a65d5e99d 100644 --- a/packages/react-devtools-shared/src/backend/fiber/renderer.js +++ b/packages/react-devtools-shared/src/backend/fiber/renderer.js @@ -5859,73 +5859,79 @@ export function attach( return unresolvedSource; } - function __internal_only_getComponentTree(): string { - let treeString = ''; + let internalMcpFunctions = {}; + if (__IS_INTERNAL_MCP_BUILD__) { + function __internal_only_getComponentTree(): string { + let treeString = ''; - function buildTreeString( - instance: DevToolsInstance, - prefix: string = '', - isLastChild: boolean = true, - ): void { - if (!instance) return; + 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 name = + (instance.kind !== VIRTUAL_INSTANCE + ? getDisplayNameForFiber(instance.data) + : instance.data.name) || 'Unknown'; - const id = instance.id !== undefined ? instance.id : 'unknown'; + const id = instance.id !== undefined ? instance.id : 'unknown'; - if (name !== 'createRoot()') { - treeString += - prefix + - (isLastChild ? '└── ' : '├── ') + - name + - ' (id: ' + - id + - ')\n'; - } + if (name !== 'createRoot()') { + treeString += + prefix + + (isLastChild ? '└── ' : '├── ') + + name + + ' (id: ' + + id + + ')\n'; + } - const childPrefix = prefix + (isLastChild ? ' ' : '│ '); + const childPrefix = prefix + (isLastChild ? ' ' : '│ '); - let childCount = 0; - let tempChild = instance.firstChild; - while (tempChild !== null) { - childCount++; - tempChild = tempChild.nextSibling; - } + let childCount = 0; + let tempChild = instance.firstChild; + while (tempChild !== null) { + childCount++; + tempChild = tempChild.nextSibling; + } - let child = instance.firstChild; - let currentChildIndex = 0; + let child = instance.firstChild; + let currentChildIndex = 0; - while (child !== null) { - currentChildIndex++; - const isLastSibling = currentChildIndex === childCount; - buildTreeString(child, childPrefix, isLastSibling); - child = child.nextSibling; - } - } - - const rootInstances: Array = []; - idToDevToolsInstanceMap.forEach(instance => { - if (instance.parent === null || instance.parent.parent === null) { - rootInstances.push(instance); - } - }); - - if (rootInstances.length > 0) { - for (let i = 0; i < rootInstances.length; i++) { - const isLast = i === rootInstances.length - 1; - buildTreeString(rootInstances[i], '', isLast); - if (!isLast) { - treeString += '\n'; + while (child !== null) { + currentChildIndex++; + const isLastSibling = currentChildIndex === childCount; + buildTreeString(child, childPrefix, isLastSibling); + child = child.nextSibling; } } - } else { - treeString = 'No component tree found.'; + + const rootInstances: Array = []; + idToDevToolsInstanceMap.forEach(instance => { + if (instance.parent === null || instance.parent.parent === null) { + rootInstances.push(instance); + } + }); + + if (rootInstances.length > 0) { + for (let i = 0; i < rootInstances.length; i++) { + const isLast = i === rootInstances.length - 1; + buildTreeString(rootInstances[i], '', isLast); + if (!isLast) { + treeString += '\n'; + } + } + } else { + treeString = 'No component tree found.'; + } + + return treeString; } - return treeString; + internalMcpFunctions.__internal_only_getComponentTree = + __internal_only_getComponentTree; } return { @@ -5942,7 +5948,6 @@ export function attach( getNearestMountedDOMNode, getElementIDForHostInstance, getInstanceAndStyle, - ...(__IS_INTERNAL_MCP_BUILD__ && {__internal_only_getComponentTree}), getOwnersList, getPathForElement, getProfilingData, @@ -5968,5 +5973,6 @@ export function attach( storeAsGlobal, updateComponentFilters, getEnvironmentNames, + ...internalMcpFunctions, }; }