This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
/
noShadowedVariableRule.ts
365 lines (340 loc) · 15.5 KB
/
noShadowedVariableRule.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
/**
* @license
* Copyright 2013 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 {
hasModifier, isBlockScopedVariableDeclarationList, isClassExpression, isFunctionExpression, isFunctionWithBody, isNodeFlagSet,
isScopeBoundary, isThisParameter, ScopeBoundary,
} from "tsutils";
import * as ts from "typescript";
import * as Lint from "../index";
export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
ruleName: "no-shadowed-variable",
description: "Disallows shadowing variable declarations.",
rationale: Lint.Utils.dedent`
Shadowing a variable masks access to it and obscures to what value an identifier actually refers.
For example, in the following code, it can be confusing why the filter is likely never true:
\`\`\`
const findNeighborsWithin = (instance: MyClass, instances: MyClass[]): MyClass[] => {
return instances.filter((instance) => instance.neighbors.includes(instance));
};
\`\`\`
`,
optionsDescription: Lint.Utils.dedent`
You can optionally pass an object to disable checking for certain kinds of declarations.
Possible keys are \`"class"\`, \`"enum"\`, \`"function"\`, \`"import"\`, \`"interface"\`, \`"namespace"\`, \`"typeAlias"\`
and \`"typeParameter"\`. Just set the value to \`false\` for the check you want to disable.
All checks default to \`true\`, i.e. are enabled by default.
Note that you cannot disable variables and parameters.
The option \`"temporalDeadZone"\` defaults to \`true\` which shows errors when shadowing block scoped declarations in their
temporal dead zone. When set to \`false\` parameters, classes, enums and variables declared
with \`let\` or \`const\` are not considered shadowed if the shadowing occurs within their
[temporal dead zone](http://jsrocks.org/2015/01/temporal-dead-zone-tdz-demystified).
The following example shows how the \`"temporalDeadZone"\` option changes the linting result:
\`\`\`ts
function fn(value) {
if (value) {
const tmp = value; // no error on this line if "temporalDeadZone" is false
return tmp;
}
let tmp = undefined;
if (!value) {
const tmp = value; // this line always contains an error
return tmp;
}
}
\`\`\`
`,
options: {
type: "object",
properties: {
class: {type: "boolean"},
enum: {type: "boolean"},
function: {type: "boolean"},
import: {type: "boolean"},
interface: {type: "boolean"},
namespace: {type: "boolean"},
typeAlias: {type: "boolean"},
typeParameter: {type: "boolean"},
temporalDeadZone: {type: "boolean"},
},
},
optionExamples: [
true,
[true, {class: true, enum: true, function: true, interface: false, namespace: true, typeAlias: false, typeParameter: false}],
],
type: "functionality",
typescriptOnly: false,
};
/* tslint:enable:object-literal-sort-keys */
public static FAILURE_STRING_FACTORY(name: string) {
return `Shadowed name: '${name}'`;
}
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(
new NoShadowedVariableWalker(sourceFile, this.ruleName, parseOptions(this.ruleArguments[0] as Partial<Options> | undefined)),
);
}
}
type Kind = "class" | "import" | "interface" | "function" | "enum" | "namespace" | "typeParameter" | "typeAlias" | "temporalDeadZone";
type Options = Record<Kind, boolean>;
function parseOptions(option: Partial<Options> | undefined): Options {
return {
class: true,
enum: true,
function: true,
import: true,
interface: true,
namespace: true,
temporalDeadZone: true,
typeAlias: true,
typeParameter: true,
...option,
};
}
interface VariableInfo {
identifier: ts.Identifier;
tdz: boolean;
}
class Scope {
public functionScope: Scope;
public variables = new Map<string, VariableInfo[]>();
public variablesSeen = new Map<string, ts.Identifier[]>();
public reassigned = new Set<string>();
constructor(functionScope?: Scope) {
// if no functionScope is provided we are in the process of creating a new function scope, which for consistency links to itself
this.functionScope = functionScope !== undefined ? functionScope : this;
}
public addVariable(identifier: ts.Identifier, blockScoped = true, tdz = false) {
// block scoped variables go to the block scope, function scoped variables to the containing function scope
const scope = blockScoped ? this : this.functionScope;
const list = scope.variables.get(identifier.text);
const variableInfo: VariableInfo = {
identifier,
tdz,
};
if (list === undefined) {
scope.variables.set(identifier.text, [variableInfo]);
} else {
list.push(variableInfo);
}
}
}
class NoShadowedVariableWalker extends Lint.AbstractWalker<Options> {
private scope: Scope = new Scope();
public walk(sourceFile: ts.SourceFile) {
if (sourceFile.isDeclarationFile) {
return;
}
this.scope = new Scope();
const cb = (node: ts.Node): void => {
const parentScope = this.scope;
if ((this.options.function && isFunctionExpression(node) || this.options.class && isClassExpression(node)) &&
node.name !== undefined) {
/* special handling for named function and class expressions:
technically the name of the function is only visible inside of it,
but variables with the same name declared inside don't cause compiler errors.
Therefore we add an additional function scope only for the function name to avoid merging with other declarations */
const functionScope = new Scope();
functionScope.addVariable(node.name, false);
this.scope = new Scope();
if (isClassExpression(node)) {
this.visitClassLikeDeclaration(node, functionScope, cb);
} else {
ts.forEachChild(node, cb);
}
this.onScopeEnd(functionScope);
this.scope = functionScope;
this.onScopeEnd(parentScope);
this.scope = parentScope;
return;
}
/* Visit decorators before entering a function scope.
In the AST decorators are children of the declaration they decorate, but we don't want to warn for the following code:
@decorator((param) => param)
function foo(param) {}
*/
if (node.decorators !== undefined) {
for (const decorator of node.decorators) {
ts.forEachChild(decorator, cb);
}
}
const boundary = isScopeBoundary(node);
if (boundary === ScopeBoundary.Block) {
this.scope = new Scope(parentScope.functionScope);
} else if (boundary === ScopeBoundary.Function) {
this.scope = new Scope();
}
switch (node.kind) {
case ts.SyntaxKind.Decorator:
return; // handled above
case ts.SyntaxKind.VariableDeclarationList:
this.handleVariableDeclarationList(node as ts.VariableDeclarationList);
break;
case ts.SyntaxKind.TypeParameter:
if (this.options.typeParameter) {
this.scope.addVariable((node as ts.TypeParameterDeclaration).name);
}
break;
case ts.SyntaxKind.FunctionDeclaration:
if (this.options.function && (node as ts.FunctionDeclaration).name !== undefined) {
parentScope.addVariable((node as ts.FunctionDeclaration).name!, false);
}
break;
case ts.SyntaxKind.ClassDeclaration:
if (this.options.class && (node as ts.ClassDeclaration).name !== undefined) {
parentScope.addVariable((node as ts.ClassDeclaration).name!, true, true);
}
// falls through
case ts.SyntaxKind.ClassExpression:
this.visitClassLikeDeclaration(node as ts.ClassLikeDeclaration, parentScope, cb);
this.onScopeEnd(parentScope);
this.scope = parentScope;
return;
case ts.SyntaxKind.TypeAliasDeclaration:
if (this.options.typeAlias) {
parentScope.addVariable((node as ts.TypeAliasDeclaration).name);
}
break;
case ts.SyntaxKind.EnumDeclaration:
if (this.options.enum) {
parentScope.addVariable((node as ts.EnumDeclaration).name, true, true);
}
break;
case ts.SyntaxKind.InterfaceDeclaration:
if (this.options.interface) {
parentScope.addVariable((node as ts.InterfaceDeclaration).name);
}
break;
case ts.SyntaxKind.Parameter:
if (node.parent!.kind !== ts.SyntaxKind.IndexSignature &&
!isThisParameter(node as ts.ParameterDeclaration) &&
isFunctionWithBody(node.parent!)) {
this.handleBindingName((node as ts.ParameterDeclaration).name, false, true);
}
break;
case ts.SyntaxKind.ModuleDeclaration:
if (this.options.namespace &&
node.parent!.kind !== ts.SyntaxKind.ModuleDeclaration &&
(node as ts.ModuleDeclaration).name.kind === ts.SyntaxKind.Identifier &&
!isNodeFlagSet(node, ts.NodeFlags.GlobalAugmentation)
) {
parentScope.addVariable((node as ts.NamespaceDeclaration).name, false);
}
if (hasModifier(node.modifiers, ts.SyntaxKind.DeclareKeyword)) {
this.onScopeEnd(parentScope);
this.scope = parentScope;
return; // don't check any ambient declaration blocks
}
break;
case ts.SyntaxKind.ImportClause:
if (this.options.import && (node as ts.ImportClause).name !== undefined) {
this.scope.addVariable((node as ts.ImportClause).name!, false);
}
break;
case ts.SyntaxKind.NamespaceImport:
case ts.SyntaxKind.ImportSpecifier:
case ts.SyntaxKind.ImportEqualsDeclaration:
if (this.options.import) {
this.scope.addVariable((node as ts.NamespaceImport | ts.ImportSpecifier | ts.ImportEqualsDeclaration).name, false);
}
}
if (boundary !== ScopeBoundary.None) {
ts.forEachChild(node, cb);
this.onScopeEnd(parentScope);
this.scope = parentScope;
} else {
return ts.forEachChild(node, cb);
}
};
ts.forEachChild(sourceFile, cb);
this.onScopeEnd();
}
private visitClassLikeDeclaration(declaration: ts.ClassLikeDeclaration, parentScope: Scope, cb: (node: ts.Node) => void) {
const currentScope = this.scope;
ts.forEachChild(declaration, (node) => {
if (!hasModifier(node.modifiers, ts.SyntaxKind.StaticKeyword)) {
return cb(node);
}
/* Don't treat static members as children of the class' scope. That avoid shadowed type parameter warnings on static members.
class C<T> {
static method<T>() {}
}
*/
this.scope = parentScope;
cb(node);
this.scope = currentScope;
});
}
private handleVariableDeclarationList(node: ts.VariableDeclarationList) {
const blockScoped = isBlockScopedVariableDeclarationList(node);
for (const variable of node.declarations) {
this.handleBindingName(variable.name, blockScoped);
}
}
private handleBindingName(node: ts.BindingName, blockScoped: boolean, tdz = blockScoped) {
if (node.kind === ts.SyntaxKind.Identifier) {
this.scope.addVariable(node, blockScoped, tdz);
} else {
for (const element of node.elements) {
if (element.kind !== ts.SyntaxKind.OmittedExpression) {
this.handleBindingName(element.name, blockScoped, tdz);
}
}
}
}
private onScopeEnd(parent?: Scope) {
const {variables, variablesSeen} = this.scope;
variablesSeen.forEach((identifiers, name) => {
const declarationsInScope = variables.get(name);
for (const identifier of identifiers) {
if (declarationsInScope !== undefined &&
(this.options.temporalDeadZone ||
// check if any of the declaration either has no temporal dead zone or is declared before the identifier
declarationsInScope.some((declaration) => !declaration.tdz || declaration.identifier.pos < identifier.pos))
) {
this.addFailureAtNode(identifier, Rule.FAILURE_STRING_FACTORY(name));
} else if (parent !== undefined) {
addOneToList(parent.variablesSeen, name, identifier);
}
}
});
if (parent !== undefined) {
variables.forEach((identifiers, name) => {
addToList(parent.variablesSeen, name, identifiers);
});
}
}
}
function addToList(map: Map<string, ts.Identifier[]>, name: string, variables: VariableInfo[]) {
let list = map.get(name);
if (list === undefined) {
list = [];
map.set(name, list);
}
for (const variable of variables) {
list.push(variable.identifier);
}
}
function addOneToList(map: Map<string, ts.Identifier[]>, name: string, identifier: ts.Identifier) {
const list = map.get(name);
if (list === undefined) {
map.set(name, [identifier]);
} else {
list.push(identifier);
}
}