templateNoCallExpressionRule.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 rules_1 = require("tslint/lib/rules");
  17. var ngWalker_1 = require("./angular/ngWalker");
  18. var basicTemplateAstVisitor_1 = require("./angular/templates/basicTemplateAstVisitor");
  19. var recursiveAngularExpressionVisitor_1 = require("./angular/templates/recursiveAngularExpressionVisitor");
  20. var ALLOWED_METHOD_NAMES = new Set(['$any']);
  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 = {
  28. expressionVisitorCtrl: ExpressionVisitorCtrl,
  29. templateVisitorCtrl: TemplateVisitorCtrl
  30. };
  31. var walker = new ngWalker_1.NgWalker(sourceFile, this.getOptions(), walkerConfig);
  32. return this.applyWithWalker(walker);
  33. };
  34. Rule.metadata = {
  35. description: 'Disallows calling expressions in templates, except for output handlers.',
  36. options: null,
  37. optionsDescription: 'Not configurable.',
  38. rationale: 'Calling expressions in templates causes it to run on every change detection cycle and may cause performance issues.',
  39. ruleName: 'template-no-call-expression',
  40. type: 'maintainability',
  41. typescriptOnly: true
  42. };
  43. Rule.FAILURE_STRING = 'Avoid calling expressions in templates';
  44. return Rule;
  45. }(rules_1.AbstractRule));
  46. exports.Rule = Rule;
  47. var TemplateVisitorCtrl = (function (_super) {
  48. __extends(TemplateVisitorCtrl, _super);
  49. function TemplateVisitorCtrl() {
  50. return _super !== null && _super.apply(this, arguments) || this;
  51. }
  52. TemplateVisitorCtrl.prototype.visitEvent = function () { };
  53. return TemplateVisitorCtrl;
  54. }(basicTemplateAstVisitor_1.BasicTemplateAstVisitor));
  55. var ExpressionVisitorCtrl = (function (_super) {
  56. __extends(ExpressionVisitorCtrl, _super);
  57. function ExpressionVisitorCtrl() {
  58. return _super !== null && _super.apply(this, arguments) || this;
  59. }
  60. ExpressionVisitorCtrl.prototype.visitMethodCall = function (ast, context) {
  61. this.validateMethodCall(ast);
  62. _super.prototype.visitMethodCall.call(this, ast, context);
  63. };
  64. ExpressionVisitorCtrl.prototype.generateFailure = function (ast) {
  65. var _a = ast.span, endSpan = _a.end, startSpan = _a.start;
  66. this.addFailureFromStartToEnd(startSpan, endSpan, Rule.FAILURE_STRING);
  67. };
  68. ExpressionVisitorCtrl.prototype.validateMethodCall = function (ast) {
  69. var isMethodAllowed = ALLOWED_METHOD_NAMES.has(ast.name);
  70. if (isMethodAllowed)
  71. return;
  72. this.generateFailure(ast);
  73. };
  74. return ExpressionVisitorCtrl;
  75. }(recursiveAngularExpressionVisitor_1.RecursiveAngularExpressionVisitor));