UNPKG

tslint-folders

Version:

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

79 lines (78 loc) 3.57 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 path = require("path"); var Lint = require("tslint"); var ConfigFactory_1 = require("./config/ConfigFactory"); var FilenamesRuleConfig_1 = require("./model/FilenamesRuleConfig"); var RuleId_1 = require("./RuleId"); var EnumUtils_1 = require("./utils/EnumUtils"); var FilenameCasingUtils_1 = require("./utils/FilenameCasingUtils"); var ImportRuleUtils_1 = require("./utils/ImportRuleUtils"); /** Custom version of the standard file-name-casing rule, that allows for *multiple* casings. * ref: https://github.com/palantir/tslint/blob/master/src/rules/fileNameCasingRule.ts */ var Rule = /** @class */ (function (_super) { __extends(Rule, _super); function Rule() { return _super !== null && _super.apply(this, arguments) || this; } Rule.FAILURE_STRING = function (expectedCasings) { var _this = this; var stylizedCasings = expectedCasings .map(function (casing) { return _this.stylizedNameForCasing(casing); }) .join(" or "); // include the rule ID, to make it easier to disable return "File name must be ".concat(stylizedCasings, " (").concat(RuleId_1.RuleId.TsfFoldersFileNames, ")"); }; Rule.stylizedNameForCasing = function (casing) { switch (casing) { case FilenamesRuleConfig_1.Casing.CamelCase: return "camelCase"; case FilenamesRuleConfig_1.Casing.PascalCase: return "PascalCase"; case FilenamesRuleConfig_1.Casing.KebabCase: return "kebab-case"; case FilenamesRuleConfig_1.Casing.SnakeCase: return "snake_case"; default: throw new Error("unhandled casing ".concat(casing)); } }; Rule.prototype.apply = function (sourceFile) { var config = ConfigFactory_1.ConfigFactory.createForFilenames(this.getOptions()); if (ImportRuleUtils_1.ImportRuleUtils.shouldIgnorePath(sourceFile.fileName, config.ignorePaths)) { return []; } var parsedPath = path.parse(sourceFile.fileName); var filename = parsedPath.name; var isAnAllowedCasing = config.casings.some(function (casingName) { var casing = EnumUtils_1.EnumUtils.parseCasing(casingName); var isAllowed = FilenameCasingUtils_1.FilenameCasingUtils.isCased(filename, casing); return isAllowed; }); if (!isAnAllowedCasing) { return [ new Lint.RuleFailure(sourceFile, 0, 0, Rule.FAILURE_STRING(config.casings), RuleId_1.RuleId.TsfFoldersFileNames) ]; } return []; }; return Rule; }(Lint.Rules.AbstractRule)); exports.Rule = Rule;