Skip to content

Commit ed1c787

Browse files
Luke Zapartljharb
Luke Zapart
authored andcommitted
[Fix] no-unused-prop-types: fix issue with propTypes misclassifying props
1 parent c93cbc6 commit ed1c787

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

lib/util/propTypes.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,24 @@ function iterateProperties(context, properties, fn) {
6767
}
6868
}
6969

70+
/**
71+
* Checks if a node is inside a class property.
72+
*
73+
* @param {ASTNode} node the AST node being checked.
74+
* @returns {Boolean} True if the node has a ClassProperty ancestor, false if not.
75+
*/
76+
function isInsideClassProperty(node) {
77+
let parent = node.parent;
78+
while (parent) {
79+
if (parent.type === 'ClassProperty') {
80+
return true;
81+
}
82+
parent = parent.parent;
83+
}
84+
85+
return false;
86+
}
87+
7088
module.exports = function propTypesInstructions(context, components, utils) {
7189
// Used to track the type annotations in scope.
7290
// Necessary because babel's scopes do not track type annotations.
@@ -554,6 +572,10 @@ module.exports = function propTypesInstructions(context, components, utils) {
554572
return;
555573
}
556574

575+
if (isInsideClassProperty(node)) {
576+
return;
577+
}
578+
557579
const param = node.params[0];
558580
if (param.typeAnnotation && param.typeAnnotation.typeAnnotation && param.typeAnnotation.typeAnnotation.type === 'UnionTypeAnnotation') {
559581
param.typeAnnotation.typeAnnotation.types.forEach(annotation => {

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2929,6 +2929,21 @@ ruleTester.run('no-unused-prop-types', rule, {
29292929
' })',
29302930
'};'
29312931
].join('\n')
2932+
}, {
2933+
code: `
2934+
type Props = {
2935+
used: string,
2936+
}
2937+
class Hello extends React.Component<Props> {
2938+
renderHelper = ({unused}: {unused: string}) => {
2939+
return <div />;
2940+
}
2941+
render() {
2942+
return <div>{this.props.used}</div>;
2943+
}
2944+
}
2945+
`,
2946+
parser: 'babel-eslint'
29322947
}
29332948
],
29342949

0 commit comments

Comments
 (0)