diff --git a/src/rules/useFlowType.js b/src/rules/useFlowType.js index 185dad40..dba4d23d 100644 --- a/src/rules/useFlowType.js +++ b/src/rules/useFlowType.js @@ -4,33 +4,43 @@ const create = (context) => { const markTypeAsUsed = (node) => { context.markVariableAsUsed(node.id.name); }; + const markTypeAsUsedWithGenericType = (node) => { + let typeId; + let scope; + let variable; + + if (node.id.type === 'Identifier') { + typeId = node.id; + } else if (node.id.type === 'QualifiedTypeIdentifier') { + typeId = node.id; + do { + typeId = typeId.qualification; + } while (typeId.qualification); + } + + for (scope = context.getScope(); scope; scope = scope.upper) { + variable = scope.set.get(typeId.name); + if (variable && variable.defs.length) { + context.markVariableAsUsed(typeId.name); + break; + } + } + }; return { DeclareClass: markTypeAsUsed, DeclareFunction: markTypeAsUsed, DeclareModule: markTypeAsUsed, DeclareVariable: markTypeAsUsed, - GenericTypeAnnotation (node) { - let typeId; - let scope; - let variable; - - if (node.id.type === 'Identifier') { - typeId = node.id; - } else if (node.id.type === 'QualifiedTypeIdentifier') { - typeId = node.id; - do { - typeId = typeId.qualification; - } while (typeId.qualification); - } - - for (scope = context.getScope(); scope; scope = scope.upper) { - variable = scope.set.get(typeId.name); - if (variable && variable.defs.length) { - context.markVariableAsUsed(typeId.name); - break; + GenericTypeAnnotation: markTypeAsUsedWithGenericType, + TypeParameterDeclaration (node) { + node.params.forEach((param) => { + if (param.default && param.default.typeParameters) { + param.default.typeParameters.params.forEach((typeParameterNode) => { + markTypeAsUsedWithGenericType(typeParameterNode); + }); } - } + }); } }; }; diff --git a/tests/rules/assertions/useFlowType.js b/tests/rules/assertions/useFlowType.js index e4765aca..526b91c5 100644 --- a/tests/rules/assertions/useFlowType.js +++ b/tests/rules/assertions/useFlowType.js @@ -34,6 +34,12 @@ const VALID_WITH_USE_FLOW_TYPE = [ errors: [ '\'A\' is defined but never used.' ] + }, + { + code: 'import type A from "a"; type X> = { b: B }; let x: X; console.log(x);', + errors: [ + '\'A\' is defined but never used.' + ] } ];