This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 889
/
noUnusedVariableRule.ts
547 lines (493 loc) · 20.5 KB
/
noUnusedVariableRule.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/**
* @license
* Copyright 2014 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as semver from "semver";
import * as utils from "tsutils";
import * as ts from "typescript";
import * as Lint from "../index";
const OPTION_CHECK_PARAMETERS = "check-parameters";
const OPTION_IGNORE_PATTERN = "ignore-pattern";
export class Rule extends Lint.Rules.TypedRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
ruleName: "no-unused-variable",
description: Lint.Utils.dedent`
Disallows unused imports, variables, functions and
private class members. Similar to tsc's --noUnusedParameters and --noUnusedLocals
options, but does not interrupt code compilation.`,
descriptionDetails: Lint.Utils.dedent`
In addition to avoiding compilation errors, this rule may still be useful if you
wish to have \`tslint\` automatically remove unused imports, variables, functions,
and private class members, when using TSLint's \`--fix\` option.`,
hasFix: true,
optionsDescription: Lint.Utils.dedent`
Two optional arguments may be optionally provided:
* \`"check-parameters"\` disallows unused function and constructor parameters.
* NOTE: this option is experimental and does not work with classes
that use abstract method declarations, among other things.
* \`{"ignore-pattern": "pattern"}\` where pattern is a case-sensitive regexp.
Variable names and imports that match the pattern will be ignored.`,
options: {
type: "array",
items: {
oneOf: [
{
type: "string",
enum: ["check-parameters"],
},
{
type: "object",
properties: {
"ignore-pattern": { type: "string" },
},
additionalProperties: false,
},
],
},
minLength: 0,
maxLength: 2,
},
optionExamples: [true, [true, { "ignore-pattern": "^_" }]],
rationale: Lint.Utils.dedent`
Variables that are declared and not used anywhere in code are likely an error due to incomplete refactoring.
Such variables take up space in the code, are mild performance pains, and can lead to confusion by readers.
`,
type: "functionality",
typescriptOnly: true,
requiresTypeInfo: true,
deprecationMessage: semver.gte(ts.version, "2.9.0-dev.0")
? "Since TypeScript 2.9. Please use the built-in compiler checks instead."
: undefined,
};
/* tslint:enable:object-literal-sort-keys */
public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] {
return this.applyWithFunction(sourceFile, walk, parseOptions(this.ruleArguments), program);
}
}
interface Options {
checkParameters: boolean;
ignorePattern: RegExp | undefined;
}
function parseOptions(options: any[]): Options {
const checkParameters = options.indexOf(OPTION_CHECK_PARAMETERS) !== -1;
let ignorePattern: RegExp | undefined;
for (const o of options) {
if (typeof o === "object") {
// tslint:disable-next-line no-unsafe-any no-null-undefined-union
const ignore = o[OPTION_IGNORE_PATTERN] as string | null | undefined;
if (ignore != undefined) {
ignorePattern = new RegExp(ignore);
break;
}
}
}
return { checkParameters, ignorePattern };
}
function walk(ctx: Lint.WalkContext<Options>, program: ts.Program): void {
const {
sourceFile,
options: { checkParameters, ignorePattern },
} = ctx;
const unusedCheckedProgram = getUnusedCheckedProgram(program, checkParameters);
const diagnostics = ts.getPreEmitDiagnostics(unusedCheckedProgram, sourceFile);
const checker = unusedCheckedProgram.getTypeChecker(); // Doesn't matter which program is used for this.
const declaration = program.getCompilerOptions().declaration;
// If all specifiers in an import are unused, we elide the entire import.
const importSpecifierFailures = new Map<ts.Identifier, string>();
for (const diag of diagnostics) {
if (diag.start === undefined) {
continue;
}
const kind = getUnusedDiagnostic(diag);
if (kind === undefined) {
continue;
}
const failure = ts.flattenDiagnosticMessageText(diag.messageText, "\n");
// BUG: this means imports / destructures with all (2+) unused variables don't respect ignore pattern
if (
ignorePattern !== undefined &&
kind !== UnusedKind.DECLARATION &&
kind !== UnusedKind.ALL_DESTRUCTURES
) {
const varName = /'(.*)'/.exec(failure)![1];
if (ignorePattern.test(varName)) {
continue;
}
}
if (kind === UnusedKind.VARIABLE_OR_PARAMETER || kind === UnusedKind.DECLARATION) {
const importNames = findImports(diag.start, sourceFile, kind);
if (importNames.length > 0) {
for (const importName of importNames) {
if (declaration && isImportUsed(importName, sourceFile, checker)) {
continue;
}
if (importSpecifierFailures.has(importName)) {
throw new Error("Should not get 2 errors for the same import.");
}
importSpecifierFailures.set(importName, failure);
}
continue;
}
}
ctx.addFailureAt(diag.start, diag.length!, failure);
}
if (importSpecifierFailures.size !== 0) {
addImportSpecifierFailures(ctx, importSpecifierFailures, sourceFile);
}
}
/**
* Handle import-specifier failures separately.
* - If all of the import specifiers in an import are unused, add a combined failure for them all.
* - Unused imports are fixable.
*/
function addImportSpecifierFailures(
ctx: Lint.WalkContext<Options>,
failures: Map<ts.Identifier, string>,
sourceFile: ts.SourceFile,
) {
forEachImport(sourceFile, importNode => {
if (importNode.kind === ts.SyntaxKind.ImportEqualsDeclaration) {
tryRemoveAll(importNode.name);
return;
}
if (importNode.importClause === undefined) {
// Error node
return;
}
const { name: defaultName, namedBindings } = importNode.importClause;
if (namedBindings !== undefined && namedBindings.kind === ts.SyntaxKind.NamespaceImport) {
tryRemoveAll(namedBindings.name);
return;
}
const allNamedBindingsAreFailures =
namedBindings === undefined || namedBindings.elements.every(e => failures.has(e.name));
if (namedBindings !== undefined && allNamedBindingsAreFailures) {
for (const e of namedBindings.elements) {
failures.delete(e.name);
}
}
if (
(defaultName === undefined || failures.has(defaultName)) &&
allNamedBindingsAreFailures
) {
if (defaultName !== undefined) {
failures.delete(defaultName);
}
removeAll(importNode, "All imports on this line are unused.");
return;
}
if (defaultName !== undefined) {
const failure = tryDelete(defaultName);
if (failure !== undefined) {
const start = defaultName.getStart();
const end =
namedBindings !== undefined
? namedBindings.getStart()
: importNode.moduleSpecifier.getStart();
const fix = Lint.Replacement.deleteFromTo(start, end);
ctx.addFailureAtNode(defaultName, failure, fix);
}
}
if (namedBindings !== undefined) {
if (allNamedBindingsAreFailures) {
const start =
defaultName !== undefined ? defaultName.getEnd() : namedBindings.getStart();
const fix = Lint.Replacement.deleteFromTo(start, namedBindings.getEnd());
const failure = "All named bindings are unused.";
ctx.addFailureAtNode(namedBindings, failure, fix);
} else {
const { elements } = namedBindings;
for (let i = 0; i < elements.length; i++) {
const element = elements[i];
const failure = tryDelete(element.name);
if (failure === undefined) {
continue;
}
const prevElement = elements[i - 1];
const nextElement = elements[i + 1];
const start =
prevElement !== undefined ? prevElement.getEnd() : element.getStart();
const end =
nextElement !== undefined && prevElement == undefined
? nextElement.getStart()
: element.getEnd();
const fix = Lint.Replacement.deleteFromTo(start, end);
ctx.addFailureAtNode(element.name, failure, fix);
}
}
}
function tryRemoveAll(name: ts.Identifier): void {
const failure = tryDelete(name);
if (failure !== undefined) {
removeAll(name, failure);
}
}
function removeAll(errorNode: ts.Node, failure: string): void {
const start = importNode.getStart();
let end = importNode.getEnd();
utils.forEachToken(
importNode,
token => {
ts.forEachTrailingCommentRange(
ctx.sourceFile.text,
token.end,
(_, commentEnd, __) => {
end = commentEnd;
},
);
},
ctx.sourceFile,
);
if (isEntireLine(start, end)) {
end = getNextLineStart(end);
}
const fix = Lint.Replacement.deleteFromTo(start, end);
ctx.addFailureAtNode(errorNode, failure, fix);
}
function isEntireLine(start: number, end: number): boolean {
return (
ctx.sourceFile.getLineAndCharacterOfPosition(start).character === 0 &&
ctx.sourceFile.getLineEndOfPosition(end) === end
);
}
function getNextLineStart(position: number): number {
const nextLine = ctx.sourceFile.getLineAndCharacterOfPosition(position).line + 1;
const lineStarts = ctx.sourceFile.getLineStarts();
if (nextLine < lineStarts.length) {
return lineStarts[nextLine];
} else {
return position;
}
}
});
if (failures.size !== 0) {
throw new Error("Should have revisited all import specifier failures.");
}
function tryDelete(name: ts.Identifier): string | undefined {
const failure = failures.get(name);
if (failure !== undefined) {
failures.delete(name);
return failure;
}
return undefined;
}
}
/**
* Ignore this import if it's used as an implicit type somewhere.
* Workround for https://github.com/Microsoft/TypeScript/issues/9944
*/
function isImportUsed(
importSpecifier: ts.Identifier,
sourceFile: ts.SourceFile,
checker: ts.TypeChecker,
): boolean {
const importedSymbol = checker.getSymbolAtLocation(importSpecifier);
if (importedSymbol === undefined) {
return false;
}
const symbol = checker.getAliasedSymbol(importedSymbol);
if (!utils.isSymbolFlagSet(symbol, ts.SymbolFlags.Type)) {
return false;
}
return (
ts.forEachChild(sourceFile, function cb(child): boolean | undefined {
if (isImportLike(child)) {
return false;
}
const type = getImplicitType(child, checker);
// TODO: checker.typeEquals https://github.com/Microsoft/TypeScript/issues/13502
if (
type !== undefined &&
checker.typeToString(type) === checker.symbolToString(symbol)
) {
return true;
}
return ts.forEachChild(child, cb);
}) === true
);
}
function getImplicitType(node: ts.Node, checker: ts.TypeChecker): ts.Type | undefined {
if (
((utils.isPropertyDeclaration(node) || utils.isVariableDeclaration(node)) &&
node.type === undefined &&
node.name.kind === ts.SyntaxKind.Identifier) ||
(utils.isBindingElement(node) && node.name.kind === ts.SyntaxKind.Identifier)
) {
return checker.getTypeAtLocation(node);
} else if (utils.isSignatureDeclaration(node) && node.type === undefined) {
const sig = checker.getSignatureFromDeclaration(node);
return sig === undefined ? undefined : sig.getReturnType();
} else {
return undefined;
}
}
type ImportLike = ts.ImportDeclaration | ts.ImportEqualsDeclaration;
function isImportLike(node: ts.Node): node is ImportLike {
return (
node.kind === ts.SyntaxKind.ImportDeclaration ||
node.kind === ts.SyntaxKind.ImportEqualsDeclaration
);
}
function forEachImport<T>(
sourceFile: ts.SourceFile,
f: (i: ImportLike) => T | undefined,
): T | undefined {
return ts.forEachChild(sourceFile, child => {
if (isImportLike(child)) {
const res = f(child);
if (res !== undefined) {
return res;
}
}
return undefined;
});
}
function findImports(
pos: number,
sourceFile: ts.SourceFile,
kind: UnusedKind,
): ReadonlyArray<ts.Identifier> {
const imports = forEachImport(sourceFile, i => {
if (!isInRange(i, pos)) {
return undefined;
}
if (i.kind === ts.SyntaxKind.ImportEqualsDeclaration) {
return [i.name];
} else {
if (i.importClause === undefined) {
// Error node
return undefined;
}
const { name: defaultName, namedBindings } = i.importClause;
if (
namedBindings !== undefined &&
namedBindings.kind === ts.SyntaxKind.NamespaceImport
) {
return [namedBindings.name];
}
// Starting from TS2.8, when all imports in an import node are not used,
// TS emits only 1 diagnostic object for the whole line as opposed
// to the previous behavior of outputting a diagnostic with kind == 6192
// (UnusedKind.VARIABLE_OR_PARAMETER) for every unused import.
// From TS2.8, in the case of none of the imports in a line being used,
// the single diagnostic TS outputs are different between the 1 import
// and 2+ imports cases:
// - 1 import in node:
// - diagnostic has kind == 6133 (UnusedKind.VARIABLE_OR_PARAMETER)
// - the text range is the whole node (`import { ... } from "..."`)
// whereas pre-TS2.8, the text range was for the import node. so
// `name.getStart()` won't equal `pos` like in pre-TS2.8
// - 2+ imports in node:
// - diagnostic has kind == 6192 (UnusedKind.DECLARATION)
// - we know that all of these are unused
if (kind === UnusedKind.DECLARATION) {
const imp: ts.Identifier[] = [];
if (defaultName !== undefined) {
imp.push(defaultName);
}
if (namedBindings !== undefined) {
imp.push(...namedBindings.elements.map(el => el.name));
}
return imp.length > 0 ? imp : undefined;
} else if (
defaultName !== undefined &&
(isInRange(defaultName, pos) || namedBindings === undefined) // defaultName is the only option
) {
return [defaultName];
} else if (namedBindings !== undefined) {
if (namedBindings.elements.length === 1) {
return [namedBindings.elements[0].name];
}
for (const element of namedBindings.elements) {
if (isInRange(element, pos)) {
return [element.name];
}
}
}
}
return undefined;
});
return imports !== undefined ? imports : [];
}
function isInRange(range: ts.TextRange, pos: number): boolean {
return range.pos <= pos && range.end >= pos;
}
const enum UnusedKind {
VARIABLE_OR_PARAMETER,
PROPERTY,
DECLARATION, // Introduced in TS 2.8
ALL_DESTRUCTURES, // introduced in TS 2.9
}
function getUnusedDiagnostic(diag: ts.Diagnostic): UnusedKind | undefined {
// https://github.com/Microsoft/TypeScript/blob/master/src/compiler/diagnosticMessages.json
switch (diag.code) {
case 6133: // Pre TS 2.9 "'{0}' is declared but never used.
// TS 2.9+ "'{0}' is declared but its value is never read."
case 6196: // TS 2.9+ "'{0}' is declared but never used."
return UnusedKind.VARIABLE_OR_PARAMETER;
case 6138:
return UnusedKind.PROPERTY; // "Property '{0}' is declared but never used."
case 6192:
return UnusedKind.DECLARATION; // "All imports in import declaration are unused."
case 6198:
return UnusedKind.ALL_DESTRUCTURES; // "All destructured elements are unused."
default:
return undefined;
}
}
const programToUnusedCheckedProgram = new WeakMap<ts.Program, ts.Program>();
function getUnusedCheckedProgram(program: ts.Program, checkParameters: boolean): ts.Program {
// Assuming checkParameters will always have the same value, so only lookup by program.
let checkedProgram = programToUnusedCheckedProgram.get(program);
if (checkedProgram !== undefined) {
return checkedProgram;
}
checkedProgram = makeUnusedCheckedProgram(program, checkParameters);
programToUnusedCheckedProgram.set(program, checkedProgram);
return checkedProgram;
}
function makeUnusedCheckedProgram(program: ts.Program, checkParameters: boolean): ts.Program {
const originalOptions = program.getCompilerOptions();
const options = {
...originalOptions,
noEmit: true,
noUnusedLocals: true,
noUnusedParameters: originalOptions.noUnusedParameters || checkParameters,
};
const sourceFilesByName = new Map<string, ts.SourceFile>(
program
.getSourceFiles()
.map<[string, ts.SourceFile]>(s => [getCanonicalFileName(s.fileName), s]),
);
// tslint:disable object-literal-sort-keys
return ts.createProgram(Array.from(sourceFilesByName.keys()), options, {
fileExists: f => sourceFilesByName.has(getCanonicalFileName(f)),
readFile: f => sourceFilesByName.get(getCanonicalFileName(f))!.text,
getSourceFile: f => sourceFilesByName.get(getCanonicalFileName(f))!,
getDefaultLibFileName: () => ts.getDefaultLibFileName(options),
writeFile: () => undefined,
getCurrentDirectory: () => "",
getDirectories: () => [],
getCanonicalFileName,
useCaseSensitiveFileNames: () => ts.sys.useCaseSensitiveFileNames,
getNewLine: () => "\n",
});
// tslint:enable object-literal-sort-keys
// We need to be careful with file system case sensitivity
function getCanonicalFileName(fileName: string): string {
return ts.sys.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase();
}
}