This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
/
noVarRule.ts
60 lines (51 loc) · 1.99 KB
/
noVarRule.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
import * as Lint from "tslint";
import * as ts from "typescript";
// TODO: pull request to update tslint's `no-var-keyword`
export class Rule extends Lint.Rules.AbstractRule {
static metadata: Lint.IRuleMetadata = {
ruleName: "no-var",
description: "Forbids 'var'.",
optionsDescription: "Not configurable.",
options: null,
type: "style",
typescriptOnly: false,
};
static FAILURE_STRING = "Do not use 'var'.";
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithFunction(sourceFile, walk);
}
}
function walk(ctx: Lint.WalkContext<void>): void {
const { sourceFile } = ctx;
ts.forEachChild(ctx.sourceFile, function cb(node: ts.Node) {
switch (node.kind) {
case ts.SyntaxKind.VariableStatement:
if (!Lint.hasModifier(node.modifiers, ts.SyntaxKind.DeclareKeyword)
&& !Lint.isBlockScopedVariable(node as ts.VariableStatement)
// Global 'var' declaration OK.
&& !(node.parent!.kind === ts.SyntaxKind.ModuleBlock && isDeclareGlobal(node.parent!.parent!))
&& !(node.parent === sourceFile && !ts.isExternalModule(sourceFile) && sourceFile.isDeclarationFile)) {
ctx.addFailureAtNode(node, Rule.FAILURE_STRING);
}
break;
case ts.SyntaxKind.ForStatement:
case ts.SyntaxKind.ForInStatement:
case ts.SyntaxKind.ForOfStatement: {
const { initializer } = node as ts.ForStatement | ts.ForInStatement | ts.ForOfStatement;
if (initializer && initializer.kind === ts.SyntaxKind.VariableDeclarationList &&
// tslint:disable-next-line no-bitwise
!Lint.isNodeFlagSet(initializer, ts.NodeFlags.Let | ts.NodeFlags.Const)) {
ctx.addFailureAtNode(initializer, Rule.FAILURE_STRING);
}
break;
}
}
ts.forEachChild(node, cb);
});
}
function isDeclareGlobal(node: ts.Node) {
return isModuleDeclaration(node) && node.name.kind === ts.SyntaxKind.Identifier && node.name.text === "global";
}
function isModuleDeclaration(node: ts.Node): node is ts.ModuleDeclaration {
return node.kind === ts.SyntaxKind.ModuleDeclaration;
}