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 885
/
Copy pathnoUnusedVariableRule.ts
208 lines (171 loc) · 8.41 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
/*
* 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.
*/
const OPTION_CHECK_PARAMETERS = "check-parameters";
export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING = "unused variable: ";
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const documentRegistry = ts.createDocumentRegistry();
const languageServiceHost = Lint.createLanguageServiceHost("file.ts", sourceFile.getFullText());
const languageService = ts.createLanguageService(languageServiceHost, documentRegistry);
return this.applyWithWalker(new NoUnusedVariablesWalker(sourceFile, this.getOptions(), languageService));
}
}
class NoUnusedVariablesWalker extends Lint.RuleWalker {
private skipBindingElement: boolean;
private skipParameterDeclaration: boolean;
private skipVariableDeclaration: boolean;
private languageService: ts.LanguageService;
constructor(sourceFile: ts.SourceFile, options: Lint.IOptions, languageService: ts.LanguageService) {
super(sourceFile, options);
this.skipVariableDeclaration = false;
this.skipParameterDeclaration = false;
this.languageService = languageService;
}
public visitBindingElement(node: ts.BindingElement) {
const isSingleVariable = node.name.kind === ts.SyntaxKind.Identifier;
if (isSingleVariable && !this.skipBindingElement) {
const variableIdentifier = <ts.Identifier> node.name;
this.validateReferencesForVariable(variableIdentifier.text, variableIdentifier.getStart());
}
super.visitBindingElement(node);
}
public visitImportDeclaration(node: ts.ImportDeclaration) {
if (!Lint.hasModifier(node.modifiers, ts.SyntaxKind.ExportKeyword)) {
const importClause = node.importClause;
// Named imports & namespace imports handled by other walker methods.
// importClause will be null for bare imports.
if (importClause != null && importClause.name != null) {
const variableIdentifier = importClause.name;
this.validateReferencesForVariable(variableIdentifier.text, variableIdentifier.getStart());
}
}
super.visitImportDeclaration(node);
}
public visitImportEqualsDeclaration(node: ts.ImportEqualsDeclaration) {
if (!Lint.hasModifier(node.modifiers, ts.SyntaxKind.ExportKeyword)) {
const name = node.name;
this.validateReferencesForVariable(name.text, name.getStart());
}
super.visitImportEqualsDeclaration(node);
}
public visitCatchClause(node: ts.CatchClause) {
// don't visit the catch clause variable declaration, just visit the block
// the catch clause variable declaration needs to be there but doesn't need to be used
this.visitBlock(node.block);
}
public visitNamedImports(node: ts.NamedImports) {
node.elements.forEach((namedImport: ts.ImportSpecifier) => {
this.validateReferencesForVariable(namedImport.name.text, namedImport.name.getStart());
});
super.visitNamedImports(node);
}
public visitNamespaceImport(node: ts.NamespaceImport) {
this.validateReferencesForVariable(node.name.text, node.name.getStart());
super.visitNamespaceImport(node);
}
public visitVariableDeclaration(node: ts.VariableDeclaration) {
const isSingleVariable = node.name.kind === ts.SyntaxKind.Identifier;
if (isSingleVariable && !this.skipVariableDeclaration) {
const variableIdentifier = <ts.Identifier> node.name;
this.validateReferencesForVariable(variableIdentifier.text, variableIdentifier.getStart());
}
super.visitVariableDeclaration(node);
}
// skip parameters in interfaces
public visitInterfaceDeclaration(node: ts.InterfaceDeclaration) {
this.skipParameterDeclaration = true;
super.visitInterfaceDeclaration(node);
this.skipParameterDeclaration = false;
}
// skip parameters in index signatures (stuff like [key: string]: string)
public visitIndexSignatureDeclaration(node: ts.IndexSignatureDeclaration) {
this.skipParameterDeclaration = true;
super.visitIndexSignatureDeclaration(node);
this.skipParameterDeclaration = false;
}
// skip exported and declared variables
public visitVariableStatement(node: ts.VariableStatement) {
if (Lint.hasModifier(node.modifiers, ts.SyntaxKind.ExportKeyword, ts.SyntaxKind.DeclareKeyword)) {
this.skipBindingElement = true;
this.skipVariableDeclaration = true;
}
super.visitVariableStatement(node);
this.skipBindingElement = false;
this.skipVariableDeclaration = false;
}
public visitFunctionType(node: ts.Node) {
this.skipParameterDeclaration = true;
super.visitFunctionType(node);
this.skipParameterDeclaration = false;
}
// skip exported and declared functions
public visitFunctionDeclaration(node: ts.FunctionDeclaration) {
if (!Lint.hasModifier(node.modifiers, ts.SyntaxKind.ExportKeyword, ts.SyntaxKind.DeclareKeyword)) {
const variableName = node.name.text;
this.validateReferencesForVariable(variableName, node.name.getStart());
}
super.visitFunctionDeclaration(node);
}
public visitParameterDeclaration(node: ts.ParameterDeclaration) {
const isSingleVariable = node.name.kind === ts.SyntaxKind.Identifier;
const isPropertyParameter = Lint.hasModifier(node.modifiers,
ts.SyntaxKind.PublicKeyword,
ts.SyntaxKind.PrivateKeyword,
ts.SyntaxKind.ProtectedKeyword);
if (!isSingleVariable && isPropertyParameter) {
// tsc error: a parameter property may not be a binding pattern
this.skipBindingElement = true;
}
if (this.hasOption(OPTION_CHECK_PARAMETERS)
&& isSingleVariable
&& !this.skipParameterDeclaration
&& !Lint.hasModifier(node.modifiers, ts.SyntaxKind.PublicKeyword)) {
const nameNode = <ts.Identifier> node.name;
this.validateReferencesForVariable(nameNode.text, node.name.getStart());
}
super.visitParameterDeclaration(node);
this.skipBindingElement = false;
}
// check private member variables
public visitPropertyDeclaration(node: ts.PropertyDeclaration) {
if (node.name != null && node.name.kind === ts.SyntaxKind.Identifier) {
const modifiers = node.modifiers;
const variableName = (<ts.Identifier> node.name).text;
// check only if an explicit 'private' modifier is specified
if (Lint.hasModifier(modifiers, ts.SyntaxKind.PrivateKeyword)) {
this.validateReferencesForVariable(variableName, node.name.getStart());
}
}
super.visitPropertyDeclaration(node);
}
// check private member functions
public visitMethodDeclaration(node: ts.MethodDeclaration) {
if (node.name != null && node.name.kind === ts.SyntaxKind.Identifier) {
const modifiers = node.modifiers;
const variableName = (<ts.Identifier> node.name).text;
if (Lint.hasModifier(modifiers, ts.SyntaxKind.PrivateKeyword)) {
this.validateReferencesForVariable(variableName, node.name.getStart());
}
}
super.visitMethodDeclaration(node);
}
private validateReferencesForVariable(name: string, position: number) {
const highlights = this.languageService.getDocumentHighlights("file.ts", position, ["file.ts"]);
if (highlights[0].highlightSpans.length <= 1) {
this.addFailure(this.createFailure(position, name.length, `${Rule.FAILURE_STRING}'${name}'`));
}
}
}