UNPKG

tslint-folders

Version:

Custom TSLint rules for checking imports between packages and their folders, and generating relevant diagrams.

71 lines (70 loc) 2.98 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.Rule = void 0; var Lint = require("tslint"); var ts = require("typescript"); var RuleId_1 = require("./RuleId"); var Rule = /** @class */ (function (_super) { __extends(Rule, _super); function Rule() { return _super !== null && _super.apply(this, arguments) || this; } Rule.prototype.apply = function (sourceFile) { return this.applyWithFunction(sourceFile, walk, this.getDisallowedFolders()); }; Rule.prototype.getDisallowedFolders = function () { var options = this.getOptions().ruleArguments; if (options.length !== 1) { throw new Error("tslint rule is misconfigured (".concat(RuleId_1.RuleId.TsfFoldersImportFromDisallowedFolders, ")")); } return options[0]; }; // include the rule ID, to make it easier to disable Rule.FAILURE_STRING = "do not import from invalid folder like 'DISALLOWED_TEXT' (".concat(RuleId_1.RuleId.TsfFoldersImportFromDisallowedFolders, ")"); return Rule; }(Lint.Rules.AbstractRule)); exports.Rule = Rule; var walk = function (ctx) { return ts.forEachChild(ctx.sourceFile, checkNode); function checkNode(node) { if (node.kind === ts.SyntaxKind.ImportDeclaration) { visitImportDeclaration(node, ctx); } else if (node.kind === ts.SyntaxKind.ImportEqualsDeclaration) { visitImportEqualsDeclaration(node, ctx); } return ts.forEachChild(node, checkNode); } }; function visitImportDeclaration(node, ctx) { validate(node, node.moduleSpecifier.getText(), ctx); } function visitImportEqualsDeclaration(node, ctx) { validate(node, node.moduleReference.getText(), ctx); } function validate(node, text, ctx) { var disallowedTexts = ctx.options.disallowed; for (var _i = 0, disallowedTexts_1 = disallowedTexts; _i < disallowedTexts_1.length; _i++) { var disallowed = disallowedTexts_1[_i]; if (text.indexOf(disallowed) >= 0) { ctx.addFailureAtNode(node, Rule.FAILURE_STRING.replace("DISALLOWED_TEXT", disallowed)); // one error per line is enough! return; } } }