Skip to content

Commit 9688f14

Browse files
committed
Fix parsing for minified prod bundles
1 parent 62d0cba commit 9688f14

File tree

1 file changed

+36
-37
lines changed

1 file changed

+36
-37
lines changed

packages/react-debug-tools/src/ReactDebugHooks.js

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type HookLogEntry = {
4747
stackError: Error,
4848
value: mixed,
4949
debugInfo: ReactDebugInfo | null,
50-
dispatcherMethodName: string,
50+
dispatcherHookName: string,
5151
};
5252

5353
let hookLog: Array<HookLogEntry> = [];
@@ -210,7 +210,7 @@ function use<T>(usable: Usable<T>): T {
210210
value: fulfilledValue,
211211
debugInfo:
212212
thenable._debugInfo === undefined ? null : thenable._debugInfo,
213-
dispatcherMethodName: 'use',
213+
dispatcherHookName: 'Use',
214214
});
215215
return fulfilledValue;
216216
}
@@ -228,7 +228,7 @@ function use<T>(usable: Usable<T>): T {
228228
value: thenable,
229229
debugInfo:
230230
thenable._debugInfo === undefined ? null : thenable._debugInfo,
231-
dispatcherMethodName: 'use',
231+
dispatcherHookName: 'Use',
232232
});
233233
throw SuspenseException;
234234
} else if (usable.$$typeof === REACT_CONTEXT_TYPE) {
@@ -241,7 +241,7 @@ function use<T>(usable: Usable<T>): T {
241241
stackError: new Error(),
242242
value,
243243
debugInfo: null,
244-
dispatcherMethodName: 'use',
244+
dispatcherHookName: 'Use',
245245
});
246246

247247
return value;
@@ -260,7 +260,7 @@ function useContext<T>(context: ReactContext<T>): T {
260260
stackError: new Error(),
261261
value: value,
262262
debugInfo: null,
263-
dispatcherMethodName: 'useContext',
263+
dispatcherHookName: 'Context',
264264
});
265265
return value;
266266
}
@@ -282,7 +282,7 @@ function useState<S>(
282282
stackError: new Error(),
283283
value: state,
284284
debugInfo: null,
285-
dispatcherMethodName: 'useState',
285+
dispatcherHookName: 'State',
286286
});
287287
return [state, (action: BasicStateAction<S>) => {}];
288288
}
@@ -305,7 +305,7 @@ function useReducer<S, I, A>(
305305
stackError: new Error(),
306306
value: state,
307307
debugInfo: null,
308-
dispatcherMethodName: 'useReducer',
308+
dispatcherHookName: 'Reducer',
309309
});
310310
return [state, (action: A) => {}];
311311
}
@@ -319,7 +319,7 @@ function useRef<T>(initialValue: T): {current: T} {
319319
stackError: new Error(),
320320
value: ref.current,
321321
debugInfo: null,
322-
dispatcherMethodName: 'useRef',
322+
dispatcherHookName: 'Ref',
323323
});
324324
return ref;
325325
}
@@ -332,7 +332,7 @@ function useCacheRefresh(): () => void {
332332
stackError: new Error(),
333333
value: hook !== null ? hook.memoizedState : function refresh() {},
334334
debugInfo: null,
335-
dispatcherMethodName: 'useCacheRefresh',
335+
dispatcherHookName: 'CacheRefresh',
336336
});
337337
return () => {};
338338
}
@@ -348,7 +348,7 @@ function useLayoutEffect(
348348
stackError: new Error(),
349349
value: create,
350350
debugInfo: null,
351-
dispatcherMethodName: 'useLayoutEffect',
351+
dispatcherHookName: 'LayoutEffect',
352352
});
353353
}
354354

@@ -363,7 +363,7 @@ function useInsertionEffect(
363363
stackError: new Error(),
364364
value: create,
365365
debugInfo: null,
366-
dispatcherMethodName: 'useInsertionEffect',
366+
dispatcherHookName: 'InsertionEffect',
367367
});
368368
}
369369

@@ -378,7 +378,7 @@ function useEffect(
378378
stackError: new Error(),
379379
value: create,
380380
debugInfo: null,
381-
dispatcherMethodName: 'useEffect',
381+
dispatcherHookName: 'Effect',
382382
});
383383
}
384384

@@ -402,7 +402,7 @@ function useImperativeHandle<T>(
402402
stackError: new Error(),
403403
value: instance,
404404
debugInfo: null,
405-
dispatcherMethodName: 'useImperativeHandle',
405+
dispatcherHookName: 'ImperativeHandle',
406406
});
407407
}
408408

@@ -413,7 +413,7 @@ function useDebugValue(value: any, formatterFn: ?(value: any) => any) {
413413
stackError: new Error(),
414414
value: typeof formatterFn === 'function' ? formatterFn(value) : value,
415415
debugInfo: null,
416-
dispatcherMethodName: 'useDebugValue',
416+
dispatcherHookName: 'DebugValue',
417417
});
418418
}
419419

