MCPcopy
hub / github.com/facebook/react / buildTree

Function buildTree

packages/react-debug-tools/src/ReactDebugHooks.js:996–1131  ·  packages/react-debug-tools/src/ReactDebugHooks.js::buildTree
(
  rootStack: ParsedStackFrame[],
  readHookLog: Array<HookLogEntry>,
)

Source from the content-addressed store, hash-verified

994}
995
996function buildTree(
997 rootStack: ParsedStackFrame[],
998 readHookLog: Array<HookLogEntry>,
999): HooksTree {
1000 const rootChildren: Array<HooksNode> = [];
1001 let prevStack = null;
1002 let levelChildren = rootChildren;
1003 let nativeHookID = 0;
1004 const stackOfChildren = [];
1005 for (let i = 0; i < readHookLog.length; i++) {
1006 const hook = readHookLog[i];
1007 const parseResult = parseTrimmedStack(rootStack, hook);
1008 const primitiveFrame = parseResult[0];
1009 const stack = parseResult[1];
1010 let displayName = hook.displayName;
1011 if (displayName === null && primitiveFrame !== null) {
1012 displayName =
1013 parseHookName(primitiveFrame.functionName) ||
1014 class="cm">// Older versions of React do not have sourcemaps.
1015 class="cm">// In those versions there was always a 1:1 mapping between wrapper and dispatcher method.
1016 parseHookName(hook.dispatcherHookName);
1017 }
1018 if (stack !== null) {
1019 class="cm">// Note: The indices 0 <= n < length-1 will contain the names.
1020 class="cm">// The indices 1 <= n < length will contain the source locations.
1021 class="cm">// Thatclass="st">'s why we get the name from n - 1 and don't check the source
1022 class="cm">// of index 0.
1023 let commonSteps = 0;
1024 if (prevStack !== null) {
1025 class="cm">// Compare the current level's stack to the new stack.
1026 while (commonSteps < stack.length && commonSteps < prevStack.length) {
1027 const stackSource = stack[stack.length - commonSteps - 1].source;
1028 const prevSource =
1029 prevStack[prevStack.length - commonSteps - 1].source;
1030 if (stackSource !== prevSource) {
1031 break;
1032 }
1033 commonSteps++;
1034 }
1035 class="cm">// Pop back the stack as many steps as were not common.
1036 for (let j = prevStack.length - 1; j > commonSteps; j--) {
1037 class="cm">// $FlowFixMe[incompatible-type]
1038 levelChildren = stackOfChildren.pop();
1039 }
1040 }
1041 class="cm">// The remaining part of the new stack are custom hooks. Push them
1042 class="cm">// to the tree.
1043 for (let j = stack.length - commonSteps - 1; j >= 1; j--) {
1044 const children: Array<HooksNode> = [];
1045 const stackFrame = stack[j];
1046 const levelChild: HooksNode = {
1047 id: null,
1048 isStateEditable: false,
1049 name: parseHookName(stack[j - 1].functionName),
1050 value: undefined,
1051 subHooks: children,
1052 debugInfo: null,
1053 hookSource: {

Callers 2

inspectHooksFunction · 0.85
inspectHooksOfForwardRefFunction · 0.85

Calls 5

parseTrimmedStackFunction · 0.85
parseHookNameFunction · 0.85
processDebugValuesFunction · 0.85
popMethod · 0.65
pushMethod · 0.65

Tested by

no test coverage detected