Skip to content

Commit 6c733ff

Browse files
committed
Add flow annotation type support to no-unused-prop-types
1 parent 94fbe6f commit 6c733ff

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

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

+42
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,42 @@ module.exports = {
133133
return false;
134134
}
135135

136+
/**
137+
* Resolve the type annotation for a given node.
138+
* Flow annotations are sometimes wrapped in outer `TypeAnnotation`
139+
* and `NullableTypeAnnotation` nodes which obscure the annotation we're
140+
* interested in.
141+
* This method also resolves type aliases where possible.
142+
*
143+
* @param {ASTNode} node The annotation or a node containing the type annotation.
144+
* @returns {ASTNode} The resolved type annotation for the node.
145+
*/
146+
function resolveTypeAnnotation(node) {
147+
let annotation = node.typeAnnotation || node;
148+
while (annotation && (annotation.type === 'TypeAnnotation' || annotation.type === 'NullableTypeAnnotation')) {
149+
annotation = annotation.typeAnnotation;
150+
}
151+
if (annotation.type === 'GenericTypeAnnotation' && typeScope(annotation.id.name)) {
152+
return typeScope(annotation.id.name);
153+
}
154+
return annotation;
155+
}
156+
157+
/**
158+
* Checks if we are declaring a props as a generic type in a flow-annotated class.
159+
*
160+
* @param {ASTNode} node the AST node being checked.
161+
* @returns {Boolean} True if the node is a class with generic prop types, false if not.
162+
*/
163+
function isSuperTypeParameterPropsDeclaration(node) {
164+
if (node && node.type === 'ClassDeclaration') {
165+
if (node.superTypeParameters && node.superTypeParameters.params.length > 0) {
166+
return true;
167+
}
168+
}
169+
return false;
170+
}
171+
136172
/**
137173
* Checks if we are declaring a prop
138174
* @param {ASTNode} node The AST node being checked.
@@ -866,6 +902,12 @@ module.exports = {
866902
// --------------------------------------------------------------------------
867903

868904
return {
905+
ClassDeclaration: function(node) {
906+
if (isSuperTypeParameterPropsDeclaration(node)) {
907+
markPropTypesAsDeclared(node, resolveSuperParameterPropsType(node));
908+
}
909+
},
910+
869911
ClassProperty: function(node) {
870912
if (isAnnotatedClassPropsDeclaration(node)) {
871913
markPropTypesAsDeclared(node, resolveTypeAnnotation(node));

0 commit comments

Comments
 (0)