-
Notifications
You must be signed in to change notification settings - Fork 47.2k
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
Warn if PropType function is called manually #7132
Warn if PropType function is called manually #7132
Conversation
) { | ||
componentName = componentName || ANONYMOUS; | ||
propFullName = propFullName || propName; | ||
if (!__DEV__) { | ||
if (secret !== ReactPropTypesSecret) { | ||
console.error( |
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 also check if (typeof console !== 'undefined')
'testComponent', | ||
'prop' | ||
); | ||
const expectedCount = __DEV__ ? 0 : 1; |
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 make an if
branch and do different assertions in if
and else
. This would read simpler IMO.
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.
Well if we're not doing any __DEV__
tests then it would just be a constant 0
right? No need for any kind of branching.
|
||
'use strict'; | ||
|
||
const ReactPropTypesSecret = '__REACT_PROP_TYPES_SECRET__' + Math.random(); |
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: .toString()
.
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.
👍 will do. Just curious, why? It'll be coerced to a string either way AFAIK.
Now that I think of it, why not warn in development instead? It would be much easier to catch that issue even if the warning is only printed once. Sorry about the churn 😄 So we could do the following:
Do you think this makes more sense? We’d still change production behavior eventually, but at least people would become aware during development that calling them manually is not OK—whether in development or in production. |
I agree, production-only errors might be frustrating since some users might not catch those until it's deployed to a staging environment (or even to actual production).
No worries at all! I'd rather get it done right than get it done quick 😄
That seems sensible and consistent with other warnings/errors in React. Since we'd be doing these warnings in development, I think it would make the most sense to warn once for every instance where they're calling PropTypes function manually. Since dev warnings aren't really expensive, I feel like that would be the most useful. All we'd need to do is create a cache in the Does that sound good @gaearon? |
I’m hesitant because I can’t imagine somebody calling them as one-offs. If somebody actually calls those functions manually, I imagine they might be a part of something like a form validator, in which case you’d see a warning on every keystroke. Do you see deduping based on |
That's actually what I was trying to recommend. Checking if a warning has been logged for By "instance" I meant every place where they're manually calling a PropType function, not every time the function is invoked. Sorry, my terminology was probably a little confusing. |
Well, technically, you can’t really know where it’s being called from. You only know what arguments are being passed. But seems like we agree on the proposal. |
Right, I don't mean to imply that the location in the code path is being considered at all. I mean that we cache it so that for each PropTypes function we only log out a warning once for any combination of
|
@gaearon I'd be happy to! I'll get a PR going right now |
* Warn if PropType function is called in production * Check if console is undefined before warning * Randomize value of ReactPropTypesSecret * Remove dev environment tests * Rename typeCheckPass to productionWarningCheck * Rename productionWarningCheck to expectWarningInProduction * Call toString on Math.random() * Rename test block for React type checks * Make sure warning isnt emitted for failing props * Cache warning by component and prop, warn in dev * Pass ReactPropTypesSecret to internal checks * Move tests to ReactPropTypes-test.js * Update the warning message to include link * Do not test warning for unions with invalid args
* Warn if PropType function is called in production * Check if console is undefined before warning * Randomize value of ReactPropTypesSecret * Remove dev environment tests * Rename typeCheckPass to productionWarningCheck * Rename productionWarningCheck to expectWarningInProduction * Call toString on Math.random() * Rename test block for React type checks * Make sure warning isnt emitted for failing props * Cache warning by component and prop, warn in dev * Pass ReactPropTypesSecret to internal checks * Move tests to ReactPropTypes-test.js * Update the warning message to include link * Do not test warning for unions with invalid args (cherry picked from commit 95ac239)
@spicyj but there were plenty of other great possibilities! like |
This reverts commit e75e8dc.
This reverts commit e75e8dc.
…k#7132)"" This reverts commit be71f76. In other words, now we again have the warning if you attempt to call PropTypes manually. It was removed in facebook#8066 but we shouldn't have done this since we still want to avoid people accidentally calling them in production (and even more so since now it would throw). Fixes facebook#8080.
…k#7132)"" This reverts commit be71f76. In other words, now we again have the warning if you attempt to call PropTypes manually. It was removed in facebook#8066 but we shouldn't have done this since we still want to avoid people accidentally calling them in production (and even more so since now it would throw). Fixes facebook#8080.
* Revert "Revert "Warn if PropType function is called manually (#7132)"" This reverts commit be71f76. In other words, now we again have the warning if you attempt to call PropTypes manually. It was removed in #8066 but we shouldn't have done this since we still want to avoid people accidentally calling them in production (and even more so since now it would throw). Fixes #8080. * Pass secret in ReactControlledValuePropTypes * Record tests
* Strip PropTypes in production build * Revert "Warn if PropType function is called manually (facebook#7132)" This reverts commit e75e8dc.
Resolves #7131
@gaearon so I added a new file
ReactPropTypesSecret
that is used internally to verify that a propType function is not being called manually. For the tests I basically reused what you had in #6401 as far as structure goes. Everything should be passing.I wasn't sure if we wanted to dedupe this errors. If so I'm thinking we'd have to track it by
componentName
or something. Let me know what you think.