@a5sys/angular-tslint-rules
Version:
Linting for Angular applications, adding extra rules to codelyzer
107 lines (106 loc) • 4 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var Lint = require("tslint");
var Rule = (function (_super) {
__extends(Rule, _super);
function Rule() {
return _super !== null && _super.apply(this, arguments) || this;
}
Rule.prototype.apply = function (sourceFile) {
return this.applyWithWalker(new TypescriptAngularFilenameWalker(sourceFile, this.getOptions()));
};
return Rule;
}(Lint.Rules.AbstractRule));
exports.Rule = Rule;
var TypescriptAngularFilenameWalker = (function (_super) {
__extends(TypescriptAngularFilenameWalker, _super);
function TypescriptAngularFilenameWalker(sourceFile, options) {
var _this = _super.call(this, sourceFile, options) || this;
_this.allAllowed = [];
_this.failureMessage = 'The filename must end with:';
_this.prefix = [
'component',
'service',
'module',
'directive',
'model',
'pipe',
'tools'
];
_this.suffix = [
'ts',
'spec.ts',
];
_this.ignore = [
'public_api.ts'
];
_this.computeAllAllowed(options);
_this.failureMessage = _this.failureMessage + _this.allAllowed.join(' OR ');
return _this;
}
TypescriptAngularFilenameWalker.prototype.visitSourceFile = function (node) {
var filename = this.getFilenameFromCompletePath(node.fileName);
var extension = this.getExtension(filename);
if (extension === 'ts') {
if (!this.isFilenameAllowed(filename)) {
this.addFailure(this.createFailure(node.getStart(), node.getEnd(), node.fileName + this.failureMessage));
}
}
_super.prototype.visitSourceFile.call(this, node);
};
TypescriptAngularFilenameWalker.prototype.getFilenameFromCompletePath = function (path) {
var completeFilename = path;
var splitted = completeFilename.split('/');
return splitted.pop();
};
TypescriptAngularFilenameWalker.prototype.getExtension = function (filename) {
var dotSplited = filename.split('.');
return dotSplited.pop();
};
TypescriptAngularFilenameWalker.prototype.isFilenameAllowed = function (filename) {
for (var index = 0; index < this.allAllowed.length; index++) {
var allowed = this.allAllowed[index];
if (filename.includes(allowed)) {
return true;
}
}
return false;
};
TypescriptAngularFilenameWalker.prototype.computeAllAllowed = function (options) {
var _this = this;
var prefix = this.prefix;
var suffix = this.suffix;
var ignore = this.ignore;
if (options.ruleArguments[0]) {
var args = options.ruleArguments[0];
if (args['prefix']) {
prefix = args['prefix'];
}
if (args['suffix']) {
suffix = args['suffix'];
}
if (args['ignore']) {
ignore = args['ignore'];
}
}
prefix.forEach(function (prefixEl) {
suffix.forEach(function (suffixEl) {
_this.allAllowed.push('.' + prefixEl + '.' + suffixEl);
});
});
ignore.forEach(function (value) {
_this.allAllowed.push(value);
});
};
return TypescriptAngularFilenameWalker;
}(Lint.RuleWalker));