Skip to content

Commit

Permalink
Fix prop-types to ignore validation when Flow indexers are used
Browse files Browse the repository at this point in the history
Fixes #2330
  • Loading branch information
yannickcr committed Jun 26, 2019
1 parent 8432e5e commit bc67d42
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/rules/prop-types.js
Expand Up @@ -103,7 +103,7 @@ module.exports = {
return true;
}
// Consider every children as declared
if (propType.children === true || propType.containsSpread) {
if (propType.children === true || propType.containsSpread || propType.containsIndexers) {
return true;
}
if (propType.acceptedProperties) {
Expand Down
4 changes: 3 additions & 1 deletion lib/util/propTypes.js
Expand Up @@ -122,6 +122,7 @@ module.exports = function propTypesInstructions(context, components, utils) {

ObjectTypeAnnotation(annotation, parentName, seen) {
let containsObjectTypeSpread = false;
const containsIndexers = Boolean(annotation.indexers && annotation.indexers.length);
const shapeTypeDefinition = {
type: 'shape',
children: {}
Expand All @@ -140,9 +141,10 @@ module.exports = function propTypesInstructions(context, components, utils) {
}
});

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

return shapeTypeDefinition;
},
Expand Down
48 changes: 48 additions & 0 deletions tests/lib/rules/prop-types.js
Expand Up @@ -2355,6 +2355,26 @@ ruleTester.run('prop-types', rule, {
code: `
for (const {result} of results) {}
`
},
{
// issue #2330
code: `
type Props = {
user: {
[string]: string
}
};
export function Bar(props: Props) {
return (
<div>
{props.user}
{props.user.name}
</div>
);
}
`,
parser: parsers.BABEL_ESLINT
}
],

Expand Down Expand Up @@ -4675,6 +4695,34 @@ ruleTester.run('prop-types', rule, {
errors: [{
message: '\'lastname\' is missing in props validation'
}]
},
{
// issue #2330
code: `
type Props = {
user: {
name: {
firstname: string,
[string]: string
}
}
};
export function Bar(props: Props) {
return (
<div>
{props.user}
{props.user.name.firstname}
{props.user.name.lastname}
{props.user.age}
</div>
);
}
`,
parser: parsers.BABEL_ESLINT,
errors: [{
message: '\'user.age\' is missing in props validation'
}]
}
]
});

0 comments on commit bc67d42

Please sign in to comment.