componentMaxInlineDeclarationsRule.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. var _a, _b;
  20. Object.defineProperty(exports, "__esModule", { value: true });
  21. var sprintf_js_1 = require("sprintf-js");
  22. var rules_1 = require("tslint/lib/rules");
  23. var utils_1 = require("tslint/lib/utils");
  24. var ngWalker_1 = require("./angular/ngWalker");
  25. var DEFAULT_ANIMATIONS_LIMIT = 15;
  26. var DEFAULT_STYLES_LIMIT = 3;
  27. var DEFAULT_TEMPLATE_LIMIT = 3;
  28. var OPTION_ANIMATIONS = 'animations';
  29. var OPTION_STYLES = 'styles';
  30. var OPTION_TEMPLATE = 'template';
  31. var STYLE_GUIDE_LINK = 'https://angular.io/guide/styleguide#style-05-04.';
  32. var generateFailure = function (type, limit, value) { return sprintf_js_1.sprintf(Rule.FAILURE_STRING, type, limit, value); };
  33. exports.getAnimationsFailure = function (value, limit) {
  34. if (limit === void 0) { limit = DEFAULT_ANIMATIONS_LIMIT; }
  35. return generateFailure(OPTION_ANIMATIONS, limit, value);
  36. };
  37. exports.getStylesFailure = function (value, limit) {
  38. if (limit === void 0) { limit = DEFAULT_STYLES_LIMIT; }
  39. return generateFailure(OPTION_STYLES, limit, value);
  40. };
  41. exports.getTemplateFailure = function (value, limit) {
  42. if (limit === void 0) { limit = DEFAULT_TEMPLATE_LIMIT; }
  43. return generateFailure(OPTION_TEMPLATE, limit, value);
  44. };
  45. var Rule = (function (_super) {
  46. __extends(Rule, _super);
  47. function Rule() {
  48. return _super !== null && _super.apply(this, arguments) || this;
  49. }
  50. Rule.prototype.apply = function (sourceFile) {
  51. var walker = new Walker(sourceFile, this.getOptions());
  52. return this.applyWithWalker(walker);
  53. };
  54. Rule.prototype.isEnabled = function () {
  55. var _a = Rule.metadata.options, maxLength = _a.maxLength, minLength = _a.minLength;
  56. var length = this.ruleArguments.length;
  57. return _super.prototype.isEnabled.call(this) && length >= minLength && length <= maxLength;
  58. };
  59. Rule.metadata = {
  60. description: 'Disallows having too many lines in inline template and styles. Forces separate template or styles file creation.',
  61. descriptionDetails: "See more at " + STYLE_GUIDE_LINK + ".",
  62. optionExamples: [true, [true, (_a = {}, _a[OPTION_ANIMATIONS] = 20, _a[OPTION_STYLES] = 8, _a[OPTION_TEMPLATE] = 5, _a)]],
  63. options: {
  64. items: {
  65. properties: (_b = {},
  66. _b[OPTION_ANIMATIONS] = {
  67. type: 'number'
  68. },
  69. _b[OPTION_STYLES] = {
  70. type: 'number'
  71. },
  72. _b[OPTION_TEMPLATE] = {
  73. type: 'number'
  74. },
  75. _b),
  76. type: 'object'
  77. },
  78. maxLength: 1,
  79. minLength: 0,
  80. type: 'array'
  81. },
  82. optionsDescription: utils_1.dedent(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n It can take an optional object with the properties '", "', '", "' and '", "':\n * `", "` - number > 0 defining the maximum allowed inline lines for animations. Defaults to ", ".\n * `", "` - number > 0 defining the maximum allowed inline lines for styles. Defaults to ", ".\n * `", "` - number > 0 defining the maximum allowed inline lines for template. Defaults to ", ".\n "], ["\n It can take an optional object with the properties '", "', '", "' and '", "':\n * \\`", "\\` - number > 0 defining the maximum allowed inline lines for animations. Defaults to ", ".\n * \\`", "\\` - number > 0 defining the maximum allowed inline lines for styles. Defaults to ", ".\n * \\`", "\\` - number > 0 defining the maximum allowed inline lines for template. Defaults to ", ".\n "])), OPTION_ANIMATIONS, OPTION_STYLES, OPTION_TEMPLATE, OPTION_ANIMATIONS, DEFAULT_ANIMATIONS_LIMIT, OPTION_STYLES, DEFAULT_STYLES_LIMIT, OPTION_TEMPLATE, DEFAULT_TEMPLATE_LIMIT),
  83. rationale: "Large, inline templates and styles obscure the component's purpose and implementation, reducing readability and maintainability.",
  84. ruleName: 'component-max-inline-declarations',
  85. type: 'maintainability',
  86. typescriptOnly: true
  87. };
  88. Rule.FAILURE_STRING = "Exceeds the maximum allowed inline lines for %s. Defined limit: %s / total lines: %s (" + STYLE_GUIDE_LINK + ")";
  89. return Rule;
  90. }(rules_1.AbstractRule));
  91. exports.Rule = Rule;
  92. var Walker = (function (_super) {
  93. __extends(Walker, _super);
  94. function Walker(sourceFile, options) {
  95. var _this = _super.call(this, sourceFile, options) || this;
  96. _this.animationsLinesLimit = DEFAULT_ANIMATIONS_LIMIT;
  97. _this.stylesLinesLimit = DEFAULT_STYLES_LIMIT;
  98. _this.templateLinesLimit = DEFAULT_TEMPLATE_LIMIT;
  99. _this.newLineRegExp = /\r\n|\r|\n/;
  100. var _a = (options.ruleArguments[0] || []), _b = _a.animations, animations = _b === void 0 ? -1 : _b, _c = _a.styles, styles = _c === void 0 ? -1 : _c, _d = _a.template, template = _d === void 0 ? -1 : _d;
  101. _this.animationsLinesLimit = animations > -1 ? animations : _this.animationsLinesLimit;
  102. _this.stylesLinesLimit = styles > -1 ? styles : _this.stylesLinesLimit;
  103. _this.templateLinesLimit = template > -1 ? template : _this.templateLinesLimit;
  104. return _this;
  105. }
  106. Walker.prototype.visitNgComponent = function (metadata) {
  107. this.validateInlineAnimations(metadata);
  108. this.validateInlineStyles(metadata);
  109. this.validateInlineTemplate(metadata);
  110. _super.prototype.visitNgComponent.call(this, metadata);
  111. };
  112. Walker.prototype.getLinesCount = function (source) {
  113. return source.trim().split(this.newLineRegExp).length;
  114. };
  115. Walker.prototype.getInlineAnimationsLinesCount = function (metadata) {
  116. var _this = this;
  117. return (metadata.animations || []).reduce(function (previousValue, currentValue) {
  118. if (currentValue && currentValue.animation) {
  119. previousValue += _this.getLinesCount(currentValue.animation.source);
  120. }
  121. return previousValue;
  122. }, 0);
  123. };
  124. Walker.prototype.validateInlineAnimations = function (metadata) {
  125. var linesCount = this.getInlineAnimationsLinesCount(metadata);
  126. if (linesCount <= this.animationsLinesLimit)
  127. return;
  128. var failureMessage = exports.getAnimationsFailure(linesCount, this.animationsLinesLimit);
  129. for (var _i = 0, _a = metadata.animations; _i < _a.length; _i++) {
  130. var animation = _a[_i];
  131. this.addFailureAtNode(animation.node, failureMessage);
  132. }
  133. };
  134. Walker.prototype.getInlineStylesLinesCount = function (metadata) {
  135. var _this = this;
  136. return (metadata.styles || []).reduce(function (previousValue, currentValue) {
  137. if (currentValue && !currentValue.url) {
  138. previousValue += _this.getLinesCount(currentValue.style.source);
  139. }
  140. return previousValue;
  141. }, 0);
  142. };
  143. Walker.prototype.validateInlineStyles = function (metadata) {
  144. var linesCount = this.getInlineStylesLinesCount(metadata);
  145. if (linesCount <= this.stylesLinesLimit)
  146. return;
  147. var failureMessage = exports.getStylesFailure(linesCount, this.stylesLinesLimit);
  148. for (var _i = 0, _a = metadata.styles; _i < _a.length; _i++) {
  149. var style = _a[_i];
  150. this.addFailureAtNode(style.node, failureMessage);
  151. }
  152. };
  153. Walker.prototype.getTemplateLinesCount = function (metadata) {
  154. return this.hasInlineTemplate(metadata) ? this.getLinesCount(metadata.template.template.source) : 0;
  155. };
  156. Walker.prototype.hasInlineTemplate = function (metadata) {
  157. return !!(metadata.template && !metadata.template.url && metadata.template.template && metadata.template.template.source);
  158. };
  159. Walker.prototype.validateInlineTemplate = function (metadata) {
  160. var linesCount = this.getTemplateLinesCount(metadata);
  161. if (linesCount <= this.templateLinesLimit)
  162. return;
  163. var failureMessage = exports.getTemplateFailure(linesCount, this.templateLinesLimit);
  164. this.addFailureAtNode(metadata.template.node, failureMessage);
  165. };
  166. return Walker;
  167. }(ngWalker_1.NgWalker));
  168. var templateObject_1;