Skip to content

Commit 7850e57

Browse files
jzabalaljharb
authored andcommitted
[New] prop-types: add support for PropTypes.exact
Closes #2590.
1 parent d8741de commit 7850e57

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

lib/util/propTypes.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,13 +422,14 @@ module.exports = function propTypesInstructions(context, components, utils) {
422422
const callName = value.callee.property.name;
423423
const argument = value.arguments[0];
424424
switch (callName) {
425-
case 'shape': {
425+
case 'shape':
426+
case 'exact': {
426427
if (argument.type !== 'ObjectExpression') {
427428
// Invalid proptype or cannot analyse statically
428429
return {};
429430
}
430431
const shapeTypeDefinition = {
431-
type: 'shape',
432+
type: callName,
432433
children: {}
433434
};
434435
iterateProperties(context, argument.properties, (childKey, childValue, propNode) => {

tests/lib/rules/prop-types.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5827,6 +5827,70 @@ ruleTester.run('prop-types', rule, {
58275827
errors: [{
58285828
message: '\'dateCreated\' is missing in props validation'
58295829
}]
5830+
},
5831+
{
5832+
code: `
5833+
function Zoo(props) {
5834+
return (
5835+
<>
5836+
{props.foo.c}
5837+
</>
5838+
);
5839+
}
5840+
5841+
Zoo.propTypes = {
5842+
foo: PropTypes.exact({
5843+
a: PropTypes.number,
5844+
b: PropTypes.number,
5845+
}),
5846+
};
5847+
`,
5848+
errors: [{
5849+
message: "'foo.c' is missing in props validation"
5850+
}]
5851+
},
5852+
{
5853+
code: `
5854+
function Zoo(props) {
5855+
return (
5856+
<>
5857+
{props.foo.c}
5858+
</>
5859+
);
5860+
}
5861+
5862+
Zoo.propTypes = {
5863+
foo: React.PropTypes.exact({
5864+
a: PropTypes.number,
5865+
b: PropTypes.number,
5866+
}),
5867+
};
5868+
`,
5869+
errors: [{
5870+
message: "'foo.c' is missing in props validation"
5871+
}]
5872+
},
5873+
{
5874+
code: `
5875+
function Zoo(props) {
5876+
return (
5877+
<>
5878+
{props.foo.c}
5879+
</>
5880+
);
5881+
}
5882+
5883+
Zoo.propTypes = {
5884+
foo: Foo.PropTypes.exact({
5885+
a: PropTypes.number,
5886+
b: PropTypes.number,
5887+
}),
5888+
};
5889+
`,
5890+
settings,
5891+
errors: [{
5892+
message: "'foo.c' is missing in props validation"
5893+
}]
58305894
}
58315895
])
58325896
)

0 commit comments

Comments
 (0)