directiveSelectorRule.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. extendStatics(d, b);
  11. function __() { this.constructor = d; }
  12. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  13. };
  14. })();
  15. var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
  16. if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
  17. return cooked;
  18. };
  19. Object.defineProperty(exports, "__esModule", { value: true });
  20. var utils_1 = require("tslint/lib/utils");
  21. var selectorPropertyBase_1 = require("./selectorPropertyBase");
  22. var isNotNullOrUndefined_1 = require("./util/isNotNullOrUndefined");
  23. var STYLE_GUIDE_PREFIX_LINK = 'https://angular.io/guide/styleguide#style-02-08';
  24. var STYLE_GUIDE_STYLE_TYPE_LINK = 'https://angular.io/guide/styleguide#style-02-06';
  25. var Rule = (function (_super) {
  26. __extends(Rule, _super);
  27. function Rule() {
  28. var _this = _super !== null && _super.apply(this, arguments) || this;
  29. _this.handleType = 'Directive';
  30. return _this;
  31. }
  32. Rule.prototype.getPrefixFailure = function (prefixes) {
  33. var prefixStr = prefixes.length === 1 ? '' : ' one of the prefixes:';
  34. return "The selector should be prefixed by" + prefixStr + " \"" + prefixes.join(', ') + "\" (" + STYLE_GUIDE_PREFIX_LINK + ")";
  35. };
  36. Rule.prototype.getStyleFailure = function (style) {
  37. var styleStr = style === selectorPropertyBase_1.OPTION_STYLE_KEBAB_CASE ? selectorPropertyBase_1.OPTION_STYLE_KEBAB_CASE + "d and include a dash" : selectorPropertyBase_1.OPTION_STYLE_CAMEL_CASE + "d";
  38. return "The selector should be " + styleStr + " (" + STYLE_GUIDE_STYLE_TYPE_LINK + ")";
  39. };
  40. Rule.prototype.getTypeFailure = function (types) {
  41. return "The selector should be used as an " + types.join(' or ') + " (" + STYLE_GUIDE_STYLE_TYPE_LINK + ")";
  42. };
  43. Rule.prototype.isEnabled = function () {
  44. var _a = Rule.metadata.options, _b = _a.items, enumTypes = _b[0].enum, enumStyles = _b[2].enum, maxLength = _a.maxLength, minLength = _a.minLength;
  45. var argumentsLength = this.ruleArguments.length;
  46. var typeArgument = utils_1.arrayify(this.ruleArguments[0]);
  47. var prefixArgument = utils_1.arrayify(this.ruleArguments[1]).filter(isNotNullOrUndefined_1.isNotNullOrUndefined);
  48. var styleArgument = this.ruleArguments[2];
  49. var argumentsLengthInRange = argumentsLength >= minLength && argumentsLength <= maxLength;
  50. var isTypeArgumentValid = typeArgument.length > 0 && typeArgument.every(function (argument) { return enumTypes.indexOf(argument) !== -1; });
  51. var isPrefixArgumentValid = prefixArgument.length > 0;
  52. var isStyleArgumentValid = enumStyles.indexOf(styleArgument) !== -1;
  53. return _super.prototype.isEnabled.call(this) && argumentsLengthInRange && isTypeArgumentValid && isPrefixArgumentValid && isStyleArgumentValid;
  54. };
  55. Rule.metadata = {
  56. description: 'Directive selectors should follow given naming rules.',
  57. descriptionDetails: "See more at " + STYLE_GUIDE_STYLE_TYPE_LINK + " and " + STYLE_GUIDE_PREFIX_LINK,
  58. optionExamples: [
  59. [true, selectorPropertyBase_1.OPTION_TYPE_ELEMENT, 'my-prefix', selectorPropertyBase_1.OPTION_STYLE_KEBAB_CASE],
  60. [true, selectorPropertyBase_1.OPTION_TYPE_ELEMENT, ['ng', 'ngx'], selectorPropertyBase_1.OPTION_STYLE_KEBAB_CASE],
  61. [true, selectorPropertyBase_1.OPTION_TYPE_ATTRIBUTE, 'myPrefix', selectorPropertyBase_1.OPTION_STYLE_CAMEL_CASE],
  62. [true, [selectorPropertyBase_1.OPTION_TYPE_ELEMENT, selectorPropertyBase_1.OPTION_TYPE_ATTRIBUTE], 'myPrefix', selectorPropertyBase_1.OPTION_STYLE_CAMEL_CASE]
  63. ],
  64. options: {
  65. items: [
  66. {
  67. enum: [selectorPropertyBase_1.OPTION_TYPE_ATTRIBUTE, selectorPropertyBase_1.OPTION_TYPE_ELEMENT]
  68. },
  69. {
  70. oneOf: [
  71. {
  72. items: {
  73. type: 'string'
  74. },
  75. type: 'array'
  76. },
  77. {
  78. type: 'string'
  79. }
  80. ]
  81. },
  82. {
  83. enum: [selectorPropertyBase_1.OPTION_STYLE_CAMEL_CASE, selectorPropertyBase_1.OPTION_STYLE_KEBAB_CASE]
  84. }
  85. ],
  86. maxLength: 3,
  87. minLength: 3,
  88. type: 'array'
  89. },
  90. optionsDescription: utils_1.dedent(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n Options accept three obligatory items as an array:\n 1. `", "` or `", "` forces directives to be used as either elements, attributes, or both (not recommended)\n 2. A single prefix (string) or array of prefixes (strings) which have to be used in directive selectors.\n 3. `", "` or `", "` allows you to pick a case.\n "], ["\n Options accept three obligatory items as an array:\n 1. \\`", "\\` or \\`", "\\` forces directives to be used as either elements, attributes, or both (not recommended)\n 2. A single prefix (string) or array of prefixes (strings) which have to be used in directive selectors.\n 3. \\`", "\\` or \\`", "\\` allows you to pick a case.\n "])), selectorPropertyBase_1.OPTION_TYPE_ELEMENT, selectorPropertyBase_1.OPTION_TYPE_ATTRIBUTE, selectorPropertyBase_1.OPTION_STYLE_KEBAB_CASE, selectorPropertyBase_1.OPTION_STYLE_CAMEL_CASE),
  91. rationale: utils_1.dedent(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n * Consistent conventions make it easy to quickly identify and reference assets of different types.\n * Makes it easier to promote and share the directive in other apps.\n * Directives are easy to identify in the DOM.\n * Keeps the element names consistent with the specification for Custom Elements.\n * It is easier to recognize that a symbol is a directive by looking at the template's HTML.\n "], ["\n * Consistent conventions make it easy to quickly identify and reference assets of different types.\n * Makes it easier to promote and share the directive in other apps.\n * Directives are easy to identify in the DOM.\n * Keeps the element names consistent with the specification for Custom Elements.\n * It is easier to recognize that a symbol is a directive by looking at the template's HTML.\n "]))),
  92. ruleName: 'directive-selector',
  93. type: 'style',
  94. typescriptOnly: true
  95. };
  96. return Rule;
  97. }(selectorPropertyBase_1.SelectorPropertyBase));
  98. exports.Rule = Rule;
  99. var templateObject_1, templateObject_2;