UNPKG

2.56 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const path = require("path");
4const Lint = require("tslint");
5const ConfigFactory_1 = require("./config/ConfigFactory");
6const FilenamesRuleConfig_1 = require("./model/FilenamesRuleConfig");
7const EnumUtils_1 = require("./utils/EnumUtils");
8const FilenameCasingUtils_1 = require("./utils/FilenameCasingUtils");
9const ImportRuleUtils_1 = require("./utils/ImportRuleUtils");
10exports.FILE_NAMES_RULE_ID = "tsf-folders-file-names";
11/** Custom version of the standard file-name-casing rule, that allows for *multiple* casings.
12 * ref: https://github.com/palantir/tslint/blob/master/src/rules/fileNameCasingRule.ts
13 */
14class Rule extends Lint.Rules.AbstractRule {
15 static FAILURE_STRING(expectedCasings) {
16 const stylizedCasings = expectedCasings
17 .map(casing => this.stylizedNameForCasing(casing))
18 .join(" or ");
19 // include the rule ID, to make it easier to disable
20 return `File name must be ${stylizedCasings} (${exports.FILE_NAMES_RULE_ID})`;
21 }
22 static stylizedNameForCasing(casing) {
23 switch (casing) {
24 case FilenamesRuleConfig_1.Casing.CamelCase:
25 return "camelCase";
26 case FilenamesRuleConfig_1.Casing.PascalCase:
27 return "PascalCase";
28 case FilenamesRuleConfig_1.Casing.KebabCase:
29 return "kebab-case";
30 case FilenamesRuleConfig_1.Casing.SnakeCase:
31 return "snake_case";
32 default:
33 throw new Error(`unhandled casing ${casing}`);
34 }
35 }
36 apply(sourceFile) {
37 const config = ConfigFactory_1.ConfigFactory.createForFilenames(this.getOptions());
38 if (ImportRuleUtils_1.ImportRuleUtils.shouldIgnorePath(sourceFile.fileName, config.ignorePaths)) {
39 return [];
40 }
41 const parsedPath = path.parse(sourceFile.fileName);
42 const filename = parsedPath.name;
43 const isAnAllowedCasing = config.casings.some(casingName => {
44 const casing = EnumUtils_1.EnumUtils.parseCasing(casingName);
45 const isAllowed = FilenameCasingUtils_1.FilenameCasingUtils.isCased(filename, casing);
46 return isAllowed;
47 });
48 if (!isAnAllowedCasing) {
49 return [
50 new Lint.RuleFailure(sourceFile, 0, 0, Rule.FAILURE_STRING(config.casings), exports.FILE_NAMES_RULE_ID)
51 ];
52 }
53 return [];
54 }
55}
56exports.Rule = Rule;
57//# sourceMappingURL=tsfFoldersFileNamesRule.js.map
\No newline at end of file