-
Notifications
You must be signed in to change notification settings - Fork 46.9k
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 for #5468: Validate proptype definitions sooner #6316
Conversation
@zpao do you want to provide feedback? This PR has been idle for three days, and we should provide a response. |
Thanks @jimfb ! |
Please note that in the future, if you see that another PR that fixes this issue already exists, it’s best to ask the original submitter whether they intend to finish their work before starting a new PR. In this case, the implementation in #5476 was waiting for review after a discussion, but we didn’t review it timely. I closed the old PR because this one is more up to date but I didn’t feel very good about this, and at least pinging the author a couple of times would be a good idea in retrospect. No hard feelings though, and thank you for contributing 😉 . Usually PRs are linked from the issues, and I can see a link from #5468 to #5476. It’s a good idea to look through such links before jumping into implementation. Cheers! |
Don’t worry about it! 😄 This happens a lot, and I personally did this mistake more than once. Some people take it harder so it’s usually best to try to avoid this but @rppc doesn’t mind so we’re all good here. Cheers! |
@gaearon Great, thanks! |
@@ -45,6 +45,13 @@ function typeCheckPass(declaration, value) { | |||
expect(error).toBe(null); | |||
} | |||
|
|||
function typeCheckWarn(propTypeFunc, 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.
This helper is a little bit confusing because it pretends to be similar to typeCheckPass
and typeCheckFail
but has a completely different purpose and signature. Let’s just write this code explicitly inline in the corresponding test cases. Additionally, we can remove typeCheckFail
calls from them because I think we only want the warning to appear once.
Added typeCheckWarn() func and updated the oneOf/oneOfType tests Added __DEV__ warning for invalid oneOf/OneOfType args
@gaearon how's this look? (btw, I was listening to a podcast this week that interviewed you (shortly before you came to Facebook) that was discussing your next thing, and I think I may have heard a knowing tone in your voice when you said you weren't sure yet. ;-) ) |
); | ||
}); | ||
if (__DEV__) { | ||
warning(false, 'Invalid argument supplied to oneOf, expected an instance of array.'); |
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 would still want to return a function for consistency—it would just be a no-op.
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 no-oped, or should I return false?
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 use emptyFunction.thatReturnsNull
(if I recall correctly)
@@ -229,6 +229,7 @@ function createEnumTypeChecker(expectedValues) { | |||
if (!Array.isArray(expectedValues)) { | |||
if (__DEV__) { | |||
warning(false, 'Invalid argument supplied to oneOf, expected an instance of array.'); | |||
return function() {}; |
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.
You can use emptyFunction
module for consistency.
warning(false, 'Invalid argument supplied to oneOf, expected an instance of array.'); | ||
return emptyFunction.thatReturnsNull; | ||
} else { | ||
return createChainableTypeChecker(function() { |
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.
For consistency with DEV behavior, let’s return emptyFunction.thatReturnsNull
here as well. This way we treat bad argument as early DEV warning, and since we can’t really check propTypes, we don’t.
Not that all these semantics really matter because nobody should be calling prop types in prod but it’s best to have them behave the same way until we explicitly disallow it.
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 oh, so don't call createChainableTypeChecker as an alternate branch flow, always call warning?
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 pushed my interpretation of your comments, thanks
'red', | ||
'Invalid argument supplied to oneOf, expected an instance of array.' | ||
); | ||
spyOn(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.
Can you change this to spyOn(console, 'error')
like the rest of the codebase does?
Thank you! |
Great, thanks! |
* Fix for 5468: Validate proptype definitions sooner Added typeCheckWarn() func and updated the oneOf/oneOfType tests Added __DEV__ warning for invalid oneOf/OneOfType args * Suppress redundant error on warn; typeCheckWarn() removed * Return no-op * Using emptyFunction module for consistency * Remove createChainableTypeChecker() call * Adjust test to assert type check passes when warned (cherry picked from commit 6cc037b)
Fix for #5468
Added
typeCheckWarn()
func and updated the oneOf/oneOfType testsAdded warning for invalid oneOf/OneOfType args
This is my first contribution to React, any feedback is appreciated. :)