templateCyclomaticComplexityRule.js 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 sprintf_js_1 = require("sprintf-js");
  17. var lib_1 = require("tslint/lib");
  18. var ngWalker_1 = require("./angular/ngWalker");
  19. var basicTemplateAstVisitor_1 = require("./angular/templates/basicTemplateAstVisitor");
  20. var Rule = (function (_super) {
  21. __extends(Rule, _super);
  22. function Rule() {
  23. return _super !== null && _super.apply(this, arguments) || this;
  24. }
  25. Rule.prototype.apply = function (sourceFile) {
  26. var walkerConfig = { templateVisitorCtrl: TemplateVisitorCtrl };
  27. var walker = new ngWalker_1.NgWalker(sourceFile, this.getOptions(), walkerConfig);
  28. return this.applyWithWalker(walker);
  29. };
  30. Rule.prototype.isEnabled = function () {
  31. var _a = Rule.metadata.options, maxLength = _a.maxLength, minLength = _a.minLength;
  32. var _b = this.ruleArguments, length = _b.length, maxComplexity = _b[0];
  33. return _super.prototype.isEnabled.call(this) && length >= minLength && length <= maxLength && (maxComplexity === undefined || maxComplexity > 0);
  34. };
  35. Rule.metadata = {
  36. description: "Checks cyclomatic complexity against a specified limit. It is a quantitative measure of the number of linearly independent paths through a program's source code",
  37. optionExamples: [true, [true, 6]],
  38. options: {
  39. items: {
  40. type: 'string'
  41. },
  42. maxLength: 1,
  43. minLength: 0,
  44. type: 'array'
  45. },
  46. optionsDescription: 'Determine the maximum number of the cyclomatic complexity.',
  47. rationale: 'Cyclomatic complexity over some threshold indicates that the logic should be moved outside the template.',
  48. ruleName: 'template-cyclomatic-complexity',
  49. type: 'maintainability',
  50. typescriptOnly: true
  51. };
  52. Rule.FAILURE_STRING = "The cyclomatic complexity exceeded the defined limit (cost '%s'). Your template should be refactored.";
  53. Rule.DEFAULT_MAX_COMPLEXITY = 5;
  54. return Rule;
  55. }(lib_1.Rules.AbstractRule));
  56. exports.Rule = Rule;
  57. exports.getFailureMessage = function (maxComplexity) {
  58. if (maxComplexity === void 0) { maxComplexity = Rule.DEFAULT_MAX_COMPLEXITY; }
  59. return sprintf_js_1.sprintf(Rule.FAILURE_STRING, maxComplexity);
  60. };
  61. var TemplateVisitorCtrl = (function (_super) {
  62. __extends(TemplateVisitorCtrl, _super);
  63. function TemplateVisitorCtrl() {
  64. var _this = _super !== null && _super.apply(this, arguments) || this;
  65. _this.totalComplexity = 0;
  66. return _this;
  67. }
  68. TemplateVisitorCtrl.prototype.visitDirectiveProperty = function (prop, context) {
  69. this.validateDirective(prop);
  70. _super.prototype.visitDirectiveProperty.call(this, prop, context);
  71. };
  72. TemplateVisitorCtrl.prototype.validateDirective = function (prop) {
  73. var pattern = /^ng(ForOf|If|Switch(Case|Default))$/;
  74. var templateName = prop.templateName;
  75. if (pattern.test(templateName)) {
  76. this.totalComplexity++;
  77. }
  78. var maxComplexity = this.getOptions()[0] || Rule.DEFAULT_MAX_COMPLEXITY;
  79. if (this.totalComplexity <= maxComplexity) {
  80. return;
  81. }
  82. var _a = prop.sourceSpan, endOffset = _a.end.offset, startOffset = _a.start.offset;
  83. this.addFailureFromStartToEnd(startOffset, endOffset, exports.getFailureMessage(maxComplexity));
  84. };
  85. return TemplateVisitorCtrl;
  86. }(basicTemplateAstVisitor_1.BasicTemplateAstVisitor));