Skip to content

Commit bc67d42

Browse files
committed
Fix prop-types to ignore validation when Flow indexers are used
Fixes #2330
1 parent 8432e5e commit bc67d42

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

lib/rules/prop-types.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ module.exports = {
103103
return true;
104104
}
105105
// Consider every children as declared
106-
if (propType.children === true || propType.containsSpread) {
106+
if (propType.children === true || propType.containsSpread || propType.containsIndexers) {
107107
return true;
108108
}
109109
if (propType.acceptedProperties) {

lib/util/propTypes.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ module.exports = function propTypesInstructions(context, components, utils) {
122122

123123
ObjectTypeAnnotation(annotation, parentName, seen) {
124124
let containsObjectTypeSpread = false;
125+
const containsIndexers = Boolean(annotation.indexers && annotation.indexers.length);
125126
const shapeTypeDefinition = {
126127
type: 'shape',
127128
children: {}
@@ -140,9 +141,10 @@ module.exports = function propTypesInstructions(context, components, utils) {
140141
}
141142
});
142143

143-
// Mark if this shape has spread. We will know to consider all props from this shape as having propTypes,
144+
// Mark if this shape has spread or an indexer. We will know to consider all props from this shape as having propTypes,
144145
// but still have the ability to detect unused children of this shape.
145146
shapeTypeDefinition.containsSpread = containsObjectTypeSpread;
147+
shapeTypeDefinition.containsIndexers = containsIndexers;
146148

147149
return shapeTypeDefinition;
148150
},

tests/lib/rules/prop-types.js

+48
Original file line numberDiff line numberDiff line change
@@ -2355,6 +2355,26 @@ ruleTester.run('prop-types', rule, {
23552355
code: `
23562356
for (const {result} of results) {}
23572357
`
2358+
},
2359+
{
2360+
// issue #2330
2361+
code: `
2362+
type Props = {
2363+
user: {
2364+
[string]: string
2365+
}
2366+
};
2367+
2368+
export function Bar(props: Props) {
2369+
return (
2370+
<div>
2371+
{props.user}
2372+
{props.user.name}
2373+
</div>
2374+
);
2375+
}
2376+
`,
2377+
parser: parsers.BABEL_ESLINT
23582378
}
23592379
],
23602380

@@ -4675,6 +4695,34 @@ ruleTester.run('prop-types', rule, {
46754695
errors: [{
46764696
message: '\'lastname\' is missing in props validation'
46774697
}]
4698+
},
4699+
{
4700+
// issue #2330
4701+
code: `
4702+
type Props = {
4703+
user: {
4704+
name: {
4705+
firstname: string,
4706+
[string]: string
4707+
}
4708+
}
4709+
};
4710+
4711+
export function Bar(props: Props) {
4712+
return (
4713+
<div>
4714+
{props.user}
4715+
{props.user.name.firstname}
4716+
{props.user.name.lastname}
4717+
{props.user.age}
4718+
</div>
4719+
);
4720+
}
4721+
`,
4722+
parser: parsers.BABEL_ESLINT,
4723+
errors: [{
4724+
message: '\'user.age\' is missing in props validation'
4725+
}]
46784726
}
46794727
]
46804728
});

0 commit comments

Comments
 (0)