Skip to content

Commit

Permalink
fix: fix no-unused-vars with with default type in GenericType (#411)
Browse files Browse the repository at this point in the history
  • Loading branch information
HsuTing authored and gajus committed Jun 13, 2019
1 parent c79837b commit e8d640c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
50 changes: 30 additions & 20 deletions src/rules/useFlowType.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
}
});
}
};
};
Expand Down
6 changes: 6 additions & 0 deletions tests/rules/assertions/useFlowType.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ComponentType<A>> = { b: B }; let x: X; console.log(x);',
errors: [
'\'A\' is defined but never used.'
]
}
];

Expand Down

0 comments on commit e8d640c

Please sign in to comment.