UNPKG

6.97 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = Object.setPrototypeOf ||
4 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6 return function (d, b) {
7 extendStatics(d, b);
8 function __() { this.constructor = d; }
9 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10 };
11})();
12Object.defineProperty(exports, "__esModule", { value: true });
13var ts = require("typescript");
14var Lint = require("tslint");
15var ErrorTolerantWalker_1 = require("./utils/ErrorTolerantWalker");
16var Utils_1 = require("./utils/Utils");
17var AstUtils_1 = require("./utils/AstUtils");
18var Rule = (function (_super) {
19 __extends(Rule, _super);
20 function Rule() {
21 return _super !== null && _super.apply(this, arguments) || this;
22 }
23 Rule.prototype.apply = function (sourceFile) {
24 return this.applyWithWalker(new ExportNameWalker(sourceFile, this.getOptions()));
25 };
26 Rule.getExceptions = function (options) {
27 if (options.ruleArguments instanceof Array) {
28 return options.ruleArguments[0];
29 }
30 if (options instanceof Array) {
31 return options;
32 }
33 return null;
34 };
35 Rule.metadata = {
36 ruleName: 'export-name',
37 type: 'maintainability',
38 description: 'The name of the exported module must match the filename of the source file',
39 options: null,
40 optionsDescription: '',
41 typescriptOnly: true,
42 issueClass: 'Ignored',
43 issueType: 'Warning',
44 severity: 'Low',
45 level: 'Opportunity for Excellence',
46 group: 'Clarity',
47 commonWeaknessEnumeration: '710'
48 };
49 Rule.FAILURE_STRING = 'The exported module or identifier name must match the file name. Found: ';
50 return Rule;
51}(Lint.Rules.AbstractRule));
52exports.Rule = Rule;
53var ExportNameWalker = (function (_super) {
54 __extends(ExportNameWalker, _super);
55 function ExportNameWalker() {
56 return _super !== null && _super.apply(this, arguments) || this;
57 }
58 ExportNameWalker.prototype.visitSourceFile = function (node) {
59 var _this = this;
60 var singleExport = node.statements.filter(function (element) {
61 return element.kind === ts.SyntaxKind.ExportAssignment;
62 });
63 if (singleExport.length === 1) {
64 var exportAssignment = singleExport[0];
65 this.validateExport(exportAssignment.expression.getText(), exportAssignment.expression);
66 return;
67 }
68 var exportedTopLevelElements = [];
69 node.statements.forEach(function (element) {
70 var exportStatements = _this.getExportStatements(element);
71 exportedTopLevelElements = exportedTopLevelElements.concat(exportStatements);
72 });
73 if (exportedTopLevelElements.length === 0) {
74 node.statements.forEach(function (element) {
75 if (element.kind === ts.SyntaxKind.ModuleDeclaration) {
76 var exportStatements = _this.getExportStatementsWithinModules(element);
77 exportedTopLevelElements = exportedTopLevelElements.concat(exportStatements);
78 }
79 });
80 }
81 this.validateExportedElements(exportedTopLevelElements);
82 };
83 ExportNameWalker.prototype.getExportStatementsWithinModules = function (moduleDeclaration) {
84 var _this = this;
85 if (moduleDeclaration.body.kind === ts.SyntaxKind.ModuleDeclaration) {
86 return this.getExportStatementsWithinModules(moduleDeclaration.body);
87 }
88 else if (moduleDeclaration.body.kind === ts.SyntaxKind.ModuleBlock) {
89 var exportStatements_1 = [];
90 var moduleBlock = moduleDeclaration.body;
91 moduleBlock.statements.forEach(function (element) {
92 exportStatements_1 = exportStatements_1.concat(_this.getExportStatements(element));
93 });
94 return exportStatements_1;
95 }
96 return null;
97 };
98 ExportNameWalker.prototype.getExportStatements = function (element) {
99 var exportStatements = [];
100 if (element.kind === ts.SyntaxKind.ExportAssignment) {
101 var exportAssignment = element;
102 this.validateExport(exportAssignment.expression.getText(), exportAssignment.expression);
103 }
104 else if (AstUtils_1.AstUtils.hasModifier(element.modifiers, ts.SyntaxKind.ExportKeyword)) {
105 exportStatements.push(element);
106 }
107 return exportStatements;
108 };
109 ExportNameWalker.prototype.validateExportedElements = function (exportedElements) {
110 if (exportedElements.length === 1) {
111 if (exportedElements[0].kind === ts.SyntaxKind.ModuleDeclaration ||
112 exportedElements[0].kind === ts.SyntaxKind.ClassDeclaration ||
113 exportedElements[0].kind === ts.SyntaxKind.FunctionDeclaration) {
114 this.validateExport(exportedElements[0].name.text, exportedElements[0]);
115 }
116 else if (exportedElements[0].kind === ts.SyntaxKind.VariableStatement) {
117 var variableStatement = exportedElements[0];
118 if (variableStatement.declarationList.declarations.length === 1) {
119 var variableDeclaration = variableStatement.declarationList.declarations[0];
120 this.validateExport(variableDeclaration.name.text, variableDeclaration);
121 }
122 }
123 }
124 };
125 ExportNameWalker.prototype.validateExport = function (exportedName, node) {
126 var regex = new RegExp(exportedName + '\..*');
127 if (!regex.test(this.getFilename())) {
128 if (!this.isSuppressed(exportedName)) {
129 var failureString = Rule.FAILURE_STRING + this.getSourceFile().fileName + ' and ' + exportedName;
130 this.addFailureAt(node.getStart(), node.getWidth(), failureString);
131 }
132 }
133 };
134 ExportNameWalker.prototype.getFilename = function () {
135 var filename = this.getSourceFile().fileName;
136 var lastSlash = filename.lastIndexOf('/');
137 if (lastSlash > -1) {
138 return filename.substring(lastSlash + 1);
139 }
140 return filename;
141 };
142 ExportNameWalker.prototype.isSuppressed = function (exportedName) {
143 var allExceptions = Rule.getExceptions(this.getOptions());
144 return Utils_1.Utils.exists(allExceptions, function (exception) {
145 return new RegExp(exception).test(exportedName);
146 });
147 };
148 return ExportNameWalker;
149}(ErrorTolerantWalker_1.ErrorTolerantWalker));
150exports.ExportNameWalker = ExportNameWalker;
151//# sourceMappingURL=exportNameRule.js.map
\No newline at end of file