Skip to content

Commit

Permalink
Fix parsing for minified prod bundles
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Apr 3, 2024
1 parent 62d0cba commit 9688f14
Showing 1 changed file with 36 additions and 37 deletions.
73 changes: 36 additions & 37 deletions packages/react-debug-tools/src/ReactDebugHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type HookLogEntry = {
stackError: Error,
value: mixed,
debugInfo: ReactDebugInfo | null,
dispatcherMethodName: string,
dispatcherHookName: string,
};

let hookLog: Array<HookLogEntry> = [];
Expand Down Expand Up @@ -210,7 +210,7 @@ function use<T>(usable: Usable<T>): T {
value: fulfilledValue,
debugInfo:
thenable._debugInfo === undefined ? null : thenable._debugInfo,
dispatcherMethodName: 'use',
dispatcherHookName: 'Use',
});
return fulfilledValue;
}
Expand All @@ -228,7 +228,7 @@ function use<T>(usable: Usable<T>): T {
value: thenable,
debugInfo:
thenable._debugInfo === undefined ? null : thenable._debugInfo,
dispatcherMethodName: 'use',
dispatcherHookName: 'Use',
});
throw SuspenseException;
} else if (usable.$$typeof === REACT_CONTEXT_TYPE) {
Expand All @@ -241,7 +241,7 @@ function use<T>(usable: Usable<T>): T {
stackError: new Error(),
value,
debugInfo: null,
dispatcherMethodName: 'use',
dispatcherHookName: 'Use',
});

return value;
Expand All @@ -260,7 +260,7 @@ function useContext<T>(context: ReactContext<T>): T {
stackError: new Error(),
value: value,
debugInfo: null,
dispatcherMethodName: 'useContext',
dispatcherHookName: 'Context',
});
return value;
}
Expand All @@ -282,7 +282,7 @@ function useState<S>(
stackError: new Error(),
value: state,
debugInfo: null,
dispatcherMethodName: 'useState',
dispatcherHookName: 'State',
});
return [state, (action: BasicStateAction<S>) => {}];
}
Expand All @@ -305,7 +305,7 @@ function useReducer<S, I, A>(
stackError: new Error(),
value: state,
debugInfo: null,
dispatcherMethodName: 'useReducer',
dispatcherHookName: 'Reducer',
});
return [state, (action: A) => {}];
}
Expand All @@ -319,7 +319,7 @@ function useRef<T>(initialValue: T): {current: T} {
stackError: new Error(),
value: ref.current,
debugInfo: null,
dispatcherMethodName: 'useRef',
dispatcherHookName: 'Ref',
});
return ref;
}
Expand All @@ -332,7 +332,7 @@ function useCacheRefresh(): () => void {
stackError: new Error(),
value: hook !== null ? hook.memoizedState : function refresh() {},
debugInfo: null,
dispatcherMethodName: 'useCacheRefresh',
dispatcherHookName: 'CacheRefresh',
});
return () => {};
}
Expand All @@ -348,7 +348,7 @@ function useLayoutEffect(
stackError: new Error(),
value: create,
debugInfo: null,
dispatcherMethodName: 'useLayoutEffect',
dispatcherHookName: 'LayoutEffect',
});
}

Expand All @@ -363,7 +363,7 @@ function useInsertionEffect(
stackError: new Error(),
value: create,
debugInfo: null,
dispatcherMethodName: 'useInsertionEffect',
dispatcherHookName: 'InsertionEffect',
});
}

Expand All @@ -378,7 +378,7 @@ function useEffect(
stackError: new Error(),
value: create,
debugInfo: null,
dispatcherMethodName: 'useEffect',
dispatcherHookName: 'Effect',
});
}

Expand All @@ -402,7 +402,7 @@ function useImperativeHandle<T>(
stackError: new Error(),
value: instance,
debugInfo: null,
dispatcherMethodName: 'useImperativeHandle',
dispatcherHookName: 'ImperativeHandle',
});
}

Expand All @@ -413,7 +413,7 @@ function useDebugValue(value: any, formatterFn: ?(value: any) => any) {
stackError: new Error(),
value: typeof formatterFn === 'function' ? formatterFn(value) : value,
debugInfo: null,
dispatcherMethodName: 'useDebugValue',
dispatcherHookName: 'DebugValue',
});
}

