Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with propTypes misclassifying props #2111

Merged
merged 1 commit into from Jan 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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