.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / fileNameCasingRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2018 Palantir Technologies, Inc.
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
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");
24 var Casing;
25 (function (Casing) {
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 = {}));
32 var rules = [
33     Casing.CamelCase,
34     Casing.Ignored,
35     Casing.PascalCase,
36     Casing.KebabCase,
37     Casing.SnakeCase,
38 ];
39 var validCasingOptions = new Set(rules);
40 function isCorrectCasing(fileName, casing) {
41     switch (casing) {
42         case Casing.CamelCase:
43             return utils_1.isCamelCased(fileName);
44         case Casing.PascalCase:
45             return utils_1.isPascalCased(fileName);
46         case Casing.Ignored:
47             return true;
48         case Casing.KebabCase:
49             return utils_1.isKebabCased(fileName);
50         case Casing.SnakeCase:
51             return utils_1.isSnakeCased(fileName);
52     }
53 }
54 var getValidRegExp = function (regExpString) {
55     try {
56         return RegExp(regExpString, "i");
57     }
58     catch (_a) {
59         return undefined;
60     }
61 };
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");
67         return undefined;
68     }
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);
74             continue;
75         }
76         var casing = casingConfig[rawMatcher];
77         if (!validCasingOptions.has(casing)) {
78             Rule.showWarning("Unexpected casing option provided: " + casing);
79             continue;
80         }
81         if (!regex.test(fileBaseName)) {
82             continue;
83         }
84         return isCorrectCasing(fileBaseName, casing) ? undefined : casing;
85     }
86     return undefined;
87 };
88 var validateWithSimpleConfig = function (sourceFile, casingConfig) {
89     if (!validCasingOptions.has(casingConfig)) {
90         Rule.showWarning("Unexpected casing option provided: " + casingConfig);
91         return undefined;
92     }
93     var fileName = path.parse(sourceFile.fileName).name;
94     var isValid = isCorrectCasing(fileName, casingConfig);
95     return isValid ? undefined : casingConfig;
96 };
97 var validate = function (sourceFile, casingConfig) {
98     if (casingConfig === undefined) {
99         Rule.showWarning("Provide a rule option as string or object");
100         return undefined;
101     }
102     if (typeof casingConfig === "string") {
103         return validateWithSimpleConfig(sourceFile, casingConfig);
104     }
105     if (typeof casingConfig === "object") {
106         return validateWithRegexConfig(sourceFile, casingConfig);
107     }
108     Rule.showWarning("Received unexpected rule option");
109     return undefined;
110 };
111 var Rule = /** @class */ (function (_super) {
112     tslib_1.__extends(Rule, _super);
113     function Rule() {
114         return _super !== null && _super.apply(this, arguments) || this;
115     }
116     /* tslint:enable:object-literal-sort-keys */
117     Rule.showWarning = function (message) {
118         error_1.showWarningOnce("Warning: " + Rule.metadata.ruleName + " - " + message);
119     };
120     Rule.FAILURE_STRING = function (expectedCasing) {
121         return "File name must be " + Rule.stylizedNameForCasing(expectedCasing);
122     };
123     Rule.stylizedNameForCasing = function (casing) {
124         switch (casing) {
125             case Casing.CamelCase:
126                 return "camelCase";
127             case Casing.PascalCase:
128                 return "PascalCase";
129             case Casing.Ignored:
130                 return "ignored";
131             case Casing.KebabCase:
132                 return "kebab-case";
133             case Casing.SnakeCase:
134                 return "snake_case";
135         }
136     };
137     Rule.prototype.apply = function (sourceFile) {
138         if (this.ruleArguments.length !== 1) {
139             return [];
140         }
141         var casingConfig = this.ruleArguments[0];
142         var validation = validate(sourceFile, casingConfig);
143         return validation === undefined
144             ? []
145             : [
146                 new Lint.RuleFailure(sourceFile, 0, 0, Rule.FAILURE_STRING(validation), this.ruleName),
147             ];
148     };
149     /* tslint:disable:object-literal-sort-keys */
150     Rule.metadata = {
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),
155         options: {
156             type: "array",
157             items: {
158                 anyOf: [
159                     {
160                         type: "array",
161                         items: [
162                             {
163                                 type: "string",
164                                 enum: rules,
165                             },
166                         ],
167                     },
168                     {
169                         type: "object",
170                         additionalProperties: {
171                             type: "string",
172                             enum: rules,
173                         },
174                         minProperties: 1,
175                     },
176                 ],
177             },
178         },
179         optionExamples: [
180             [true, Casing.CamelCase],
181             [true, Casing.PascalCase],
182             [true, Casing.KebabCase],
183             [true, Casing.SnakeCase],
184             [
185                 true,
186                 {
187                     ".tsx": Casing.PascalCase,
188                     ".ts": Casing.CamelCase,
189                 },
190             ],
191             [
192                 true,
193                 {
194                     ".style.ts": Casing.KebabCase,
195                     ".tsx": Casing.PascalCase,
196                     ".*": Casing.CamelCase,
197                 },
198             ],
199             [
200                 true,
201                 {
202                     ".ts": Casing.Ignored,
203                     ".tsx": Casing.PascalCase,
204                 },
205             ],
206         ],
207         hasFix: false,
208         type: "style",
209         typescriptOnly: false,
210     };
211     return Rule;
212 }(Lint.Rules.AbstractRule));
213 exports.Rule = Rule;
214 var templateObject_1;