Expand All @@ -425,7 +425,7 @@ function useCallback<T>(callback: T, inputs: Array<mixed> | void | null): T {
stackError: new Error(),
value: hook !== null ? hook.memoizedState[0] : callback,
debugInfo: null,
dispatcherMethodName: 'useCallback',
dispatcherHookName: 'Callback',
});
return callback;
}
Expand All @@ -442,7 +442,7 @@ function useMemo<T>(
stackError: new Error(),
value,
debugInfo: null,
dispatcherMethodName: 'useMemo',
dispatcherHookName: 'Memo',
});
return value;
}
Expand All @@ -464,7 +464,7 @@ function useSyncExternalStore<T>(
stackError: new Error(),
value,
debugInfo: null,
dispatcherMethodName: 'useSyncExternalStore',
dispatcherHookName: 'SyncExternalStore',
});
return value;
}
Expand All @@ -487,7 +487,7 @@ function useTransition(): [
stackError: new Error(),
value: isPending,
debugInfo: null,
dispatcherMethodName: 'useTransition',
dispatcherHookName: 'Transition',
});
return [isPending, () => {}];
}
Expand All @@ -501,7 +501,7 @@ function useDeferredValue<T>(value: T, initialValue?: T): T {
stackError: new Error(),
value: prevValue,
debugInfo: null,
dispatcherMethodName: 'useDeferredValue',
dispatcherHookName: 'DeferredValue',
});
return prevValue;
}
Expand All @@ -515,7 +515,7 @@ function useId(): string {
stackError: new Error(),
value: id,
debugInfo: null,
dispatcherMethodName: 'useId',
dispatcherHookName: 'Id',
});
return id;
}
Expand Down Expand Up @@ -566,7 +566,7 @@ function useOptimistic<S, A>(
stackError: new Error(),
value: state,
debugInfo: null,
dispatcherMethodName: 'useOptimistic',
dispatcherHookName: 'Optimistic',
});
return [state, (action: A) => {}];
}
Expand Down Expand Up @@ -626,7 +626,7 @@ function useFormState<S, P>(
stackError: stackError,
value: value,
debugInfo: debugInfo,
dispatcherMethodName: 'useFormState',
dispatcherHookName: 'FormState',
});

if (error !== null) {
Expand Down Expand Up @@ -696,7 +696,7 @@ function useActionState<S, P>(
stackError: stackError,
value: value,
debugInfo: debugInfo,
dispatcherMethodName: 'useActionState',
dispatcherHookName: 'ActionState',
});

if (error !== null) {
Expand Down Expand Up @@ -835,16 +835,7 @@ function findCommonAncestorIndex(rootStack: any, hookStack: any) {
}

function isReactWrapper(functionName: any, wrapperName: string) {
if (!functionName) {
return false;
}
if (functionName.length < wrapperName.length) {
return false;
}
return (
functionName.lastIndexOf(wrapperName) ===
functionName.length - wrapperName.length
);
return parseHookName(functionName) === wrapperName;
}

function findPrimitiveIndex(hookStack: any, hook: HookLogEntry) {
Expand All @@ -860,13 +851,13 @@ function findPrimitiveIndex(hookStack: any, hook: HookLogEntry) {
// This prohibits nesting dispatcher calls in hooks.
if (
i < hookStack.length - 1 &&
isReactWrapper(hookStack[i].functionName, hook.dispatcherMethodName)
isReactWrapper(hookStack[i].functionName, hook.dispatcherHookName)
) {
i++;
}
if (
i < hookStack.length - 1 &&
isReactWrapper(hookStack[i].functionName, hook.dispatcherMethodName)
isReactWrapper(hookStack[i].functionName, hook.dispatcherHookName)
) {
i++;
}
Expand Down Expand Up @@ -904,7 +895,15 @@ function parseHookName(functionName: void | string): string {
if (!functionName) {
return '';
}
let startIndex = functionName.lastIndexOf('.');
let startIndex = functionName.lastIndexOf('[as ');

if (startIndex !== -1) {
// Workaround for sourcemaps in Jest and Chrome.
// In `node --enable-source-maps`, we don't see "Object.useHostTransitionStatus [as useFormStatus]" but "Object.useFormStatus"
// "Object.useHostTransitionStatus [as useFormStatus]" -> "useFormStatus"
return parseHookName(functionName.slice(startIndex + '[as '.length, -1));
}
startIndex = functionName.lastIndexOf('.');
if (startIndex === -1) {
startIndex = 0;
} else {
Expand Down Expand Up @@ -939,7 +938,7 @@ function buildTree(
parseHookName(primitiveFrame.functionName) ||
// Older versions of React do not have sourcemaps.
// In those versions there was always a 1:1 mapping between wrapper and dispatcher method.
parseHookName(hook.dispatcherMethodName);
parseHookName(hook.dispatcherHookName);
}
if (stack !== null) {
// Note: The indices 0 <= n < length-1 will contain the names.
Expand Down

0 comments on commit 9688f14

Please sign in to comment.