.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / interfaceNameRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2013 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 utils = require("tsutils");
21 var ts = require("typescript");
22 var Lint = require("../index");
23 var OPTION_ALWAYS = "always-prefix";
24 var OPTION_NEVER = "never-prefix";
25 var Rule = /** @class */ (function (_super) {
26     tslib_1.__extends(Rule, _super);
27     function Rule() {
28         return _super !== null && _super.apply(this, arguments) || this;
29     }
30     Rule.prototype.apply = function (sourceFile) {
31         return this.applyWithFunction(sourceFile, walk, {
32             never: this.ruleArguments.indexOf(OPTION_NEVER) !== -1,
33         });
34     };
35     /* tslint:disable:object-literal-sort-keys */
36     Rule.metadata = {
37         ruleName: "interface-name",
38         description: "Requires interface names to begin with a capital 'I'",
39         rationale: "Makes it easy to differentiate interfaces from regular classes at a glance.",
40         optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n            One of the following two options must be provided:\n\n            * `\"", "\"` requires interface names to start with an \"I\"\n            * `\"", "\"` requires interface names to not have an \"I\" prefix"], ["\n            One of the following two options must be provided:\n\n            * \\`\"", "\"\\` requires interface names to start with an \"I\"\n            * \\`\"", "\"\\` requires interface names to not have an \"I\" prefix"])), OPTION_ALWAYS, OPTION_NEVER),
41         options: {
42             type: "string",
43             enum: [OPTION_ALWAYS, OPTION_NEVER],
44         },
45         optionExamples: [[true, OPTION_ALWAYS], [true, OPTION_NEVER]],
46         type: "style",
47         typescriptOnly: true,
48     };
49     /* tslint:enable:object-literal-sort-keys */
50     Rule.FAILURE_STRING = "interface name must start with a capitalized I";
51     Rule.FAILURE_STRING_NO_PREFIX = 'interface name must not have an "I" prefix';
52     return Rule;
53 }(Lint.Rules.AbstractRule));
54 exports.Rule = Rule;
55 function walk(ctx) {
56     var never = ctx.options.never;
57     return ts.forEachChild(ctx.sourceFile, function cb(node) {
58         if (utils.isInterfaceDeclaration(node)) {
59             var name = node.name;
60             if (!cantDecide(name.text)) {
61                 if (never && hasPrefixI(name.text)) {
62                     ctx.addFailureAtNode(name, Rule.FAILURE_STRING_NO_PREFIX);
63                 }
64                 else if (!never && !hasPrefixI(name.text)) {
65                     ctx.addFailureAtNode(name, Rule.FAILURE_STRING);
66                 }
67             }
68         }
69         else {
70             return ts.forEachChild(node, cb);
71         }
72     });
73 }
74 function hasPrefixI(name) {
75     return name.length >= 3 && name[0] === "I" && /^[A-Z]*$/.test(name[1]);
76 }
77 function cantDecide(name) {
78     return (
79     // Case ID
80     (name.length === 2 && name[0] === "I" && /^[A-Z]*$/.test(name[1])) ||
81         // Case IDB or ID42
82         (name.length >= 2 &&
83             name[0] === "I" &&
84             /^[A-Z]*$/.test(name[1]) &&
85             !/^[a-z]*$/.test(name[2])));
86 }
87 var templateObject_1;