Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -484,4 +484,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?',
);
}
});
});