Skip to content

Commit

Permalink
[New] prop-types: add support for PropTypes.exact
Browse files Browse the repository at this point in the history
Closes #2590.
  • Loading branch information
jzabala authored and ljharb committed Aug 3, 2020
1 parent d8741de commit 7850e57
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
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': {
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

0 comments on commit 7850e57

Please sign in to comment.