UNPKG

tslint-import-rules

Version:

Set of TSLint rules that help validate proper imports.

46 lines (45 loc) 1.8 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 _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 && (afterNewLineSymbolsCount > 1)) { this.addFailureAtNode(node, 'Unexpected empty line between import declarations'); } _super.prototype.visitImportDeclaration.call(this, node); }; return RuleWalker; }(Lint.RuleWalker));