aboutsummaryrefslogtreecommitdiff
path: root/node_modules/tslint/lib/rules/noUnusedVariableRule.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/tslint/lib/rules/noUnusedVariableRule.js')
-rw-r--r--node_modules/tslint/lib/rules/noUnusedVariableRule.js112
1 files changed, 79 insertions, 33 deletions
diff --git a/node_modules/tslint/lib/rules/noUnusedVariableRule.js b/node_modules/tslint/lib/rules/noUnusedVariableRule.js
index b7b9f6fe4..8a7d7b5af 100644
--- a/node_modules/tslint/lib/rules/noUnusedVariableRule.js
+++ b/node_modules/tslint/lib/rules/noUnusedVariableRule.js
@@ -17,6 +17,7 @@
*/
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
+var semver = require("semver");
var utils = require("tsutils");
var ts = require("typescript");
var Lint = require("../index");
@@ -34,10 +35,10 @@ var Rule = /** @class */ (function (_super) {
/* tslint:disable:object-literal-sort-keys */
Rule.metadata = {
ruleName: "no-unused-variable",
- description: (_a = ["Disallows unused imports, variables, functions and\n private class members. Similar to tsc's --noUnusedParameters and --noUnusedLocals\n options, but does not interrupt code compilation."], _a.raw = ["Disallows unused imports, variables, functions and\n private class members. Similar to tsc's --noUnusedParameters and --noUnusedLocals\n options, but does not interrupt code compilation."], Lint.Utils.dedent(_a)),
- descriptionDetails: (_b = ["\n In addition to avoiding compilation errors, this rule may still be useful if you\n wish to have `tslint` automatically remove unused imports, variables, functions,\n and private class members, when using TSLint's `--fix` option."], _b.raw = ["\n In addition to avoiding compilation errors, this rule may still be useful if you\n wish to have \\`tslint\\` automatically remove unused imports, variables, functions,\n and private class members, when using TSLint's \\`--fix\\` option."], Lint.Utils.dedent(_b)),
+ description: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Disallows unused imports, variables, functions and\n private class members. Similar to tsc's --noUnusedParameters and --noUnusedLocals\n options, but does not interrupt code compilation."], ["\n Disallows unused imports, variables, functions and\n private class members. Similar to tsc's --noUnusedParameters and --noUnusedLocals\n options, but does not interrupt code compilation."]))),
+ descriptionDetails: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n In addition to avoiding compilation errors, this rule may still be useful if you\n wish to have `tslint` automatically remove unused imports, variables, functions,\n and private class members, when using TSLint's `--fix` option."], ["\n In addition to avoiding compilation errors, this rule may still be useful if you\n wish to have \\`tslint\\` automatically remove unused imports, variables, functions,\n and private class members, when using TSLint's \\`--fix\\` option."]))),
hasFix: true,
- optionsDescription: (_c = ["\n Three optional arguments may be optionally provided:\n\n * `\"check-parameters\"` disallows unused function and constructor parameters.\n * NOTE: this option is experimental and does not work with classes\n that use abstract method declarations, among other things.\n * `{\"ignore-pattern\": \"pattern\"}` where pattern is a case-sensitive regexp.\n Variable names and imports that match the pattern will be ignored."], _c.raw = ["\n Three optional arguments may be optionally provided:\n\n * \\`\"check-parameters\"\\` disallows unused function and constructor parameters.\n * NOTE: this option is experimental and does not work with classes\n that use abstract method declarations, among other things.\n * \\`{\"ignore-pattern\": \"pattern\"}\\` where pattern is a case-sensitive regexp.\n Variable names and imports that match the pattern will be ignored."], Lint.Utils.dedent(_c)),
+ optionsDescription: Lint.Utils.dedent(templateObject_3 || (templateObject_3 = tslib_1.__makeTemplateObject(["\n Three optional arguments may be optionally provided:\n\n * `\"check-parameters\"` disallows unused function and constructor parameters.\n * NOTE: this option is experimental and does not work with classes\n that use abstract method declarations, among other things.\n * `{\"ignore-pattern\": \"pattern\"}` where pattern is a case-sensitive regexp.\n Variable names and imports that match the pattern will be ignored."], ["\n Three optional arguments may be optionally provided:\n\n * \\`\"check-parameters\"\\` disallows unused function and constructor parameters.\n * NOTE: this option is experimental and does not work with classes\n that use abstract method declarations, among other things.\n * \\`{\"ignore-pattern\": \"pattern\"}\\` where pattern is a case-sensitive regexp.\n Variable names and imports that match the pattern will be ignored."]))),
options: {
type: "array",
items: {
@@ -59,9 +60,13 @@ var Rule = /** @class */ (function (_super) {
maxLength: 3,
},
optionExamples: [true, [true, { "ignore-pattern": "^_" }]],
+ rationale: Lint.Utils.dedent(templateObject_4 || (templateObject_4 = tslib_1.__makeTemplateObject(["\n Variables that are declared and not used anywhere in code are likely an error due to incomplete refactoring.\n Such variables take up space in the code, are mild performance pains, and can lead to confusion by readers.\n "], ["\n Variables that are declared and not used anywhere in code are likely an error due to incomplete refactoring.\n Such variables take up space in the code, are mild performance pains, and can lead to confusion by readers.\n "]))),
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,
};
return Rule;
}(Lint.Rules.TypedRule));
@@ -100,22 +105,26 @@ function walk(ctx, program) {
continue;
}
var failure = ts.flattenDiagnosticMessageText(diag.messageText, "\n");
- if (ignorePattern !== undefined) {
+ // BUG: this means imports / destructures with all (2+) unused variables don't respect ignore pattern
+ if (ignorePattern !== undefined && kind !== 2 /* DECLARATION */ && kind !== 3 /* ALL_DESTRUCTURES */) {
var varName = /'(.*)'/.exec(failure)[1];
if (ignorePattern.test(varName)) {
continue;
}
}
- if (kind === 0 /* VARIABLE_OR_PARAMETER */) {
- var importName = findImport(diag.start, sourceFile);
- if (importName !== undefined) {
- if (declaration && isImportUsed(importName, sourceFile, checker)) {
- continue;
- }
- if (importSpecifierFailures.has(importName)) {
- throw new Error("Should not get 2 errors for the same import.");
+ if (kind === 0 /* VARIABLE_OR_PARAMETER */ || kind === 2 /* DECLARATION */) {
+ var importNames = findImports(diag.start, sourceFile, kind);
+ if (importNames.length > 0) {
+ for (var _b = 0, importNames_1 = importNames; _b < importNames_1.length; _b++) {
+ var importName = importNames_1[_b];
+ 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);
}
- importSpecifierFailures.set(importName, failure);
continue;
}
}
@@ -156,7 +165,7 @@ function addImportSpecifierFailures(ctx, failures, sourceFile) {
if (defaultName !== undefined) {
failures.delete(defaultName);
}
- removeAll(importNode, "All imports are unused.");
+ removeAll(importNode, "All imports on this line are unused.");
return;
}
if (defaultName !== undefined) {
@@ -249,7 +258,7 @@ function isImportUsed(importSpecifier, sourceFile, checker) {
return false;
}
var symbol = checker.getAliasedSymbol(importedSymbol);
- if (!Lint.isSymbolFlagSet(symbol, ts.SymbolFlags.Type)) {
+ if (!utils.isSymbolFlagSet(symbol, ts.SymbolFlags.Type)) {
return false;
}
return ts.forEachChild(sourceFile, function cb(child) {
@@ -292,12 +301,13 @@ function forEachImport(sourceFile, f) {
return undefined;
});
}
-function findImport(pos, sourceFile) {
- return forEachImport(sourceFile, function (i) {
+function findImports(pos, sourceFile, kind) {
+ var imports = forEachImport(sourceFile, function (i) {
+ if (!isInRange(i, pos)) {
+ return undefined;
+ }
if (i.kind === ts.SyntaxKind.ImportEqualsDeclaration) {
- if (i.name.getStart() === pos) {
- return i.name;
- }
+ return [i.name];
}
else {
if (i.importClause === undefined) {
@@ -306,33 +316,69 @@ function findImport(pos, sourceFile) {
}
var _a = i.importClause, defaultName = _a.name, namedBindings = _a.namedBindings;
if (namedBindings !== undefined && namedBindings.kind === ts.SyntaxKind.NamespaceImport) {
- var name = namedBindings.name;
- if (name.getStart() === pos) {
- return name;
+ 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 === 2 /* DECLARATION */) {
+ var imp = [];
+ if (defaultName !== undefined) {
+ imp.push(defaultName);
}
- return undefined;
+ if (namedBindings !== undefined) {
+ imp.push.apply(imp, namedBindings.elements.map(function (el) { return el.name; }));
+ }
+ return imp.length > 0 ? imp : undefined;
}
- if (defaultName !== undefined && defaultName.getStart() === pos) {
- return defaultName;
+ 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 (var _i = 0, _b = namedBindings.elements; _i < _b.length; _i++) {
- var name = _b[_i].name;
- if (name.getStart() === pos) {
- return name;
+ var element = _b[_i];
+ if (isInRange(element, pos)) {
+ return [element.name];
}
}
}
}
return undefined;
});
+ return imports !== undefined ? imports : [];
+}
+function isInRange(range, pos) {
+ return range.pos <= pos && range.end >= pos;
}
function getUnusedDiagnostic(diag) {
+ // https://github.com/Microsoft/TypeScript/blob/master/src/compiler/diagnosticMessages.json
switch (diag.code) {
- case 6133:
- return 0 /* VARIABLE_OR_PARAMETER */; // "'{0}' is declared but never used.
+ 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 0 /* VARIABLE_OR_PARAMETER */;
case 6138:
return 1 /* PROPERTY */; // "Property '{0}' is declared but never used."
+ case 6192:
+ return 2 /* DECLARATION */; // "All imports in import declaration are unused."
+ case 6198:
+ return 3 /* ALL_DESTRUCTURES */; // "All destructured elements are unused."
default:
return undefined;
}
@@ -358,7 +404,7 @@ function makeUnusedCheckedProgram(program, checkParameters) {
readFile: function (f) { return sourceFilesByName.get(getCanonicalFileName(f)).text; },
getSourceFile: function (f) { return sourceFilesByName.get(getCanonicalFileName(f)); },
getDefaultLibFileName: function () { return ts.getDefaultLibFileName(options); },
- writeFile: function () { },
+ writeFile: function () { return undefined; },
getCurrentDirectory: function () { return ""; },
getDirectories: function () { return []; },
getCanonicalFileName: getCanonicalFileName,
@@ -371,4 +417,4 @@ function makeUnusedCheckedProgram(program, checkParameters) {
return ts.sys.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase();
}
}
-var _a, _b, _c;
+var templateObject_1, templateObject_2, templateObject_3, templateObject_4;