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

[New] prop-types: add support for PropTypes.exact #2740

Merged
merged 1 commit into from Aug 13, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/util/propTypes.js
Expand Up @@ -422,13 +422,14 @@ module.exports = function propTypesInstructions(context, components, utils) {
const callName = value.callee.property.name;
const argument = value.arguments[0];
switch (callName) {
case 'shape': {
case 'shape':
case 'exact': {
Copy link
Member

Choose a reason for hiding this comment

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

i don't think this should be included here - i think it should be a default member of propWrapperFunctions. Otherwise, how can you turn it off?

Also, when the installed prop-types package doesn't have exact, it should not be supported.

Copy link
Contributor Author

@jzabala jzabala Aug 4, 2020

Choose a reason for hiding this comment

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

@ljharb
But why we would turn it off? exact has been part of the library since 2017 from what I see.

About validating that prop-types is installed, that would make us more correct but is not like React.PropTypes.exact is going to work anyway. In that case the user would get a ReferenceError on exact, I think.

Copy link
Member

Choose a reason for hiding this comment

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

Because someone might be using an older version of the library. There's lots of precedent; this plugin dynamically looks up the keys on require('prop-types') in order to determine what's available.

React.PropTypes.exact might work if a) the pragma isn't "React", b) they'd done React.PropTypes = require('prop-types');.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because someone might be using an older version of the library. There's lots of precedent; this plugin dynamically looks up the keys on require('prop-types') in order to determine what's available.

Yeah, but the rule no-types works the user having the library installed or not since this project already has it as a dependency:

https://github.com/yannickcr/eslint-plugin-react/blob/d8741de74da0fb7a0cb730f0fea14de05e4faa9b/package.json#L39

Doing as suggested in comment #2590 (comment) would result in exact always being included since the library would always resolve to the internal version of prop-types.

React.PropTypes.exact might work if a) the pragma isn't "React", b) they'd done React.PropTypes = require('prop-types');

Added two more tests for this cases.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, that is a fair point on the prop-types dep.

Copy link
Contributor Author

@jzabala jzabala Aug 13, 2020

Choose a reason for hiding this comment

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

But we can still do it right.

The only rule that depends on prop-types is no-typos.

https://github.com/yannickcr/eslint-plugin-react/blob/d8741de74da0fb7a0cb730f0fea14de05e4faa9b/lib/rules/no-typos.js#L7

So, to correctly feature detect exact what do you think if I change prop-types to a dev dependency, check for the existent of prop-types before requiring it and fix no-typos to not validate prop-types names if the lib is not present.

Copy link
Member

Choose a reason for hiding this comment

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

The intention in no-typos is that when new validators are added, they're suggested by default, without needing a change in this library.

if (argument.type !== 'ObjectExpression') {
// Invalid proptype or cannot analyse statically
return {};
}
const shapeTypeDefinition = {
type: 'shape',
type: callName,
children: {}
};
iterateProperties(context, argument.properties, (childKey, childValue, propNode) => {
Expand Down
64 changes: 64 additions & 0 deletions tests/lib/rules/prop-types.js
Expand Up @@ -5827,6 +5827,70 @@ ruleTester.run('prop-types', rule, {
errors: [{
message: '\'dateCreated\' is missing in props validation'
}]
},
{
code: `
function Zoo(props) {
return (
<>
{props.foo.c}
</>
);
}

Zoo.propTypes = {
foo: PropTypes.exact({
a: PropTypes.number,
b: PropTypes.number,
}),
};
`,
errors: [{
message: "'foo.c' is missing in props validation"
}]
},
{
code: `
function Zoo(props) {
return (
<>
{props.foo.c}
</>
);
}

Zoo.propTypes = {
foo: React.PropTypes.exact({
a: PropTypes.number,
b: PropTypes.number,
}),
};
`,
errors: [{
message: "'foo.c' is missing in props validation"
}]
},
{
code: `
function Zoo(props) {
return (
<>
{props.foo.c}
</>
);
}

Zoo.propTypes = {
foo: Foo.PropTypes.exact({
a: PropTypes.number,
b: PropTypes.number,
}),
};
`,
settings,
errors: [{
message: "'foo.c' is missing in props validation"
}]
}
])
)
Expand Down