4 * Copyright 2013 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 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);
28 return _super !== null && _super.apply(this, arguments) || this;
30 Rule.prototype.apply = function (sourceFile) {
31 return this.applyWithFunction(sourceFile, walk, {
32 never: this.ruleArguments.indexOf(OPTION_NEVER) !== -1,
35 /* tslint:disable:object-literal-sort-keys */
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),
43 enum: [OPTION_ALWAYS, OPTION_NEVER],
45 optionExamples: [[true, OPTION_ALWAYS], [true, OPTION_NEVER]],
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';
53 }(Lint.Rules.AbstractRule));
56 var never = ctx.options.never;
57 return ts.forEachChild(ctx.sourceFile, function cb(node) {
58 if (utils.isInterfaceDeclaration(node)) {
60 if (!cantDecide(name.text)) {
61 if (never && hasPrefixI(name.text)) {
62 ctx.addFailureAtNode(name, Rule.FAILURE_STRING_NO_PREFIX);
64 else if (!never && !hasPrefixI(name.text)) {
65 ctx.addFailureAtNode(name, Rule.FAILURE_STRING);
70 return ts.forEachChild(node, cb);
74 function hasPrefixI(name) {
75 return name.length >= 3 && name[0] === "I" && /^[A-Z]*$/.test(name[1]);
77 function cantDecide(name) {
80 (name.length === 2 && name[0] === "I" && /^[A-Z]*$/.test(name[1])) ||
84 /^[A-Z]*$/.test(name[1]) &&
85 !/^[a-z]*$/.test(name[2])));