interfaceNameRule.js 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 utils_1 = require("../utils");
  24. var OPTION_ALWAYS = "always-prefix";
  25. var OPTION_NEVER = "never-prefix";
  26. var Rule = /** @class */ (function (_super) {
  27. tslib_1.__extends(Rule, _super);
  28. function Rule() {
  29. return _super !== null && _super.apply(this, arguments) || this;
  30. }
  31. Rule.prototype.apply = function (sourceFile) {
  32. return this.applyWithFunction(sourceFile, walk, { never: this.ruleArguments.indexOf(OPTION_NEVER) !== -1 });
  33. };
  34. /* tslint:disable:object-literal-sort-keys */
  35. Rule.metadata = {
  36. ruleName: "interface-name",
  37. description: "Requires interface names to begin with a capital 'I'",
  38. rationale: "Makes it easy to differentiate interfaces from regular classes at a glance.",
  39. 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),
  40. options: {
  41. type: "string",
  42. enum: [OPTION_ALWAYS, OPTION_NEVER],
  43. },
  44. optionExamples: [[true, OPTION_ALWAYS], [true, OPTION_NEVER]],
  45. type: "style",
  46. typescriptOnly: true,
  47. };
  48. /* tslint:enable:object-literal-sort-keys */
  49. Rule.FAILURE_STRING = "interface name must start with a capitalized I";
  50. Rule.FAILURE_STRING_NO_PREFIX = 'interface name must not have an "I" prefix';
  51. return Rule;
  52. }(Lint.Rules.AbstractRule));
  53. exports.Rule = Rule;
  54. function walk(ctx) {
  55. var never = ctx.options.never;
  56. return ts.forEachChild(ctx.sourceFile, function cb(node) {
  57. if (utils.isInterfaceDeclaration(node)) {
  58. var name = node.name;
  59. if (never && hasPrefixI(name.text)) {
  60. ctx.addFailureAtNode(name, Rule.FAILURE_STRING_NO_PREFIX);
  61. }
  62. else if (!never && name.text[0] !== "I") {
  63. ctx.addFailureAtNode(name, Rule.FAILURE_STRING);
  64. }
  65. }
  66. else {
  67. return ts.forEachChild(node, cb);
  68. }
  69. });
  70. }
  71. function hasPrefixI(name) {
  72. // Allow IndexedDB interfaces
  73. return name.length >= 2 && name[0] === "I" && utils_1.isUpperCase(name[1]) && !name.startsWith("IDB");
  74. }
  75. var templateObject_1;