Skip to content

Commit c153a86

Browse files
committed
[Fix] no-unused-prop-types: false positive when nested destructuring
1 parent e336aef commit c153a86

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

lib/util/usedPropTypes.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -362,19 +362,20 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
362362
}
363363
const propName = ast.getKeyValue(context, properties[k]);
364364

365-
if (
366-
propName &&
367-
properties[k].type === 'Property' &&
368-
properties[k].value.type === 'ObjectPattern'
369-
) {
365+
if (!propName || properties[k].type !== 'Property') {
366+
break;
367+
}
368+
369+
usedPropTypes.push({
370+
allNames: parentNames.concat([propName]),
371+
name: propName,
372+
node: properties[k]
373+
});
374+
375+
if (properties[k].value.type === 'ObjectPattern') {
370376
markPropTypesAsUsed(properties[k].value, parentNames.concat([propName]));
371-
} else if (propName) {
377+
} else if (properties[k].value.type === 'Identifier') {
372378
propVariables.set(propName, parentNames.concat(propName));
373-
usedPropTypes.push({
374-
allNames: parentNames.concat([propName]),
375-
name: propName,
376-
node: properties[k]
377-
});
378379
}
379380
}
380381
break;

tests/lib/rules/no-unused-prop-types.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1825,6 +1825,18 @@ ruleTester.run('no-unused-prop-types', rule, {
18251825
' return <div />;',
18261826
'};'
18271827
].join('\n')
1828+
}, {
1829+
// Nested destructuring; issue 2424
1830+
code: `
1831+
function SomeComponent(props) {
1832+
const {aaa: {bbb}} = props;
1833+
return <p>{bbb}</p>;
1834+
}
1835+
1836+
SomeComponent.propTypes = {
1837+
aaa: somePropType,
1838+
};
1839+
`
18281840
}, {
18291841
// `no-unused-prop-types` in jsx expressions - [Issue #885]
18301842
code: [
@@ -1864,7 +1876,7 @@ ruleTester.run('no-unused-prop-types', rule, {
18641876
const { a } = props;
18651877
document.title = a;
18661878
});
1867-
1879+
18681880
return <p/>;
18691881
}
18701882

tests/lib/rules/prop-types.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2498,6 +2498,7 @@ ruleTester.run('prop-types', rule, {
24982498
}
24992499
`,
25002500
errors: [
2501+
{message: "'foo' is missing in props validation"},
25012502
{message: "'foo.bar' is missing in props validation"}
25022503
]
25032504
}, {
@@ -2525,6 +2526,7 @@ ruleTester.run('prop-types', rule, {
25252526
}
25262527
`,
25272528
errors: [
2529+
{message: "'foo' is missing in props validation"},
25282530
{message: "'foo.bar' is missing in props validation"}
25292531
]
25302532
},

0 commit comments

Comments
 (0)