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

Fixes #6153 Add case typechecking to provided props against propTypes #6938

Closed
wants to merge 3 commits 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
1 change: 1 addition & 0 deletions scripts/fiber/tests-passing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js
* should not warn for valid values
* should be implicitly optional and not warn without values
* should warn for missing required values
* should warn for mismatched case of passed values to types
* should should accept any value
* should be implicitly optional and not warn without values
* should warn for missing required values
Expand Down
22 changes: 22 additions & 0 deletions src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,28 @@ describe('ReactPropTypes', () => {
it('should warn for missing required values', () => {
typeCheckFailRequiredValues(PropTypes.string.isRequired);
});

it('should warn for mismatched case of passed values to types', function() {
spyOn(console, 'error');
Component = React.createClass({
propTypes: {
onAction: PropTypes.func,
label: PropTypes.string,
},

render: function() {
return <div>{this.props.label}</div>;
},
});
var node = <Component onaction={() => alert('hello')} label="HelloWorld" />;
var node2 = <Component onaction={() => alert('hello')} label="HelloWorld" />;
ReactTestUtils.renderIntoDocument(node);
ReactTestUtils.renderIntoDocument(node2);
expect(console.error.calls.argsFor(0)[0]).toBe(
'Warning: Provided prop onaction is not defined in propTypes, did you mean onAction?',
);
expect(console.error.calls.count()).toBe(1);
});
});

describe('Any type', () => {
Expand Down
14 changes: 14 additions & 0 deletions src/shared/types/checkReactTypeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ if (
}

var loggedTypeFailures = {};
var mistypedPropFailures = {};

/**
* Assert that the values match with the type specs.
Expand All @@ -57,6 +58,9 @@ function checkReactTypeSpec(
// only during reconciliation (begin and complete phase).
workInProgressOrDebugID,
) {
// values can be undefined, at least in pre existing test cases, so default to empty array
const mismatched = values ? Object.keys(values).filter(x => Object.keys(typeSpecs).indexOf(x) === -1) : [];
const lowercase = mismatched.map(i => i.toLowerCase());
for (var typeSpecName in typeSpecs) {
if (typeSpecs.hasOwnProperty(typeSpecName)) {
var error;
Expand Down Expand Up @@ -126,6 +130,16 @@ function checkReactTypeSpec(
componentStackInfo
);
}
if (lowercase.indexOf(typeSpecName.toLowerCase()) !== -1 && !mistypedPropFailures[typeSpecName]) {
const mismatch = mismatched[lowercase.indexOf(typeSpecName.toLowerCase())];
warning(
false,
'Provided prop %s is not defined in propTypes, did you mean %s?',
mismatch,
typeSpecName,
);
mistypedPropFailures[typeSpecName] = true;
}
}
}
}
Expand Down