Skip to content

Commit e8d640c

Browse files
HsuTinggajus
authored andcommitted
fix: fix no-unused-vars with with default type in GenericType (#411)
1 parent c79837b commit e8d640c

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

src/rules/useFlowType.js

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,43 @@ const create = (context) => {
44
const markTypeAsUsed = (node) => {
55
context.markVariableAsUsed(node.id.name);
66
};
7+
const markTypeAsUsedWithGenericType = (node) => {
8+
let typeId;
9+
let scope;
10+
let variable;
11+
12+
if (node.id.type === 'Identifier') {
13+
typeId = node.id;
14+
} else if (node.id.type === 'QualifiedTypeIdentifier') {
15+
typeId = node.id;
16+
do {
17+
typeId = typeId.qualification;
18+
} while (typeId.qualification);
19+
}
20+
21+
for (scope = context.getScope(); scope; scope = scope.upper) {
22+
variable = scope.set.get(typeId.name);
23+
if (variable && variable.defs.length) {
24+
context.markVariableAsUsed(typeId.name);
25+
break;
26+
}
27+
}
28+
};
729

830
return {
931
DeclareClass: markTypeAsUsed,
1032
DeclareFunction: markTypeAsUsed,
1133
DeclareModule: markTypeAsUsed,
1234
DeclareVariable: markTypeAsUsed,
13-
GenericTypeAnnotation (node) {
14-
let typeId;
15-
let scope;
16-
let variable;
17-
18-
if (node.id.type === 'Identifier') {
19-
typeId = node.id;
20-
} else if (node.id.type === 'QualifiedTypeIdentifier') {
21-
typeId = node.id;
22-
do {
23-
typeId = typeId.qualification;
24-
} while (typeId.qualification);
25-
}
26-
27-
for (scope = context.getScope(); scope; scope = scope.upper) {
28-
variable = scope.set.get(typeId.name);
29-
if (variable && variable.defs.length) {
30-
context.markVariableAsUsed(typeId.name);
31-
break;
35+
GenericTypeAnnotation: markTypeAsUsedWithGenericType,
36+
TypeParameterDeclaration (node) {
37+
node.params.forEach((param) => {
38+
if (param.default && param.default.typeParameters) {
39+
param.default.typeParameters.params.forEach((typeParameterNode) => {
40+
markTypeAsUsedWithGenericType(typeParameterNode);
41+
});
3242
}
33-
}
43+
});
3444
}
3545
};
3646
};

tests/rules/assertions/useFlowType.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ const VALID_WITH_USE_FLOW_TYPE = [
3434
errors: [
3535
'\'A\' is defined but never used.'
3636
]
37+
},
38+
{
39+
code: 'import type A from "a"; type X<B = ComponentType<A>> = { b: B }; let x: X; console.log(x);',
40+
errors: [
41+
'\'A\' is defined but never used.'
42+
]
3743
}
3844
];
3945

0 commit comments

Comments
 (0)