templateConditionalComplexityRule.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. Object.defineProperty(exports, "__esModule", { value: true });
  16. var compiler_1 = require("@angular/compiler");
  17. var sprintf_js_1 = require("sprintf-js");
  18. var lib_1 = require("tslint/lib");
  19. var ngWalker_1 = require("./angular/ngWalker");
  20. var basicTemplateAstVisitor_1 = require("./angular/templates/basicTemplateAstVisitor");
  21. var Rule = (function (_super) {
  22. __extends(Rule, _super);
  23. function Rule() {
  24. return _super !== null && _super.apply(this, arguments) || this;
  25. }
  26. Rule.prototype.apply = function (sourceFile) {
  27. var walkerConfig = { templateVisitorCtrl: TemplateVisitorCtrl };
  28. var walker = new ngWalker_1.NgWalker(sourceFile, this.getOptions(), walkerConfig);
  29. return this.applyWithWalker(walker);
  30. };
  31. Rule.prototype.isEnabled = function () {
  32. var _a = Rule.metadata.options, maxLength = _a.maxLength, minLength = _a.minLength;
  33. var _b = this.ruleArguments, length = _b.length, maxComplexity = _b[0];
  34. return _super.prototype.isEnabled.call(this) && length >= minLength && length <= maxLength && (maxComplexity === undefined || maxComplexity > 0);
  35. };
  36. Rule.metadata = {
  37. description: "The condition complexity shouldn't exceed a rational limit in a template.",
  38. optionExamples: [true, [true, 4]],
  39. options: {
  40. items: {
  41. type: 'string'
  42. },
  43. maxLength: 1,
  44. minLength: 0,
  45. type: 'array'
  46. },
  47. optionsDescription: 'Determine the maximum number of Boolean operators allowed.',
  48. rationale: 'An important complexity complicates the tests and the maintenance.',
  49. ruleName: 'template-conditional-complexity',
  50. type: 'maintainability',
  51. typescriptOnly: true
  52. };
  53. Rule.DEFAULT_MAX_COMPLEXITY = 3;
  54. Rule.FAILURE_STRING = "The condition complexity (cost '%s') exceeded the defined limit (cost '%s'). The conditional expression should be moved into the component.";
  55. return Rule;
  56. }(lib_1.Rules.AbstractRule));
  57. exports.Rule = Rule;
  58. exports.getFailureMessage = function (totalComplexity, maxComplexity) {
  59. if (maxComplexity === void 0) { maxComplexity = Rule.DEFAULT_MAX_COMPLEXITY; }
  60. return sprintf_js_1.sprintf(Rule.FAILURE_STRING, totalComplexity, maxComplexity);
  61. };
  62. var getTotalComplexity = function (ast) {
  63. var expr = (ast.source || '').replace(/\s/g, '');
  64. var expressionParser = new compiler_1.Parser(new compiler_1.Lexer());
  65. var astWithSource = expressionParser.parseAction(expr, null, 0);
  66. var conditions = [];
  67. var totalComplexity = 0;
  68. var condition = astWithSource.ast;
  69. if (condition.operation) {
  70. totalComplexity++;
  71. conditions.push(condition);
  72. }
  73. while (conditions.length > 0) {
  74. condition = conditions.pop();
  75. if (!condition.operation) {
  76. continue;
  77. }
  78. if (condition.left instanceof compiler_1.Binary) {
  79. totalComplexity++;
  80. conditions.push(condition.left);
  81. }
  82. if (condition.right instanceof compiler_1.Binary) {
  83. conditions.push(condition.right);
  84. }
  85. }
  86. return totalComplexity;
  87. };
  88. var TemplateVisitorCtrl = (function (_super) {
  89. __extends(TemplateVisitorCtrl, _super);
  90. function TemplateVisitorCtrl() {
  91. return _super !== null && _super.apply(this, arguments) || this;
  92. }
  93. TemplateVisitorCtrl.prototype.visitDirectiveProperty = function (prop, context) {
  94. this.validateDirective(prop);
  95. _super.prototype.visitDirectiveProperty.call(this, prop, context);
  96. };
  97. TemplateVisitorCtrl.prototype.validateDirective = function (prop) {
  98. var templateName = prop.templateName, value = prop.value;
  99. if (templateName !== 'ngIf') {
  100. return;
  101. }
  102. var maxComplexity = this.getOptions()[0] || Rule.DEFAULT_MAX_COMPLEXITY;
  103. var totalComplexity = getTotalComplexity(value);
  104. if (totalComplexity <= maxComplexity) {
  105. return;
  106. }
  107. var _a = prop.sourceSpan, endOffset = _a.end.offset, startOffset = _a.start.offset;
  108. this.addFailureFromStartToEnd(startOffset, endOffset, exports.getFailureMessage(totalComplexity, maxComplexity));
  109. };
  110. return TemplateVisitorCtrl;
  111. }(basicTemplateAstVisitor_1.BasicTemplateAstVisitor));