Skip to content

Commit 4797026

Browse files
Zubair Ahmedraphamorim
authored andcommitted
Issue#11510: added verification check for misspelled propTypes (facebook#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
1 parent d410bd9 commit 4797026

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

packages/react/src/ReactElementValidator.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import ReactDebugCurrentFrame from './ReactDebugCurrentFrame';
2626
if (__DEV__) {
2727
var currentlyValidatingElement = null;
2828

29+
var propTypesMisspellWarningShown = false;
30+
2931
var getDisplayName = function(element): string {
3032
if (element == null) {
3133
return '#empty';
@@ -212,11 +214,20 @@ function validatePropTypes(element) {
212214
}
213215
var name = componentClass.displayName || componentClass.name;
214216
var propTypes = componentClass.propTypes;
215-
216217
if (propTypes) {
217218
currentlyValidatingElement = element;
218219
checkPropTypes(propTypes, element.props, 'prop', name, getStackAddendum);
219220
currentlyValidatingElement = null;
221+
} else if (
222+
componentClass.PropTypes !== undefined &&
223+
!propTypesMisspellWarningShown
224+
) {
225+
propTypesMisspellWarningShown = true;
226+
warning(
227+
false,
228+
'Component %s declared `PropTypes` instead of `propTypes`. Did you misspell the property assignment?',
229+
name || 'Unknown',
230+
);
220231
}
221232
if (typeof componentClass.getDefaultProps === 'function') {
222233
warning(

packages/react/src/__tests__/ReactElementValidator-test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,29 @@ describe('ReactElementValidator', () => {
448448
}
449449
});
450450

451+
it('should warn if component declares PropTypes instead of propTypes', () => {
452+
spyOn(console, 'error');
453+
class MisspelledPropTypesComponent extends React.Component {
454+
static PropTypes = {
455+
prop: PropTypes.string,
456+
};
457+
render() {
458+
return React.createElement('span', null, this.props.prop);
459+
}
460+
}
461+
462+
ReactTestUtils.renderIntoDocument(
463+
React.createElement(MisspelledPropTypesComponent, {prop: 'Hi'}),
464+
);
465+
if (__DEV__) {
466+
expect(console.error.calls.count()).toBe(1);
467+
expect(console.error.calls.argsFor(0)[0]).toBe(
468+
'Warning: Component MisspelledPropTypesComponent declared `PropTypes` ' +
469+
'instead of `propTypes`. Did you misspell the property assignment?',
470+
);
471+
}
472+
});
473+
451474
it('should warn when accessing .type on an element factory', () => {
452475
spyOnDev(console, 'warn');
453476
function TestComponent() {

packages/react/src/__tests__/ReactJSXElementValidator-test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,4 +402,26 @@ describe('ReactJSXElementValidator', () => {
402402
);
403403
}
404404
});
405+
406+
it('should warn if component declares PropTypes instead of propTypes', () => {
407+
spyOn(console, 'error');
408+
class MisspelledPropTypesComponent extends React.Component {
409+
render() {
410+
return <span>{this.props.prop}</span>;
411+
}
412+
}
413+
MisspelledPropTypesComponent.PropTypes = {
414+
prop: PropTypes.string,
415+
};
416+
ReactTestUtils.renderIntoDocument(
417+
<MisspelledPropTypesComponent prop="hi" />,
418+
);
419+
if (__DEV__) {
420+
expect(console.error.calls.count()).toBe(1);
421+
expect(console.error.calls.argsFor(0)[0]).toBe(
422+
'Warning: Component MisspelledPropTypesComponent declared `PropTypes` ' +
423+
'instead of `propTypes`. Did you misspell the property assignment?',
424+
);
425+
}
426+
});
405427
});

0 commit comments

Comments
 (0)