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

createChainableTypeChecker suggests mistyped props by case #6173

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,30 @@ describe('ReactElementValidator', function() {
expect(console.error.calls.length).toBe(2);
});

it('should check for suggested props by case', function() {
spyOn(console, 'error');

var Component = React.createClass({
propTypes: {
onAction: React.PropTypes.func.isRequired,
},
render: function() {
return React.createElement('span', null, this.props.prop);
},
});

ReactTestUtils.renderIntoDocument(
React.createElement(Component, {onaction: function() {}})
);

expect(console.error.calls.length).toBe(1);
expect(console.error.argsForCall[0][0]).toBe(
'Warning: Failed propType: ' +
'Required prop `onAction` was not specified in `Component`. ' +
'Did you mistype `onaction`?'
);
});

it('should warn if a PropType creator is used as a PropType', function() {
spyOn(console, 'error');

Expand Down
11 changes: 7 additions & 4 deletions src/isomorphic/classic/types/ReactPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,13 @@ function createChainableTypeChecker(validate) {
if (props[propName] == null) {
var locationName = ReactPropTypeLocationNames[location];
if (isRequired) {
return new Error(
`Required ${locationName} \`${propFullName}\` was not specified in ` +
`\`${componentName}\`.`
);
var message = `Required ${locationName} \`${propFullName}\` was not specified in ` +
`\`${componentName}\`.`;
var lowerPropName = propName.toLowerCase();
if (props[lowerPropName] != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This typecheck seems wrong to me. To detect improper capitalization, I feel like you need to normalize both sides (eg. send both of them toLowerCase). I think this code as-is will only catch the case where the user happens to use all lowercase.

message += ` Did you mistype \`${lowerPropName}\`?`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's say "miscapitalize" instead of "mistype"; it's a little more clear.

}
return new Error(message);
}
return null;
} else {
Expand Down