Skip to content

Commit 630b84c

Browse files
committed
[Fix] no-unused-prop-types: detect used props in nested components
1 parent f2869fd commit 630b84c

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

lib/util/usedPropTypes.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,20 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
407407
}
408408
}
409409

410-
components.set(component ? component.node : node, {
410+
let targetNode = component ? component.node : node;
411+
412+
if (component && !component.declaredPropTypes) {
413+
let parentComponent = components.get(node.parent);
414+
while (parentComponent) {
415+
if (parentComponent.declaredPropTypes) {
416+
targetNode = parentComponent.node;
417+
break;
418+
}
419+
parentComponent = components.get(parentComponent.parent);
420+
}
421+
}
422+
423+
components.set(targetNode, {
411424
usedPropTypes,
412425
ignoreUnusedPropTypesValidation,
413426
});

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3948,6 +3948,22 @@ ruleTester.run('no-unused-prop-types', rule, {
39483948
`,
39493949
features: ['types'],
39503950
},
3951+
{
3952+
code: `
3953+
type Props = {
3954+
used: string;
3955+
};
3956+
3957+
const Demo = React.memo<Props>(
3958+
React.forwardRef<HTMLDivElement, Props>(
3959+
({ used }, ref) => {
3960+
return <div>{used}</div>
3961+
}
3962+
)
3963+
);
3964+
`,
3965+
features: ['types'],
3966+
},
39513967
]),
39523968

39533969
invalid: parsers.all([].concat(
@@ -6709,6 +6725,26 @@ ruleTester.run('no-unused-prop-types', rule, {
67096725
errors: [
67106726
{ message: '\'bar\' PropType is defined but prop is never used' },
67116727
],
6728+
},
6729+
{
6730+
code: `
6731+
type Props = {
6732+
used: string;
6733+
unused: string;
6734+
};
6735+
6736+
const Demo = React.memo<Props>(
6737+
React.forwardRef<HTMLDivElement, Props>(
6738+
({ used }, ref) => {
6739+
return <div>{used}</div>
6740+
}
6741+
)
6742+
);
6743+
`,
6744+
features: ['ts', 'no-babel'],
6745+
errors: [
6746+
{ message: '\'unused\' PropType is defined but prop is never used' },
6747+
],
67126748
}
67136749
)),
67146750
});

0 commit comments

Comments
 (0)