/
no-unused-vars.ts
302 lines (259 loc) · 9.46 KB
/
no-unused-vars.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import {
TSESTree,
TSESLint,
AST_NODE_TYPES,
} from '@typescript-eslint/experimental-utils';
import { PatternVisitor } from '@typescript-eslint/scope-manager';
import baseRule from 'eslint/lib/rules/no-unused-vars';
import * as util from '../util';
type MessageIds = util.InferMessageIdsTypeFromRule<typeof baseRule>;
type Options = util.InferOptionsTypeFromRule<typeof baseRule>;
export default util.createRule<Options, MessageIds>({
name: 'no-unused-vars',
meta: {
type: 'problem',
docs: {
description: 'Disallow unused variables',
category: 'Variables',
recommended: 'warn',
extendsBaseRule: true,
},
schema: baseRule.meta.schema,
messages: baseRule.meta.messages,
},
defaultOptions: [{}],
create(context) {
const rules = baseRule.create(context);
/**
* Gets a list of TS module definitions for a specified variable.
* @param variable eslint-scope variable object.
*/
function getModuleNameDeclarations(
variable: TSESLint.Scope.Variable,
): TSESTree.TSModuleDeclaration[] {
const moduleDeclarations: TSESTree.TSModuleDeclaration[] = [];
variable.defs.forEach(def => {
if (def.type === 'TSModuleName') {
moduleDeclarations.push(def.node);
}
});
return moduleDeclarations;
}
/**
* Determine if an identifier is referencing an enclosing name.
* This only applies to declarations that create their own scope (modules, functions, classes)
* @param ref The reference to check.
* @param nodes The candidate function nodes.
* @returns True if it's a self-reference, false if not.
*/
function isBlockSelfReference(
ref: TSESLint.Scope.Reference,
nodes: TSESTree.Node[],
): boolean {
let scope: TSESLint.Scope.Scope | null = ref.from;
while (scope) {
if (nodes.indexOf(scope.block) >= 0) {
return true;
}
scope = scope.upper;
}
return false;
}
function isExported(
variable: TSESLint.Scope.Variable,
target: AST_NODE_TYPES,
): boolean {
// TS will require that all merged namespaces/interfaces are exported, so we only need to find one
return variable.defs.some(
def =>
def.node.type === target &&
(def.node.parent?.type === AST_NODE_TYPES.ExportNamedDeclaration ||
def.node.parent?.type === AST_NODE_TYPES.ExportDefaultDeclaration),
);
}
return {
...rules,
'TSCallSignatureDeclaration, TSConstructorType, TSConstructSignatureDeclaration, TSDeclareFunction, TSEmptyBodyFunctionExpression, TSFunctionType, TSMethodSignature'(
node:
| TSESTree.TSCallSignatureDeclaration
| TSESTree.TSConstructorType
| TSESTree.TSConstructSignatureDeclaration
| TSESTree.TSDeclareFunction
| TSESTree.TSEmptyBodyFunctionExpression
| TSESTree.TSFunctionType
| TSESTree.TSMethodSignature,
): void {
// function type signature params create variables because they can be referenced within the signature,
// but they obviously aren't unused variables for the purposes of this rule.
for (const param of node.params) {
visitPattern(param, name => {
context.markVariableAsUsed(name.name);
});
}
},
TSEnumDeclaration(): void {
// enum members create variables because they can be referenced within the enum,
// but they obviously aren't unused variables for the purposes of this rule.
const scope = context.getScope();
for (const variable of scope.variables) {
context.markVariableAsUsed(variable.name);
}
},
TSMappedType(node): void {
// mapped types create a variable for their type name, but it's not necessary to reference it,
// so we shouldn't consider it as unused for the purpose of this rule.
context.markVariableAsUsed(node.typeParameter.name.name);
},
TSModuleDeclaration(): void {
const childScope = context.getScope();
const scope = util.nullThrows(
context.getScope().upper,
util.NullThrowsReasons.MissingToken(childScope.type, 'upper scope'),
);
for (const variable of scope.variables) {
const moduleNodes = getModuleNameDeclarations(variable);
if (
moduleNodes.length === 0 ||
// ignore unreferenced module definitions, as the base rule will report on them
variable.references.length === 0 ||
// ignore exported nodes
isExported(variable, AST_NODE_TYPES.TSModuleDeclaration)
) {
continue;
}
// check if the only reference to a module's name is a self-reference in its body
// this won't be caught by the base rule because it doesn't understand TS modules
const isOnlySelfReferenced = variable.references.every(ref => {
return isBlockSelfReference(ref, moduleNodes);
});
if (isOnlySelfReferenced) {
context.report({
node: variable.identifiers[0],
messageId: 'unusedVar',
data: {
varName: variable.name,
action: 'defined',
additional: '',
},
});
}
}
},
[[
'TSParameterProperty > AssignmentPattern > Identifier.left',
'TSParameterProperty > Identifier.parameter',
].join(', ')](node: TSESTree.Identifier): void {
// just assume parameter properties are used as property usage tracking is beyond the scope of this rule
context.markVariableAsUsed(node.name);
},
':matches(FunctionDeclaration, FunctionExpression, ArrowFunctionExpression) > Identifier[name="this"].params'(
node: TSESTree.Identifier,
): void {
// this parameters should always be considered used as they're pseudo-parameters
context.markVariableAsUsed(node.name);
},
'TSInterfaceDeclaration, TSTypeAliasDeclaration'(
node: TSESTree.TSInterfaceDeclaration | TSESTree.TSTypeAliasDeclaration,
): void {
const variable = context.getScope().set.get(node.id.name);
if (!variable) {
return;
}
if (
variable.references.length === 0 ||
// ignore exported nodes
isExported(variable, node.type)
) {
return;
}
// check if the type is only self-referenced
// this won't be caught by the base rule because it doesn't understand self-referencing types
const isOnlySelfReferenced = variable.references.every(ref => {
if (
ref.identifier.range[0] >= node.range[0] &&
ref.identifier.range[1] <= node.range[1]
) {
return true;
}
return false;
});
if (isOnlySelfReferenced) {
context.report({
node: variable.identifiers[0],
messageId: 'unusedVar',
data: {
varName: variable.name,
action: 'defined',
additional: '',
},
});
}
},
// TODO - this could probably be refined a bit
'*[declare=true] Identifier'(node: TSESTree.Identifier): void {
context.markVariableAsUsed(node.name);
const scope = context.getScope();
const { variableScope } = scope;
if (variableScope !== scope) {
const superVar = variableScope.set.get(node.name);
if (superVar) {
superVar.eslintUsed = true;
}
}
},
};
function visitPattern(
node: TSESTree.Node,
cb: (node: TSESTree.Identifier) => void,
): void {
const visitor = new PatternVisitor({}, node, cb);
visitor.visit(node);
}
},
});
/*
###### TODO ######
Edge cases that aren't currently handled due to laziness and them being super edgy edge cases
--- function params referenced in typeof type refs in the function declaration ---
--- NOTE - TS gets these cases wrong
function _foo(
arg: number // arg should be unused
): typeof arg {
return 1 as any;
}
function _bar(
arg: number, // arg should be unused
_arg2: typeof arg,
) {}
--- function names referenced in typeof type refs in the function declaration ---
--- NOTE - TS gets these cases right
function foo( // foo should be unused
): typeof foo {
return 1 as any;
}
function bar( // bar should be unused
_arg: typeof bar
) {}
*/
/*
###### TODO ######
We currently extend base `no-unused-vars` implementation because it's easier and lighter-weight.
Because of this, there are a few false-negatives which won't get caught.
We could fix these if we fork the base rule; but that's a lot of code (~650 lines) to add in.
I didn't want to do that just yet without some real-world issues, considering these are pretty rare edge-cases.
These cases are mishandled because the base rule assumes that each variable has one def, but type-value shadowing
creates a variable with two defs
--- type-only or value-only references to type/value shadowed variables ---
--- NOTE - TS gets these cases wrong
type T = 1;
const T = 2; // this T should be unused
type U = T; // this U should be unused
const U = 3;
const _V = U;
--- partially exported type/value shadowed variables ---
--- NOTE - TS gets these cases wrong
export interface Foo {}
const Foo = 1; // this Foo should be unused
interface Bar {} // this Bar should be unused
export const Bar = 1;
*/