Skip to content

Commit

Permalink
[Fix] no-unused-prop-types: false positive when nested destructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
golopot committed Oct 1, 2019
1 parent e336aef commit c153a86
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
23 changes: 12 additions & 11 deletions lib/util/usedPropTypes.js
Expand Up @@ -362,19 +362,20 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
}
const propName = ast.getKeyValue(context, properties[k]);

if (
propName &&
properties[k].type === 'Property' &&
properties[k].value.type === 'ObjectPattern'
) {
if (!propName || properties[k].type !== 'Property') {
break;
}

usedPropTypes.push({
allNames: parentNames.concat([propName]),
name: propName,
node: properties[k]
});

if (properties[k].value.type === 'ObjectPattern') {
markPropTypesAsUsed(properties[k].value, parentNames.concat([propName]));
} else if (propName) {
} else if (properties[k].value.type === 'Identifier') {
propVariables.set(propName, parentNames.concat(propName));
usedPropTypes.push({
allNames: parentNames.concat([propName]),
name: propName,
node: properties[k]
});
}
}
break;
Expand Down
14 changes: 13 additions & 1 deletion tests/lib/rules/no-unused-prop-types.js
Expand Up @@ -1825,6 +1825,18 @@ ruleTester.run('no-unused-prop-types', rule, {
' return <div />;',
'};'
].join('\n')
}, {
// Nested destructuring; issue 2424
code: `
function SomeComponent(props) {
const {aaa: {bbb}} = props;
return <p>{bbb}</p>;
}
SomeComponent.propTypes = {
aaa: somePropType,
};
`
}, {
// `no-unused-prop-types` in jsx expressions - [Issue #885]
code: [
Expand Down Expand Up @@ -1864,7 +1876,7 @@ ruleTester.run('no-unused-prop-types', rule, {
const { a } = props;
document.title = a;
});
return <p/>;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/lib/rules/prop-types.js
Expand Up @@ -2498,6 +2498,7 @@ ruleTester.run('prop-types', rule, {
}
`,
errors: [
{message: "'foo' is missing in props validation"},
{message: "'foo.bar' is missing in props validation"}
]
}, {
Expand Down Expand Up @@ -2525,6 +2526,7 @@ ruleTester.run('prop-types', rule, {
}
`,
errors: [
{message: "'foo' is missing in props validation"},
{message: "'foo.bar' is missing in props validation"}
]
},
Expand Down

0 comments on commit c153a86

Please sign in to comment.