1 | var __extends = (this && this.__extends) || function (d, b) {
|
2 | for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
|
3 | function __() { this.constructor = d; }
|
4 | __.prototype = b.prototype;
|
5 | d.prototype = new __();
|
6 | };
|
7 | var SyntaxKind = require('./utils/SyntaxKind');
|
8 | var ErrorTolerantWalker = require('./utils/ErrorTolerantWalker');
|
9 | var Rule = (function (_super) {
|
10 | __extends(Rule, _super);
|
11 | function Rule() {
|
12 | _super.apply(this, arguments);
|
13 | }
|
14 | Rule.prototype.apply = function (sourceFile) {
|
15 | var documentRegistry = ts.createDocumentRegistry();
|
16 | var languageServiceHost = Lint.createLanguageServiceHost('file.ts', sourceFile.getFullText());
|
17 | var languageService = ts.createLanguageService(languageServiceHost, documentRegistry);
|
18 | var referencedDotImports = this.getReferencedDotImports(sourceFile, this.getOptions());
|
19 | return this.applyWithWalker(new NoUnusedImportsWalker(sourceFile, this.getOptions(), languageService, referencedDotImports));
|
20 | };
|
21 | Rule.prototype.getReferencedDotImports = function (sourceFile, options) {
|
22 | var gatherImportHandler = new GatherNoRequireImportsWalker(sourceFile, options);
|
23 | this.applyWithWalker(gatherImportHandler);
|
24 | return gatherImportHandler.noRequireReferences;
|
25 | };
|
26 | Rule.FAILURE_STRING = 'unused import: ';
|
27 | return Rule;
|
28 | })(Lint.Rules.AbstractRule);
|
29 | exports.Rule = Rule;
|
30 | var GatherNoRequireImportsWalker = (function (_super) {
|
31 | __extends(GatherNoRequireImportsWalker, _super);
|
32 | function GatherNoRequireImportsWalker() {
|
33 | _super.apply(this, arguments);
|
34 | this.noRequireReferences = {};
|
35 | }
|
36 | GatherNoRequireImportsWalker.prototype.visitImportEqualsDeclaration = function (node) {
|
37 | var moduleReference = node.moduleReference;
|
38 | if (moduleReference.kind === SyntaxKind.current().QualifiedName) {
|
39 | if (moduleReference.left != null) {
|
40 | this.gatherReferenceFromImport(moduleReference.left.text);
|
41 | }
|
42 | }
|
43 | _super.prototype.visitImportEqualsDeclaration.call(this, node);
|
44 | };
|
45 | GatherNoRequireImportsWalker.prototype.gatherReferenceFromImport = function (qualifiedName) {
|
46 | var _this = this;
|
47 | if (qualifiedName) {
|
48 | qualifiedName.split('.').forEach(function (name) {
|
49 | _this.noRequireReferences[name] = true;
|
50 | });
|
51 | }
|
52 | };
|
53 | return GatherNoRequireImportsWalker;
|
54 | })(Lint.RuleWalker);
|
55 | var NoUnusedImportsWalker = (function (_super) {
|
56 | __extends(NoUnusedImportsWalker, _super);
|
57 | function NoUnusedImportsWalker(sourceFile, options, languageServices, noRequireReferences) {
|
58 | _super.call(this, sourceFile, options);
|
59 | this.noRequireReferences = {};
|
60 | this.languageServices = languageServices;
|
61 | this.noRequireReferences = noRequireReferences;
|
62 | }
|
63 | NoUnusedImportsWalker.prototype.visitImportEqualsDeclaration = function (node) {
|
64 | if (!this.hasModifier(node.modifiers, SyntaxKind.current().ExportKeyword)) {
|
65 | this.validateReferencesForVariable(node.name.text, node.name.getStart());
|
66 | }
|
67 | _super.prototype.visitImportEqualsDeclaration.call(this, node);
|
68 | };
|
69 | NoUnusedImportsWalker.prototype.hasModifier = function (modifiers, modifierKind) {
|
70 | if (modifiers == null) {
|
71 | return false;
|
72 | }
|
73 | var result = false;
|
74 | modifiers.forEach(function (modifier) {
|
75 | if (modifier.kind === modifierKind) {
|
76 | result = true;
|
77 | }
|
78 | });
|
79 | return result;
|
80 | };
|
81 | NoUnusedImportsWalker.prototype.validateReferencesForVariable = function (name, position) {
|
82 | var references = this.languageServices.getReferencesAtPosition('file.ts', position);
|
83 | if (references.length <= 1 && !this.noRequireReferences[name]) {
|
84 | var failureString = Rule.FAILURE_STRING + '\'' + name + '\'';
|
85 | var failure = this.createFailure(position, name.length, failureString);
|
86 | this.addFailure(failure);
|
87 | }
|
88 | };
|
89 | return NoUnusedImportsWalker;
|
90 | })(ErrorTolerantWalker);
|
91 |
|
\ | No newline at end of file |