componentSelectorRule.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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-07';
  24. var STYLE_GUIDE_STYLE_LINK = 'https://angular.io/guide/styleguide#style-05-02';
  25. var STYLE_GUIDE_TYPE_LINK = 'https://angular.io/guide/styleguide#style-05-03';
  26. var Rule = (function (_super) {
  27. __extends(Rule, _super);
  28. function Rule() {
  29. var _this = _super !== null && _super.apply(this, arguments) || this;
  30. _this.handleType = 'Component';
  31. return _this;
  32. }
  33. Rule.prototype.getPrefixFailure = function (prefixes) {
  34. var prefixStr = prefixes.length === 1 ? '' : ' one of the prefixes:';
  35. return "The selector should be prefixed by" + prefixStr + " \"" + prefixes.join(', ') + "\" (" + STYLE_GUIDE_PREFIX_LINK + ")";
  36. };
  37. Rule.prototype.getStyleFailure = function (style) {
  38. 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";
  39. return "The selector should be " + styleStr + " (" + STYLE_GUIDE_STYLE_LINK + ")";
  40. };
  41. Rule.prototype.getTypeFailure = function (types) {
  42. return "The selector should be used as an " + types.join(' or ') + " (" + STYLE_GUIDE_TYPE_LINK + ")";
  43. };
  44. Rule.prototype.isEnabled = function () {
  45. var _a = Rule.metadata.options, _b = _a.items, enumTypes = _b[0].enum, enumStyles = _b[2].enum, maxLength = _a.maxLength, minLength = _a.minLength;
  46. var argumentsLength = this.ruleArguments.length;
  47. var typeArgument = utils_1.arrayify(this.ruleArguments[0]);
  48. var prefixArgument = utils_1.arrayify(this.ruleArguments[1]).filter(isNotNullOrUndefined_1.isNotNullOrUndefined);
  49. var styleArgument = this.ruleArguments[2];
  50. var argumentsLengthInRange = argumentsLength >= minLength && argumentsLength <= maxLength;
  51. var isTypeArgumentValid = typeArgument.length > 0 && typeArgument.every(function (argument) { return enumTypes.indexOf(argument) !== -1; });
  52. var isPrefixArgumentValid = prefixArgument.length > 0;
  53. var isStyleArgumentValid = enumStyles.indexOf(styleArgument) !== -1;
  54. return _super.prototype.isEnabled.call(this) && argumentsLengthInRange && isTypeArgumentValid && isPrefixArgumentValid && isStyleArgumentValid;
  55. };
  56. Rule.metadata = {
  57. description: 'Component selectors should follow given naming rules.',
  58. descriptionDetails: utils_1.dedent(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n See more at ", ", ", "\n and ", ".\n "], ["\n See more at ", ", ", "\n and ", ".\n "])), STYLE_GUIDE_PREFIX_LINK, STYLE_GUIDE_STYLE_LINK, STYLE_GUIDE_TYPE_LINK),
  59. optionExamples: [
  60. [true, selectorPropertyBase_1.OPTION_TYPE_ELEMENT, 'my-prefix', selectorPropertyBase_1.OPTION_STYLE_KEBAB_CASE],
  61. [true, selectorPropertyBase_1.OPTION_TYPE_ELEMENT, ['ng', 'ngx'], selectorPropertyBase_1.OPTION_STYLE_KEBAB_CASE],
  62. [true, selectorPropertyBase_1.OPTION_TYPE_ATTRIBUTE, 'myPrefix', selectorPropertyBase_1.OPTION_STYLE_CAMEL_CASE],
  63. [true, [selectorPropertyBase_1.OPTION_TYPE_ELEMENT, selectorPropertyBase_1.OPTION_TYPE_ATTRIBUTE], 'myPrefix', selectorPropertyBase_1.OPTION_STYLE_CAMEL_CASE]
  64. ],
  65. options: {
  66. items: [
  67. {
  68. enum: [selectorPropertyBase_1.OPTION_TYPE_ATTRIBUTE, selectorPropertyBase_1.OPTION_TYPE_ELEMENT]
  69. },
  70. {
  71. oneOf: [
  72. {
  73. items: {
  74. type: 'string'
  75. },
  76. type: 'array'
  77. },
  78. {
  79. type: 'string'
  80. }
  81. ]
  82. },
  83. {
  84. enum: [selectorPropertyBase_1.OPTION_STYLE_CAMEL_CASE, selectorPropertyBase_1.OPTION_STYLE_KEBAB_CASE]
  85. }
  86. ],
  87. maxLength: 3,
  88. minLength: 3,
  89. type: 'array'
  90. },
  91. optionsDescription: utils_1.dedent(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n Options accept three obligatory items as an array:\n 1. `", "` or `", "` forces components 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 component selectors.\n 3. `", "` or `", "` allows you to pick a case.\n "], ["\n Options accept three obligatory items as an array:\n 1. \\`", "\\` or \\`", "\\` forces components 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 component 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),
  92. rationale: utils_1.dedent(templateObject_3 || (templateObject_3 = __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 component in other apps.\n * Components are easy to identify in the DOM.\n * Keeps the element names consistent with the specification for Custom Elements.\n * Components have templates containing HTML and optional Angular template syntax.\n * They display content. Developers place components on the page as they would native HTML elements and WebComponents.\n * It is easier to recognize that a symbol is a component 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 component in other apps.\n * Components are easy to identify in the DOM.\n * Keeps the element names consistent with the specification for Custom Elements.\n * Components have templates containing HTML and optional Angular template syntax.\n * They display content. Developers place components on the page as they would native HTML elements and WebComponents.\n * It is easier to recognize that a symbol is a component by looking at the template's HTML.\n "]))),
  93. ruleName: 'component-selector',
  94. type: 'style',
  95. typescriptOnly: true
  96. };
  97. return Rule;
  98. }(selectorPropertyBase_1.SelectorPropertyBase));
  99. exports.Rule = Rule;
  100. var templateObject_1, templateObject_2, templateObject_3;