noLifecycleCallRule.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 typescript_1 = require("typescript");
  18. var ngWalker_1 = require("./angular/ngWalker");
  19. var utils_1 = require("./util/utils");
  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 walker = new Walker(sourceFile, this.getOptions());
  27. return this.applyWithWalker(walker);
  28. };
  29. Rule.metadata = {
  30. description: 'Disallows explicit calls to lifecycle methods.',
  31. options: null,
  32. optionsDescription: 'Not configurable.',
  33. rationale: "Explicit calls to lifecycle methods could be confusing. Invoke them is an Angular's responsability.",
  34. ruleName: 'no-lifecycle-call',
  35. type: 'functionality',
  36. typescriptOnly: true
  37. };
  38. Rule.FAILURE_STRING = 'Avoid explicit calls to lifecycle methods';
  39. return Rule;
  40. }(rules_1.AbstractRule));
  41. exports.Rule = Rule;
  42. var Walker = (function (_super) {
  43. __extends(Walker, _super);
  44. function Walker() {
  45. return _super !== null && _super.apply(this, arguments) || this;
  46. }
  47. Walker.prototype.visitCallExpression = function (node) {
  48. this.validateCallExpression(node);
  49. _super.prototype.visitCallExpression.call(this, node);
  50. };
  51. Walker.prototype.validateCallExpression = function (node) {
  52. var nodeExpression = node.expression;
  53. if (!typescript_1.isPropertyAccessExpression(nodeExpression))
  54. return;
  55. var expression = nodeExpression.expression, methodName = nodeExpression.name.text;
  56. var isLifecycleCall = utils_1.isAngularLifecycleMethod(methodName);
  57. var isSuperCall = expression.kind === typescript_1.SyntaxKind.SuperKeyword;
  58. if (!isLifecycleCall || isSuperCall)
  59. return;
  60. this.addFailureAtNode(node, Rule.FAILURE_STRING);
  61. };
  62. return Walker;
  63. }(ngWalker_1.NgWalker));