Skip to content

Commit

Permalink
Issue#11510: added verification check for misspelled propTypes (#11524)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Zubair Ahmed authored and gaearon committed Nov 24, 2017
1 parent 46dd197 commit 7d27851
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
13 changes: 12 additions & 1 deletion packages/react/src/ReactElementValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import ReactDebugCurrentFrame from './ReactDebugCurrentFrame';
if (__DEV__) {
var currentlyValidatingElement = null;

var propTypesMisspellWarningShown = false;

var getDisplayName = function(element): string {
if (element == null) {
return '#empty';
Expand Down Expand Up @@ -212,11 +214,20 @@ function validatePropTypes(element) {
}
var name = componentClass.displayName || componentClass.name;
var propTypes = componentClass.propTypes;

if (propTypes) {
currentlyValidatingElement = element;
checkPropTypes(propTypes, element.props, 'prop', name, getStackAddendum);
currentlyValidatingElement = null;
} else if (
componentClass.PropTypes !== undefined &&
!propTypesMisspellWarningShown
) {
propTypesMisspellWarningShown = true;
warning(
false,
'Component %s declared `PropTypes` instead of `propTypes`. Did you misspell the property assignment?',
name || 'Unknown',
);
}
if (typeof componentClass.getDefaultProps === 'function') {
warning(
Expand Down
23 changes: 23 additions & 0 deletions packages/react/src/__tests__/ReactElementValidator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,29 @@ describe('ReactElementValidator', () => {
}
});

it('should warn if component declares PropTypes instead of propTypes', () => {
spyOn(console, 'error');
class MisspelledPropTypesComponent extends React.Component {
static PropTypes = {
prop: PropTypes.string,
};
render() {
return React.createElement('span', null, this.props.prop);
}
}

ReactTestUtils.renderIntoDocument(
React.createElement(MisspelledPropTypesComponent, {prop: 'Hi'}),
);
if (__DEV__) {
expect(console.error.calls.count()).toBe(1);
expect(console.error.calls.argsFor(0)[0]).toBe(
'Warning: Component MisspelledPropTypesComponent declared `PropTypes` ' +
'instead of `propTypes`. Did you misspell the property assignment?',
);
}
});

it('should warn when accessing .type on an element factory', () => {
spyOnDev(console, 'warn');
function TestComponent() {
Expand Down
22 changes: 22 additions & 0 deletions packages/react/src/__tests__/ReactJSXElementValidator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,4 +402,26 @@ describe('ReactJSXElementValidator', () => {
);
}
});

it('should warn if component declares PropTypes instead of propTypes', () => {
spyOn(console, 'error');
class MisspelledPropTypesComponent extends React.Component {
render() {
return <span>{this.props.prop}</span>;
}
}
MisspelledPropTypesComponent.PropTypes = {
prop: PropTypes.string,
};
ReactTestUtils.renderIntoDocument(
<MisspelledPropTypesComponent prop="hi" />,
);
if (__DEV__) {
expect(console.error.calls.count()).toBe(1);
expect(console.error.calls.argsFor(0)[0]).toBe(
'Warning: Component MisspelledPropTypesComponent declared `PropTypes` ' +
'instead of `propTypes`. Did you misspell the property assignment?',
);
}
});
});

0 comments on commit 7d27851

Please sign in to comment.