UNPKG

tslint-import-rules

Version:

Set of TSLint rules that help validate proper imports.

54 lines (53 loc) 2.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var Lint = require("tslint"); var nonSpaceSymbolPattern = /\S/; var Rule = (function (_super) { tslib_1.__extends(Rule, _super); function Rule() { return _super !== null && _super.apply(this, arguments) || this; } Rule.prototype.apply = function (sourceFile) { return this.applyWithWalker(new RuleWalker(sourceFile, this.getOptions())); }; return Rule; }(Lint.Rules.AbstractRule)); exports.Rule = Rule; var RuleWalker = (function (_super) { tslib_1.__extends(RuleWalker, _super); function RuleWalker(sourceFile, options) { var _this = _super.call(this, sourceFile, options) || this; _this.sourceText = sourceFile.getText(); _this.sourceTextLength = sourceFile.getEnd(); return _this; } RuleWalker.prototype.visitImportDeclaration = function (node) { var options = this.getOptions(); var always = options[0] !== 'never'; var count = Object.assign({ count: 1 }, options[1]).count; var _a = this, sourceText = _a.sourceText, sourceTextLength = _a.sourceTextLength; var isAfterImportDeclaration = false; var afterNewLineSymbolsCount = 0; for (var i = node.getEnd(); i < sourceTextLength; i++) { var ch = sourceText[i]; if (ch === '\n') { afterNewLineSymbolsCount++; } else if (nonSpaceSymbolPattern.test(ch)) { isAfterImportDeclaration = sourceText.slice(i, i + 6) === 'import'; break; } } if (!isAfterImportDeclaration) { if (always && (count !== afterNewLineSymbolsCount - 1)) { this.addFailureAtNode(node, "Expected " + count + " empty line" + (count > 1 ? 's' : '') + " after import declaration"); } else if (!always && afterNewLineSymbolsCount > 1) { this.addFailureAtNode(node, 'Unexpected empty line after import declaration'); } } _super.prototype.visitImportDeclaration.call(this, node); }; return RuleWalker; }(Lint.RuleWalker));