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[react-devtools]: restore original args when recording errors #30091

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
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 restorePotentiallyModifiedArgs(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(),
// Restore and copy args before we mutate them (e.g. adding the component stack)
restorePotentiallyModifiedArgs(args),
);
}
}
Expand Down