Skip to content

Commit

Permalink
[Fix] no-unused-prop-types: fix issue with propTypes misclassifying…
Browse files Browse the repository at this point in the history
… props
  • Loading branch information
Luke Zapart authored and ljharb committed Jan 3, 2019
1 parent a86b339 commit e15bafa
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/util/propTypes.js
Expand Up @@ -67,6 +67,24 @@ function iterateProperties(context, properties, fn) {
}
}

/**
* Checks if a node is inside a class property.
*
* @param {ASTNode} node the AST node being checked.
* @returns {Boolean} True if the node has a ClassProperty ancestor, false if not.
*/
function isInsideClassProperty(node) {
let parent = node.parent;
while (parent) {
if (parent.type === 'ClassProperty') {
return true;
}
parent = parent.parent;
}

return false;
}

module.exports = function propTypesInstructions(context, components, utils) {
// Used to track the type annotations in scope.
// Necessary because babel's scopes do not track type annotations.
Expand Down Expand Up @@ -554,6 +572,10 @@ module.exports = function propTypesInstructions(context, components, utils) {
return;
}

if (isInsideClassProperty(node)) {
return;
}

const param = node.params[0];
if (param.typeAnnotation && param.typeAnnotation.typeAnnotation && param.typeAnnotation.typeAnnotation.type === 'UnionTypeAnnotation') {
param.typeAnnotation.typeAnnotation.types.forEach(annotation => {
Expand Down
15 changes: 15 additions & 0 deletions tests/lib/rules/no-unused-prop-types.js
Expand Up @@ -2929,6 +2929,21 @@ ruleTester.run('no-unused-prop-types', rule, {
' })',
'};'
].join('\n')
}, {
code: `
type Props = {
used: string,
}
class Hello extends React.Component<Props> {
renderHelper = ({unused}: {unused: string}) => {
return <div />;
}
render() {
return <div>{this.props.used}</div>;
}
}
`,
parser: 'babel-eslint'
}
],

Expand Down

0 comments on commit e15bafa

Please sign in to comment.