noUnusedVariableRule.js
13.4 KB
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
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var ts = require("typescript");
var Lint = require("../lint");
var OPTION_REACT = "react";
var OPTION_CHECK_PARAMETERS = "check-parameters";
var REACT_MODULES = ["react", "react/addons"];
var REACT_NAMESPACE_IMPORT_NAME = "React";
var MODULE_SPECIFIER_MATCH = /^["'](.+)['"]$/;
var Rule = (function (_super) {
__extends(Rule, _super);
function Rule() {
_super.apply(this, arguments);
}
Rule.prototype.apply = function (sourceFile) {
var languageService = Lint.createLanguageService(sourceFile.fileName, sourceFile.getFullText());
return this.applyWithWalker(new NoUnusedVariablesWalker(sourceFile, this.getOptions(), languageService));
};
Rule.metadata = {
ruleName: "no-unused-variable",
description: "Disallows unused imports, variables, functions and private class members.",
optionsDescription: (_a = ["\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 * `\"react\"` relaxes the rule for a namespace import named `React`\n (from either the module `\"react\"` or `\"react/addons\"`).\n Any JSX expression in the file will be treated as a usage of `React`\n (because it expands to `React.createElement `).\n * `{\"ignore-pattern\": \"pattern\"}` where pattern is a case-sensitive regexp.\n Variable names that match the pattern will be ignored."], _a.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 * \\`\"react\"\\` relaxes the rule for a namespace import named \\`React\\`\n (from either the module \\`\"react\"\\` or \\`\"react/addons\"\\`).\n Any JSX expression in the file will be treated as a usage of \\`React\\`\n (because it expands to \\`React.createElement \\`).\n * \\`{\"ignore-pattern\": \"pattern\"}\\` where pattern is a case-sensitive regexp.\n Variable names that match the pattern will be ignored."], Lint.Utils.dedent(_a)),
options: {
type: "array",
items: {
oneOf: [{
type: "string",
enum: ["check-parameters", "react"],
}, {
type: "object",
properties: {
"ignore-pattern": { type: "string" },
},
additionalProperties: false,
}],
},
minLength: 0,
maxLength: 3,
},
optionExamples: ['[true, "react"]', '[true, {"ignore-pattern": "^_"}]'],
type: "functionality",
};
Rule.FAILURE_TYPE_FUNC = "function";
Rule.FAILURE_TYPE_IMPORT = "import";
Rule.FAILURE_TYPE_METHOD = "method";
Rule.FAILURE_TYPE_PARAM = "parameter";
Rule.FAILURE_TYPE_PROP = "property";
Rule.FAILURE_TYPE_VAR = "variable";
Rule.FAILURE_STRING_FACTORY = function (type, name) { return ("Unused " + type + ": '" + name + "'"); };
return Rule;
var _a;
}(Lint.Rules.AbstractRule));
exports.Rule = Rule;
var NoUnusedVariablesWalker = (function (_super) {
__extends(NoUnusedVariablesWalker, _super);
function NoUnusedVariablesWalker(sourceFile, options, languageService) {
_super.call(this, sourceFile, options);
this.languageService = languageService;
this.skipVariableDeclaration = false;
this.skipParameterDeclaration = false;
this.hasSeenJsxElement = false;
this.isReactUsed = false;
var ignorePatternOption = this.getOptions().filter(function (option) {
return typeof option === "object" && option["ignore-pattern"] != null;
})[0];
if (ignorePatternOption != null) {
this.ignorePattern = new RegExp(ignorePatternOption["ignore-pattern"]);
}
}
NoUnusedVariablesWalker.prototype.visitSourceFile = function (node) {
_super.prototype.visitSourceFile.call(this, node);
if (this.hasOption(OPTION_REACT)
&& this.reactImport != null
&& !this.isReactUsed
&& !this.hasSeenJsxElement) {
var nameText = this.reactImport.name.getText();
if (!this.isIgnored(nameText)) {
var start = this.reactImport.name.getStart();
var msg = Rule.FAILURE_STRING_FACTORY(Rule.FAILURE_TYPE_IMPORT, nameText);
this.addFailure(this.createFailure(start, nameText.length, msg));
}
}
};
NoUnusedVariablesWalker.prototype.visitBindingElement = function (node) {
var isSingleVariable = node.name.kind === ts.SyntaxKind.Identifier;
if (isSingleVariable && !this.skipBindingElement) {
var variableIdentifier = node.name;
this.validateReferencesForVariable(Rule.FAILURE_TYPE_VAR, variableIdentifier.text, variableIdentifier.getStart());
}
_super.prototype.visitBindingElement.call(this, node);
};
NoUnusedVariablesWalker.prototype.visitCatchClause = function (node) {
this.visitBlock(node.block);
};
NoUnusedVariablesWalker.prototype.visitFunctionDeclaration = function (node) {
if (!Lint.hasModifier(node.modifiers, ts.SyntaxKind.ExportKeyword, ts.SyntaxKind.DeclareKeyword)) {
var variableName = node.name.text;
this.validateReferencesForVariable(Rule.FAILURE_TYPE_FUNC, variableName, node.name.getStart());
}
_super.prototype.visitFunctionDeclaration.call(this, node);
};
NoUnusedVariablesWalker.prototype.visitFunctionType = function (node) {
this.skipParameterDeclaration = true;
_super.prototype.visitFunctionType.call(this, node);
this.skipParameterDeclaration = false;
};
NoUnusedVariablesWalker.prototype.visitImportDeclaration = function (node) {
if (!Lint.hasModifier(node.modifiers, ts.SyntaxKind.ExportKeyword)) {
var importClause = node.importClause;
if (importClause != null && importClause.name != null) {
var variableIdentifier = importClause.name;
this.validateReferencesForVariable(Rule.FAILURE_TYPE_IMPORT, variableIdentifier.text, variableIdentifier.getStart());
}
}
_super.prototype.visitImportDeclaration.call(this, node);
};
NoUnusedVariablesWalker.prototype.visitImportEqualsDeclaration = function (node) {
if (!Lint.hasModifier(node.modifiers, ts.SyntaxKind.ExportKeyword)) {
var name_1 = node.name;
this.validateReferencesForVariable(Rule.FAILURE_TYPE_IMPORT, name_1.text, name_1.getStart());
}
_super.prototype.visitImportEqualsDeclaration.call(this, node);
};
NoUnusedVariablesWalker.prototype.visitIndexSignatureDeclaration = function (node) {
this.skipParameterDeclaration = true;
_super.prototype.visitIndexSignatureDeclaration.call(this, node);
this.skipParameterDeclaration = false;
};
NoUnusedVariablesWalker.prototype.visitInterfaceDeclaration = function (node) {
this.skipParameterDeclaration = true;
_super.prototype.visitInterfaceDeclaration.call(this, node);
this.skipParameterDeclaration = false;
};
NoUnusedVariablesWalker.prototype.visitJsxElement = function (node) {
this.hasSeenJsxElement = true;
_super.prototype.visitJsxElement.call(this, node);
};
NoUnusedVariablesWalker.prototype.visitJsxSelfClosingElement = function (node) {
this.hasSeenJsxElement = true;
_super.prototype.visitJsxSelfClosingElement.call(this, node);
};
NoUnusedVariablesWalker.prototype.visitMethodDeclaration = function (node) {
if (node.name != null && node.name.kind === ts.SyntaxKind.Identifier) {
var modifiers = node.modifiers;
var variableName = node.name.text;
if (Lint.hasModifier(modifiers, ts.SyntaxKind.PrivateKeyword)) {
this.validateReferencesForVariable(Rule.FAILURE_TYPE_METHOD, variableName, node.name.getStart());
}
}
if (Lint.hasModifier(node.modifiers, ts.SyntaxKind.AbstractKeyword)) {
this.skipParameterDeclaration = true;
}
_super.prototype.visitMethodDeclaration.call(this, node);
this.skipParameterDeclaration = false;
};
NoUnusedVariablesWalker.prototype.visitNamedImports = function (node) {
for (var _i = 0, _a = node.elements; _i < _a.length; _i++) {
var namedImport = _a[_i];
this.validateReferencesForVariable(Rule.FAILURE_TYPE_IMPORT, namedImport.name.text, namedImport.name.getStart());
}
_super.prototype.visitNamedImports.call(this, node);
};
NoUnusedVariablesWalker.prototype.visitNamespaceImport = function (node) {
var importDeclaration = node.parent.parent;
var moduleSpecifier = importDeclaration.moduleSpecifier.getText();
var moduleNameMatch = moduleSpecifier.match(MODULE_SPECIFIER_MATCH);
var isReactImport = (moduleNameMatch != null) && (REACT_MODULES.indexOf(moduleNameMatch[1]) !== -1);
if (this.hasOption(OPTION_REACT) && isReactImport && node.name.text === REACT_NAMESPACE_IMPORT_NAME) {
this.reactImport = node;
var fileName = this.getSourceFile().fileName;
var position = node.name.getStart();
var highlights = this.languageService.getDocumentHighlights(fileName, position, [fileName]);
if (highlights != null && highlights[0].highlightSpans.length > 1) {
this.isReactUsed = true;
}
}
else {
this.validateReferencesForVariable(Rule.FAILURE_TYPE_IMPORT, node.name.text, node.name.getStart());
}
_super.prototype.visitNamespaceImport.call(this, node);
};
NoUnusedVariablesWalker.prototype.visitParameterDeclaration = function (node) {
var isSingleVariable = node.name.kind === ts.SyntaxKind.Identifier;
var isPropertyParameter = Lint.hasModifier(node.modifiers, ts.SyntaxKind.PublicKeyword, ts.SyntaxKind.PrivateKeyword, ts.SyntaxKind.ProtectedKeyword);
if (!isSingleVariable && isPropertyParameter) {
this.skipBindingElement = true;
}
if (this.hasOption(OPTION_CHECK_PARAMETERS)
&& isSingleVariable
&& !this.skipParameterDeclaration
&& !Lint.hasModifier(node.modifiers, ts.SyntaxKind.PublicKeyword)) {
var nameNode = node.name;
this.validateReferencesForVariable(Rule.FAILURE_TYPE_PARAM, nameNode.text, node.name.getStart());
}
_super.prototype.visitParameterDeclaration.call(this, node);
this.skipBindingElement = false;
};
NoUnusedVariablesWalker.prototype.visitPropertyDeclaration = function (node) {
if (node.name != null && node.name.kind === ts.SyntaxKind.Identifier) {
var modifiers = node.modifiers;
var variableName = node.name.text;
if (Lint.hasModifier(modifiers, ts.SyntaxKind.PrivateKeyword)) {
this.validateReferencesForVariable(Rule.FAILURE_TYPE_PROP, variableName, node.name.getStart());
}
}
_super.prototype.visitPropertyDeclaration.call(this, node);
};
NoUnusedVariablesWalker.prototype.visitVariableDeclaration = function (node) {
var isSingleVariable = node.name.kind === ts.SyntaxKind.Identifier;
if (isSingleVariable && !this.skipVariableDeclaration) {
var variableIdentifier = node.name;
this.validateReferencesForVariable(Rule.FAILURE_TYPE_VAR, variableIdentifier.text, variableIdentifier.getStart());
}
_super.prototype.visitVariableDeclaration.call(this, node);
};
NoUnusedVariablesWalker.prototype.visitVariableStatement = function (node) {
if (Lint.hasModifier(node.modifiers, ts.SyntaxKind.ExportKeyword, ts.SyntaxKind.DeclareKeyword)) {
this.skipBindingElement = true;
this.skipVariableDeclaration = true;
}
_super.prototype.visitVariableStatement.call(this, node);
this.skipBindingElement = false;
this.skipVariableDeclaration = false;
};
NoUnusedVariablesWalker.prototype.validateReferencesForVariable = function (type, name, position) {
var fileName = this.getSourceFile().fileName;
var highlights = this.languageService.getDocumentHighlights(fileName, position, [fileName]);
if ((highlights == null || highlights[0].highlightSpans.length <= 1) && !this.isIgnored(name)) {
this.addFailure(this.createFailure(position, name.length, Rule.FAILURE_STRING_FACTORY(type, name)));
}
};
NoUnusedVariablesWalker.prototype.isIgnored = function (name) {
return this.ignorePattern != null && this.ignorePattern.test(name);
};
return NoUnusedVariablesWalker;
}(Lint.RuleWalker));