-
Notifications
You must be signed in to change notification settings - Fork 47.7k
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
chore[react-devtools]: improve console arguments formatting before passing it to original console #29873
Merged
hoxyq
merged 1 commit into
facebook:main
from
hoxyq:react-devtools/better-formatting-for-ansi-escape-stylings
Jun 17, 2024
Merged
chore[react-devtools]: improve console arguments formatting before passing it to original console #29873
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -164,6 +164,7 @@ export function serializeToString(data: any): string { | |
); | ||
} | ||
|
||
// NOTE: KEEP IN SYNC with src/hook.js | ||
// Formats an array of args with a style for console methods, using | ||
// the following algorithm: | ||
// 1. The first param is a string that contains %c | ||
|
@@ -220,11 +221,72 @@ export function formatWithStyles( | |
} | ||
} | ||
|
||
// NOTE: KEEP IN SYNC with src/hook.js | ||
// Skips CSS and object arguments, inlines other in the first argument as a template string | ||
export function formatConsoleArguments( | ||
maybeMessage: any, | ||
...inputArgs: $ReadOnlyArray<any> | ||
): $ReadOnlyArray<any> { | ||
if (inputArgs.length === 0 || typeof maybeMessage !== 'string') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe it is worth checking here if "maybeMessage" is an ANSI template. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
return [maybeMessage, ...inputArgs]; | ||
} | ||
|
||
const args = inputArgs.slice(); | ||
|
||
let template = ''; | ||
let argumentsPointer = 0; | ||
for (let i = 0; i < maybeMessage.length; ++i) { | ||
const currentChar = maybeMessage[i]; | ||
if (currentChar !== '%') { | ||
template += currentChar; | ||
continue; | ||
} | ||
|
||
const nextChar = maybeMessage[i + 1]; | ||
++i; | ||
|
||
// Only keep CSS and objects, inline other arguments | ||
switch (nextChar) { | ||
case 'c': | ||
case 'O': | ||
case 'o': { | ||
++argumentsPointer; | ||
template += `%${nextChar}`; | ||
|
||
break; | ||
} | ||
case 'd': | ||
case 'i': { | ||
const [arg] = args.splice(argumentsPointer, 1); | ||
template += parseInt(arg, 10).toString(); | ||
|
||
break; | ||
} | ||
case 'f': { | ||
const [arg] = args.splice(argumentsPointer, 1); | ||
template += parseFloat(arg).toString(); | ||
|
||
break; | ||
} | ||
case 's': { | ||
const [arg] = args.splice(argumentsPointer, 1); | ||
template += arg.toString(); | ||
|
||
break; | ||
} | ||
|
||
default: | ||
template += `%${nextChar}`; | ||
} | ||
} | ||
|
||
return [template, ...args]; | ||
} | ||
|
||
// based on https://github.com/tmpfs/format-util/blob/0e62d430efb0a1c51448709abd3e2406c14d8401/format.js#L1 | ||
// based on https://developer.mozilla.org/en-US/docs/Web/API/console#Using_string_substitutions | ||
// Implements s, d, i and f placeholders | ||
// NOTE: KEEP IN SYNC with src/hook.js | ||
export function format( | ||
export function formatConsoleArgumentsToSingleString( | ||
maybeMessage: any, | ||
...inputArgs: $ReadOnlyArray<any> | ||
): string { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't really need this pragma here, because
format
has nothing to do with different versions of React, so there is no reason to test it against them.