-
Notifications
You must be signed in to change notification settings - Fork 50.2k
Ensure arguments are coerced to strings in warnings #13385
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
Ensure arguments are coerced to strings in warnings #13385
Conversation
This prevents a bug where Chrome reports `Array(n)` where `n` is the size of the array.
|
Run Prettier? |
|
Hmm. Should we disallow non-string/number args to warning? |
Oops. Done.
I kind of think so, ya. I wonder if we could just set this with Flow and fix what fails. |
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.
Let's not fix this here.
Instead, change warningWithoutStack to coerce all substitutions to string. That's how it worked earlier when formatting was done inside the warning implementation .
|
|
||
| let argIndex = 0; | ||
| const message = | ||
| 'Warning: ' + format.replace(/%s/g, () => `${args[argIndex++]}`); |
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.
Hmmm. 'Warning: ' + is redundant with ${ args }. Fixing real quick.
| } | ||
|
|
||
| let argIndex = 0; | ||
| const message = 'Warning: ' + format.replace(/%s/g, () => args[argIndex++]); |
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.
@gaearon Ended up just moving this line up a bit so the string replacement is consistent between the two blocks. Feel okay?
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.
Sorry, that was an intentional change. See my collapsed comment below (silly GitHub)
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.
Okay. No worries :)
|
|
||
| if (typeof console !== 'undefined') { | ||
| console.error('Warning: ' + format, ...args); | ||
| console.error(message); |
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.
The part about passing ...args is intentional. It lets other tools read the stack separately.
I'm only suggesting to map over them and coerce them to strings.
| } | ||
| if (typeof console !== 'undefined') { | ||
| console.error('Warning: ' + format, ...args); | ||
| const strings = args.map(item => '' + item); |
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.
Nit: maybe stringArgs
|
Thanks for catching this before the release! |
|
the release? |
|
We’ll need to cut a patch soon |
I noticed this working on the Hydration fixture. It looks like we have a warning that depends on the
%sreplacement to stringify an array into a list of words. Instead it reportsArray(n):I use the 1Password browser extension, which adds some data attributes when you engage with a form.
Steps to reproduce
Using the 1Password browser extension (I've added some extra steps if you don't have this):
Expected
Warning: Extra attributes from the server: data-com.onepassword.iv, data-com.agilebits.onepassword.user-edited(Or whatever attributes you added)Actual:
Warning: Extra attributes from the server: Array(2)