Skip to content
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

Merged
merged 11 commits into from
Nov 24, 2017

Conversation

M-ZubairAhmed
Copy link

Prerequisites

  • All test ran successfully
  • Code formatted with prettier
  • Code linting done
  • Flow run completed
  • completed the CLA.

Changes

Added the check if PropTypes is entered instead of propTypes in the static method of class for props types check.
Please let me know if i have to change anything

@M-ZubairAhmed
Copy link
Author

solves #11510

@M-ZubairAhmed M-ZubairAhmed changed the title added verification check for misspelled propTypes Issue#11510: added verification check for misspelled propTypes Nov 11, 2017
@@ -221,7 +221,14 @@ function validatePropTypes(element) {
}
var name = componentClass.displayName || componentClass.name;
var propTypes = componentClass.propTypes;

if (componentClass.PropTypes !== undefined) {
Copy link
Collaborator

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.

Copy link
Author

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

@M-ZubairAhmed
Copy link
Author

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 (
Copy link
Collaborator

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) {
  // ...
}

Copy link
Author

@M-ZubairAhmed M-ZubairAhmed Nov 14, 2017

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 = { }`',
Copy link
Collaborator

@gaearon gaearon Nov 13, 2017

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.

Copy link
Author

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.

Copy link
Collaborator

@gaearon gaearon left a 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;
Copy link
Collaborator

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?

Copy link
Collaborator

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.

Copy link
Author

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.

Copy link
Author

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?

Copy link
Collaborator

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.

Copy link
Author

@M-ZubairAhmed M-ZubairAhmed Nov 14, 2017

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)?

@M-ZubairAhmed
Copy link
Author

are the tests correct or should I change something?

@M-ZubairAhmed
Copy link
Author

friendly ping!

);

expectDev(console.error.calls.count()).toBe(1);
/*eslint-disable max-len */
Copy link
Collaborator

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.

@M-ZubairAhmed
Copy link
Author

ok i removed eslint disable and added the split in string.

@M-ZubairAhmed
Copy link
Author

as per recent changes in master i have added DEV check to my tests. I hope it is correct?

@gaearon
Copy link
Collaborator

gaearon commented Nov 23, 2017

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 .PropTypes used intentionally on something renderable in RN. But maybe that was in the past.)

@gaearon gaearon merged commit 7d27851 into facebook:master Nov 24, 2017
@M-ZubairAhmed
Copy link
Author

thanks do you want me to run a quick check on a react Native project?

@M-ZubairAhmed M-ZubairAhmed deleted the misspelled-propTypes branch November 24, 2017 05:00
@gaearon
Copy link
Collaborator

gaearon commented Nov 24, 2017

Would be nice.

@M-ZubairAhmed
Copy link
Author

Hey @gaearon i had a chance to run the latest master build in a rn project:
There was no un usual error produced

Steps
  • Build latest from master branch
  • replaced the build version of react inside RN node modules
  • Added prop-types to RN project
  • kept track of console logs to check if anything showed up
  • intentionally passed PropTypes instead of propTypes
  • correct warning was shown as expected
  • reverted the propTypes declaration
  • no error was shown

Does this test is good?

HeroProtagonist pushed a commit to HeroProtagonist/react that referenced this pull request Nov 26, 2017
…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
raphamorim pushed a commit to raphamorim/react that referenced this pull request Nov 27, 2017
…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
Ethan-Arrowood pushed a commit to Ethan-Arrowood/react that referenced this pull request Dec 8, 2017
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants