-
Notifications
You must be signed in to change notification settings - Fork 47k
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
Issue#11510: added verification check for misspelled propTypes #11524
Conversation
solves #11510 |
@@ -221,7 +221,14 @@ function validatePropTypes(element) { | |||
} | |||
var name = componentClass.displayName || componentClass.name; | |||
var propTypes = componentClass.propTypes; | |||
|
|||
if (componentClass.PropTypes !== undefined) { |
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 should deduplicate this warning so it doesn't warn more than once. See how we do with most other warnings.
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.
I have added the boolean flag after observing other places where warning()
was used. Also tested locally
I hope this is the correct approach if not I will gladly change it. |
function validatePropTypes(element) { | ||
var componentClass = element.type; | ||
if (typeof componentClass !== 'function') { | ||
return; | ||
} | ||
var name = componentClass.displayName || componentClass.name; | ||
var propTypes = componentClass.propTypes; | ||
|
||
if ( |
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.
Maybe let's put it into an else
clause to the next if
?
if (propTypes) {
// ...
} else if (componentClass.PropTypes && !propTypesMisspellWarningShown) {
// ...
}
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.
I have also tested this in fixtures/dom
by introducing some props and misspelling propTypes
propTypesMisspellWarningShown = true; | ||
warning( | ||
false, | ||
'Static propTypes method should be accessed by `.propTypes = { }` instead of `.PropTypes = { }`', |
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 reword to say:
Component %s declared `PropTypes` instead of `propTypes`. Did you misspell the property assignment?
And pass the component name name || 'Unknown'
as an argument.
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.
changes done. Thank you @gaearon for being so patient with me.
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'll also need a test for this.
@@ -214,18 +214,28 @@ function validateChildKeys(node, parentType) { | |||
* | |||
* @param {ReactElement} element | |||
*/ | |||
var propTypesMisspellWarningShown; |
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 we move this up to other DEV only variables?
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.
Also let's initialise it to 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.
Added the variable declaration under if(__DEV__){
which is after import statements. Now working on test.
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.
Should the test be added in both ReactElementValidator-test
and ReactJSXElementValidator-test
?
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.
I think just one is fine.
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.
Oh i just added test in both of them, i pushed my code. Should i remove from one ?
Also i have verified all tests were passing and ran build in my local example which was reproducing the error.
I was wondering if i should add another test it(should not show any errors is correct propTypes property assignment is passed)
?
are the tests correct or should I change something? |
friendly ping! |
); | ||
|
||
expectDev(console.error.calls.count()).toBe(1); | ||
/*eslint-disable max-len */ |
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: instead of disabling, you can split the message into multiple lines and concatenate them. Other tests do this.
ok i removed eslint disable and added the split in string. |
as per recent changes in master i have added DEV check to my tests. I hope it is correct? |
This mostly looks good. I think we'll want to try this internally and see if it fires any unexpected warnings at FB. (e.g. I think I saw |
thanks do you want me to run a quick check on a react Native project? |
Would be nice. |
Hey @gaearon i had a chance to run the latest master build in a rn project: Steps
Does this test is good? |
…ook#11524) * added verification check for misspelled propTypes * added flag to check if misspelled warning was shown to developer before * added the condition to else if and improved the warning message * moved variable under dev section & initialized it to false * added test to confirm the missmatch prop type warning in both and tests files * removed eslint disable and split error into 2 lines * changed expectDev to expect in tests * added __DEV__ condition before both tests
…ook#11524) * added verification check for misspelled propTypes * added flag to check if misspelled warning was shown to developer before * added the condition to else if and improved the warning message * moved variable under dev section & initialized it to false * added test to confirm the missmatch prop type warning in both and tests files * removed eslint disable and split error into 2 lines * changed expectDev to expect in tests * added __DEV__ condition before both tests
…ook#11524) * added verification check for misspelled propTypes * added flag to check if misspelled warning was shown to developer before * added the condition to else if and improved the warning message * moved variable under dev section & initialized it to false * added test to confirm the missmatch prop type warning in both and tests files * removed eslint disable and split error into 2 lines * changed expectDev to expect in tests * added __DEV__ condition before both tests
Prerequisites
Changes
Added the check if
PropTypes
is entered instead ofpropTypes
in the static method of class for props types check.Please let me know if i have to change anything