Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix[ReactDebugHooks/find-primitive-index]: remove some assumptions #29652

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions packages/react-debug-tools/src/ReactDebugHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,12 @@ function findCommonAncestorIndex(rootStack: any, hookStack: any) {
}

function isReactWrapper(functionName: any, wrapperName: string) {
return parseHookName(functionName) === wrapperName;
const hookName = parseHookName(functionName);
if (wrapperName === 'HostTransitionStatus') {
return hookName === wrapperName || hookName === 'FormStatus';
}

return hookName === wrapperName;
}

function findPrimitiveIndex(hookStack: any, hook: HookLogEntry) {
Expand All @@ -878,21 +883,24 @@ function findPrimitiveIndex(hookStack: any, hook: HookLogEntry) {
return -1;
}
for (let i = 0; i < primitiveStack.length && i < hookStack.length; i++) {
// Note: there is no guarantee that we will find the top-most primitive frame in the stack
// For React Native (uses Hermes), these source fields will be identical and skipped
if (primitiveStack[i].source !== hookStack[i].source) {
// If the next frame is a method from the dispatcher, we
// assume that the next frame after that is the actual public API call.
// This prohibits nesting dispatcher calls in hooks.
// If the next two frames are functions called `useX` then we assume that they're part of the
// wrappers that the React package or other packages adds around the dispatcher.
if (
i < hookStack.length - 1 &&
isReactWrapper(hookStack[i].functionName, hook.dispatcherHookName)
) {
i++;
}
if (
i < hookStack.length - 1 &&
isReactWrapper(hookStack[i].functionName, hook.dispatcherHookName)
) {
i++;
// Guard against the dispatcher call being inlined.
// At this point we wouldn't be able to recover the actual React Hook name.
if (i < hookStack.length - 1) {
i++;
}
}

return i;
}
}
Expand Down Expand Up @@ -1040,7 +1048,7 @@ function buildTree(
const levelChild: HooksNode = {
id,
isStateEditable,
name: name,
name,
value: hook.value,
subHooks: [],
debugInfo: debugInfo,
Expand Down