4 * Copyright 2018 Palantir Technologies, Inc.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 Object.defineProperty(exports, "__esModule", { value: true });
19 var tslib_1 = require("tslib");
20 var path = require("path");
21 var error_1 = require("../error");
22 var Lint = require("../index");
23 var utils_1 = require("../utils");
26 Casing["CamelCase"] = "camel-case";
27 Casing["PascalCase"] = "pascal-case";
28 Casing["Ignored"] = "ignore";
29 Casing["KebabCase"] = "kebab-case";
30 Casing["SnakeCase"] = "snake-case";
31 })(Casing || (Casing = {}));
39 var validCasingOptions = new Set(rules);
40 function isCorrectCasing(fileName, casing) {
42 case Casing.CamelCase:
43 return utils_1.isCamelCased(fileName);
44 case Casing.PascalCase:
45 return utils_1.isPascalCased(fileName);
48 case Casing.KebabCase:
49 return utils_1.isKebabCased(fileName);
50 case Casing.SnakeCase:
51 return utils_1.isSnakeCased(fileName);
54 var getValidRegExp = function (regExpString) {
56 return RegExp(regExpString, "i");
62 var validateWithRegexConfig = function (sourceFile, casingConfig) {
63 var fileBaseName = path.parse(sourceFile.fileName).base;
64 var fileNameMatches = Object.keys(casingConfig);
65 if (fileNameMatches.length === 0) {
66 Rule.showWarning("At least one file name match must be provided");
69 for (var _i = 0, fileNameMatches_1 = fileNameMatches; _i < fileNameMatches_1.length; _i++) {
70 var rawMatcher = fileNameMatches_1[_i];
71 var regex = getValidRegExp(rawMatcher);
72 if (regex === undefined) {
73 Rule.showWarning("Invalid regular expression provided: " + rawMatcher);
76 var casing = casingConfig[rawMatcher];
77 if (!validCasingOptions.has(casing)) {
78 Rule.showWarning("Unexpected casing option provided: " + casing);
81 if (!regex.test(fileBaseName)) {
84 return isCorrectCasing(fileBaseName, casing) ? undefined : casing;
88 var validateWithSimpleConfig = function (sourceFile, casingConfig) {
89 if (!validCasingOptions.has(casingConfig)) {
90 Rule.showWarning("Unexpected casing option provided: " + casingConfig);
93 var fileName = path.parse(sourceFile.fileName).name;
94 var isValid = isCorrectCasing(fileName, casingConfig);
95 return isValid ? undefined : casingConfig;
97 var validate = function (sourceFile, casingConfig) {
98 if (casingConfig === undefined) {
99 Rule.showWarning("Provide a rule option as string or object");
102 if (typeof casingConfig === "string") {
103 return validateWithSimpleConfig(sourceFile, casingConfig);
105 if (typeof casingConfig === "object") {
106 return validateWithRegexConfig(sourceFile, casingConfig);
108 Rule.showWarning("Received unexpected rule option");
111 var Rule = /** @class */ (function (_super) {
112 tslib_1.__extends(Rule, _super);
114 return _super !== null && _super.apply(this, arguments) || this;
116 /* tslint:enable:object-literal-sort-keys */
117 Rule.showWarning = function (message) {
118 error_1.showWarningOnce("Warning: " + Rule.metadata.ruleName + " - " + message);
120 Rule.FAILURE_STRING = function (expectedCasing) {
121 return "File name must be " + Rule.stylizedNameForCasing(expectedCasing);
123 Rule.stylizedNameForCasing = function (casing) {
125 case Casing.CamelCase:
127 case Casing.PascalCase:
131 case Casing.KebabCase:
133 case Casing.SnakeCase:
137 Rule.prototype.apply = function (sourceFile) {
138 if (this.ruleArguments.length !== 1) {
141 var casingConfig = this.ruleArguments[0];
142 var validation = validate(sourceFile, casingConfig);
143 return validation === undefined
146 new Lint.RuleFailure(sourceFile, 0, 0, Rule.FAILURE_STRING(validation), this.ruleName),
149 /* tslint:disable:object-literal-sort-keys */
151 ruleName: "file-name-casing",
152 description: "Enforces a consistent file naming convention",
153 rationale: "Helps maintain a consistent style across a file hierarchy",
154 optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n One of the following arguments must be provided:\n\n * `", "`: File names must be camel-cased: `fileName.ts`.\n * `", "`: File names must be Pascal-cased: `FileName.ts`.\n * `", "`: File names must be kebab-cased: `file-name.ts`.\n * `", "`: File names must be snake-cased: `file_name.ts`.\n * `", "`: File names are ignored _(useful for the object configuration)_.\n\n Or an object, where the key represents a regular expression that\n matches the file name, and the value is the file name rule from\n the previous list.\n\n * { \".tsx\": \"", "\", \".ts\": \"", "\" }\n "], ["\n One of the following arguments must be provided:\n\n * \\`", "\\`: File names must be camel-cased: \\`fileName.ts\\`.\n * \\`", "\\`: File names must be Pascal-cased: \\`FileName.ts\\`.\n * \\`", "\\`: File names must be kebab-cased: \\`file-name.ts\\`.\n * \\`", "\\`: File names must be snake-cased: \\`file_name.ts\\`.\n * \\`", "\\`: File names are ignored _(useful for the object configuration)_.\n\n Or an object, where the key represents a regular expression that\n matches the file name, and the value is the file name rule from\n the previous list.\n\n * \\{ \\\".tsx\\\": \\\"", "\\\", \\\".ts\\\": \\\"", "\\\" \\}\n "])), Casing.CamelCase, Casing.PascalCase, Casing.KebabCase, Casing.SnakeCase, Casing.Ignored, Casing.PascalCase, Casing.CamelCase),
170 additionalProperties: {
180 [true, Casing.CamelCase],
181 [true, Casing.PascalCase],
182 [true, Casing.KebabCase],
183 [true, Casing.SnakeCase],
187 ".tsx": Casing.PascalCase,
188 ".ts": Casing.CamelCase,
194 ".style.ts": Casing.KebabCase,
195 ".tsx": Casing.PascalCase,
196 ".*": Casing.CamelCase,
202 ".ts": Casing.Ignored,
203 ".tsx": Casing.PascalCase,
209 typescriptOnly: false,
212 }(Lint.Rules.AbstractRule));
214 var templateObject_1;