noUseBeforeDeclareRule.js
5.24 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
"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 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 NoUseBeforeDeclareWalker(sourceFile, this.getOptions(), languageService));
};
Rule.metadata = {
ruleName: "no-use-before-declare",
description: "Disallows usage of variables before their declaration.",
descriptionDetails: (_a = ["\n This rule is primarily useful when using the `var` keyword -\n the compiler will detect if a `let` and `const` variable is used before it is declared."], _a.raw = ["\n This rule is primarily useful when using the \\`var\\` keyword -\n the compiler will detect if a \\`let\\` and \\`const\\` variable is used before it is declared."], Lint.Utils.dedent(_a)),
optionsDescription: "Not configurable.",
options: null,
optionExamples: ["true"],
type: "functionality",
};
Rule.FAILURE_STRING_PREFIX = "variable '";
Rule.FAILURE_STRING_POSTFIX = "' used before declaration";
return Rule;
var _a;
}(Lint.Rules.AbstractRule));
exports.Rule = Rule;
var NoUseBeforeDeclareWalker = (function (_super) {
__extends(NoUseBeforeDeclareWalker, _super);
function NoUseBeforeDeclareWalker(sourceFile, options, languageService) {
_super.call(this, sourceFile, options);
this.languageService = languageService;
}
NoUseBeforeDeclareWalker.prototype.createScope = function () {
return {};
};
NoUseBeforeDeclareWalker.prototype.visitBindingElement = function (node) {
var isSingleVariable = node.name.kind === ts.SyntaxKind.Identifier;
var isBlockScoped = Lint.isBlockScopedBindingElement(node);
if (isSingleVariable && !isBlockScoped) {
var variableName = node.name.text;
this.validateUsageForVariable(variableName, node.name.getStart());
}
_super.prototype.visitBindingElement.call(this, node);
};
NoUseBeforeDeclareWalker.prototype.visitImportDeclaration = function (node) {
var importClause = node.importClause;
if (importClause != null && importClause.name != null) {
var variableIdentifier = importClause.name;
this.validateUsageForVariable(variableIdentifier.text, variableIdentifier.getStart());
}
_super.prototype.visitImportDeclaration.call(this, node);
};
NoUseBeforeDeclareWalker.prototype.visitImportEqualsDeclaration = function (node) {
var name = node.name;
this.validateUsageForVariable(name.text, name.getStart());
_super.prototype.visitImportEqualsDeclaration.call(this, node);
};
NoUseBeforeDeclareWalker.prototype.visitNamedImports = function (node) {
for (var _i = 0, _a = node.elements; _i < _a.length; _i++) {
var namedImport = _a[_i];
this.validateUsageForVariable(namedImport.name.text, namedImport.name.getStart());
}
_super.prototype.visitNamedImports.call(this, node);
};
NoUseBeforeDeclareWalker.prototype.visitNamespaceImport = function (node) {
this.validateUsageForVariable(node.name.text, node.name.getStart());
_super.prototype.visitNamespaceImport.call(this, node);
};
NoUseBeforeDeclareWalker.prototype.visitVariableDeclaration = function (node) {
var isSingleVariable = node.name.kind === ts.SyntaxKind.Identifier;
var variableName = node.name.text;
var currentScope = this.getCurrentScope();
if (isSingleVariable && currentScope[variableName] == null) {
this.validateUsageForVariable(variableName, node.getStart());
}
currentScope[variableName] = true;
_super.prototype.visitVariableDeclaration.call(this, node);
};
NoUseBeforeDeclareWalker.prototype.validateUsageForVariable = function (name, position) {
var fileName = this.getSourceFile().fileName;
var highlights = this.languageService.getDocumentHighlights(fileName, position, [fileName]);
if (highlights != null) {
for (var _i = 0, highlights_1 = highlights; _i < highlights_1.length; _i++) {
var highlight = highlights_1[_i];
for (var _a = 0, _b = highlight.highlightSpans; _a < _b.length; _a++) {
var highlightSpan = _b[_a];
var referencePosition = highlightSpan.textSpan.start;
if (referencePosition < position) {
var failureString = Rule.FAILURE_STRING_PREFIX + name + Rule.FAILURE_STRING_POSTFIX;
this.addFailure(this.createFailure(referencePosition, name.length, failureString));
}
}
}
}
};
return NoUseBeforeDeclareWalker;
}(Lint.ScopeAwareRuleWalker));