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 pathnoUseBeforeDeclareRule.ts
103 lines (85 loc) · 4.11 KB
/
noUseBeforeDeclareRule.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
/*
* 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.
*/
export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING_PREFIX = "variable '";
public static FAILURE_STRING_POSTFIX = "' used before declaration";
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 NoUseBeforeDeclareWalker(sourceFile, this.getOptions(), languageService));
}
}
type VisitedVariables = {[varName: string]: boolean};
class NoUseBeforeDeclareWalker extends Lint.ScopeAwareRuleWalker<VisitedVariables> {
private languageService: ts.LanguageService;
constructor(sourceFile: ts.SourceFile, options: Lint.IOptions, languageService: ts.LanguageService) {
super(sourceFile, options);
this.languageService = languageService;
}
public createScope(): VisitedVariables {
return {};
}
public visitImportDeclaration(node: ts.ImportDeclaration) {
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.validateUsageForVariable(variableIdentifier.text, variableIdentifier.getStart());
}
super.visitImportDeclaration(node);
}
public visitImportEqualsDeclaration(node: ts.ImportEqualsDeclaration) {
const name = <ts.Identifier> node.name;
this.validateUsageForVariable(name.text, name.getStart());
super.visitImportEqualsDeclaration(node);
}
public visitNamedImports(node: ts.NamedImports) {
for (let namedImport of node.elements) {
this.validateUsageForVariable(namedImport.name.text, namedImport.name.getStart());
}
super.visitNamedImports(node);
}
public visitNamespaceImport(node: ts.NamespaceImport) {
this.validateUsageForVariable(node.name.text, node.name.getStart());
super.visitNamespaceImport(node);
}
public visitVariableDeclaration(node: ts.VariableDeclaration) {
const isSingleVariable = node.name.kind === ts.SyntaxKind.Identifier;
const nameNode = <ts.Identifier> node.name;
const variableName = nameNode.text;
const currentScope = this.getCurrentScope();
// only validate on the first variable declaration within the current scope
if (currentScope[variableName] == null) {
this.validateUsageForVariable(variableName, node.getStart());
}
currentScope[variableName] = true;
super.visitVariableDeclaration(node);
}
private validateUsageForVariable(name: string, position: number) {
const highlights = this.languageService.getDocumentHighlights("file.ts", position, ["file.ts"]);
for (let highlight of highlights) {
for (let highlightSpan of highlight.highlightSpans) {
const referencePosition = highlightSpan.textSpan.start;
if (referencePosition < position) {
const failureString = Rule.FAILURE_STRING_PREFIX + name + Rule.FAILURE_STRING_POSTFIX;
this.addFailure(this.createFailure(referencePosition, name.length, failureString));
}
}
}
}
}