Skip to content

Commit

Permalink
fix[react-devtools]: restore original args when recording errors
Browse files Browse the repository at this point in the history
  • Loading branch information
hoxyq committed Jun 25, 2024
1 parent f5d2feb commit 160aea7
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions packages/react-devtools-shared/src/backend/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const STYLE_DIRECTIVE_REGEX = /^%c/;
// method has been overridden by the patchForStrictMode function.
// If it has we'll need to do some special formatting of the arguments
// so the console color stays consistent
function isStrictModeOverride(args: Array<string>): boolean {
function isStrictModeOverride(args: Array<any>): boolean {
if (__IS_FIREFOX__) {
return (
args.length >= 2 &&
Expand All @@ -63,6 +63,21 @@ function isStrictModeOverride(args: Array<string>): boolean {
}
}

function potentiallyRestoreOriginalArgs(args: Array<any>): Array<any> {
// If the arguments don't have any styles applied, then just copy
if (!isStrictModeOverride(args)) {
return args.slice();
}

if (__IS_FIREFOX__) {
// Filter out %c from the start of the first argument and color as a second argument
return [args[0].slice(2)].concat(args.slice(2));
} else {
// Filter out the `\x1b...%s\x1b` template
return args.slice(1);
}
}

type OnErrorOrWarning = (
fiber: Fiber,
type: 'error' | 'warn',
Expand Down Expand Up @@ -220,8 +235,8 @@ export function patch({
onErrorOrWarning(
current,
((method: any): 'error' | 'warn'),
// Copy args before we mutate them (e.g. adding the component stack)
args.slice(),
// Potentially restore and copy args before we mutate them (e.g. adding the component stack)
potentiallyRestoreOriginalArgs(args),
);
}
}
Expand Down

0 comments on commit 160aea7

Please sign in to comment.