@@ -425,7 +425,7 @@ function useCallback<T>(callback: T, inputs: Array<mixed> | void | null): T {
425425
stackError: new Error(),
426426
value: hook !== null ? hook.memoizedState[0] : callback,
427427
debugInfo: null,
428-
dispatcherMethodName: 'useCallback',
428+
dispatcherHookName: 'Callback',
429429
});
430430
return callback;
431431
}
@@ -442,7 +442,7 @@ function useMemo<T>(
442442
stackError: new Error(),
443443
value,
444444
debugInfo: null,
445-
dispatcherMethodName: 'useMemo',
445+
dispatcherHookName: 'Memo',
446446
});
447447
return value;
448448
}
@@ -464,7 +464,7 @@ function useSyncExternalStore<T>(
464464
stackError: new Error(),
465465
value,
466466
debugInfo: null,
467-
dispatcherMethodName: 'useSyncExternalStore',
467+
dispatcherHookName: 'SyncExternalStore',
468468
});
469469
return value;
470470
}
@@ -487,7 +487,7 @@ function useTransition(): [
487487
stackError: new Error(),
488488
value: isPending,
489489
debugInfo: null,
490-
dispatcherMethodName: 'useTransition',
490+
dispatcherHookName: 'Transition',
491491
});
492492
return [isPending, () => {}];
493493
}
@@ -501,7 +501,7 @@ function useDeferredValue<T>(value: T, initialValue?: T): T {
501501
stackError: new Error(),
502502
value: prevValue,
503503
debugInfo: null,
504-
dispatcherMethodName: 'useDeferredValue',
504+
dispatcherHookName: 'DeferredValue',
505505
});
506506
return prevValue;
507507
}
@@ -515,7 +515,7 @@ function useId(): string {
515515
stackError: new Error(),
516516
value: id,
517517
debugInfo: null,
518-
dispatcherMethodName: 'useId',
518+
dispatcherHookName: 'Id',
519519
});
520520
return id;
521521
}
@@ -566,7 +566,7 @@ function useOptimistic<S, A>(
566566
stackError: new Error(),
567567
value: state,
568568
debugInfo: null,
569-
dispatcherMethodName: 'useOptimistic',
569+
dispatcherHookName: 'Optimistic',
570570
});
571571
return [state, (action: A) => {}];
572572
}
@@ -626,7 +626,7 @@ function useFormState<S, P>(
626626
stackError: stackError,
627627
value: value,
628628
debugInfo: debugInfo,
629-
dispatcherMethodName: 'useFormState',
629+
dispatcherHookName: 'FormState',
630630
});
631631

632632
if (error !== null) {
@@ -696,7 +696,7 @@ function useActionState<S, P>(
696696
stackError: stackError,
697697
value: value,
698698
debugInfo: debugInfo,
699-
dispatcherMethodName: 'useActionState',
699+
dispatcherHookName: 'ActionState',
700700
});
701701

702702
if (error !== null) {
@@ -835,16 +835,7 @@ function findCommonAncestorIndex(rootStack: any, hookStack: any) {
835835
}
836836

837837
function isReactWrapper(functionName: any, wrapperName: string) {
838-
if (!functionName) {
839-
return false;
840-
}
841-
if (functionName.length < wrapperName.length) {
842-
return false;
843-
}
844-
return (
845-
functionName.lastIndexOf(wrapperName) ===
846-
functionName.length - wrapperName.length
847-
);
838+
return parseHookName(functionName) === wrapperName;
848839
}
849840

850841
function findPrimitiveIndex(hookStack: any, hook: HookLogEntry) {
@@ -860,13 +851,13 @@ function findPrimitiveIndex(hookStack: any, hook: HookLogEntry) {
860851
// This prohibits nesting dispatcher calls in hooks.
861852
if (
862853
i < hookStack.length - 1 &&
863-
isReactWrapper(hookStack[i].functionName, hook.dispatcherMethodName)
854+
isReactWrapper(hookStack[i].functionName, hook.dispatcherHookName)
864855
) {
865856
i++;
866857
}
867858
if (
868859
i < hookStack.length - 1 &&
869-
isReactWrapper(hookStack[i].functionName, hook.dispatcherMethodName)
860+
isReactWrapper(hookStack[i].functionName, hook.dispatcherHookName)
870861
) {
871862
i++;
872863
}
@@ -904,7 +895,15 @@ function parseHookName(functionName: void | string): string {
904895
if (!functionName) {
905896
return '';
906897
}
907-
let startIndex = functionName.lastIndexOf('.');
898+
let startIndex = functionName.lastIndexOf('[as ');
899+
900+
if (startIndex !== -1) {
901+
// Workaround for sourcemaps in Jest and Chrome.
902+
// In `node --enable-source-maps`, we don't see "Object.useHostTransitionStatus [as useFormStatus]" but "Object.useFormStatus"
903+
// "Object.useHostTransitionStatus [as useFormStatus]" -> "useFormStatus"
904+
return parseHookName(functionName.slice(startIndex + '[as '.length, -1));
905+
}
906+
startIndex = functionName.lastIndexOf('.');
908907
if (startIndex === -1) {
909908
startIndex = 0;
910909
} else {
@@ -939,7 +938,7 @@ function buildTree(
939938
parseHookName(primitiveFrame.functionName) ||
940939
// Older versions of React do not have sourcemaps.
941940
// In those versions there was always a 1:1 mapping between wrapper and dispatcher method.
942-
parseHookName(hook.dispatcherMethodName);
941+
parseHookName(hook.dispatcherHookName);
943942
}
944943
if (stack !== null) {
945944
// Note: The indices 0 <= n < length-1 will contain the names.

0 commit comments

Comments
 (0)