1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const path = require("path");
|
4 | const Lint = require("tslint");
|
5 | const ConfigFactory_1 = require("./config/ConfigFactory");
|
6 | const FilenamesRuleConfig_1 = require("./model/FilenamesRuleConfig");
|
7 | const EnumUtils_1 = require("./utils/EnumUtils");
|
8 | const FilenameCasingUtils_1 = require("./utils/FilenameCasingUtils");
|
9 | const ImportRuleUtils_1 = require("./utils/ImportRuleUtils");
|
10 | exports.FILE_NAMES_RULE_ID = "tsf-folders-file-names";
|
11 |
|
12 |
|
13 |
|
14 | class Rule extends Lint.Rules.AbstractRule {
|
15 | static FAILURE_STRING(expectedCasings) {
|
16 | const stylizedCasings = expectedCasings
|
17 | .map(casing => this.stylizedNameForCasing(casing))
|
18 | .join(" or ");
|
19 |
|
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 | }
|
56 | exports.Rule = Rule;
|
57 |
|
\ | No newline at end of file |