contextualLifecycleRule.js 4.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. Object.defineProperty(exports, "__esModule", { value: true });
  20. var sprintf_js_1 = require("sprintf-js");
  21. var rules_1 = require("tslint/lib/rules");
  22. var utils_1 = require("tslint/lib/utils");
  23. var ngWalker_1 = require("./angular/ngWalker");
  24. var utils_2 = require("./util/utils");
  25. exports.getFailureMessage = function (failureParameters) {
  26. return sprintf_js_1.sprintf(Rule.FAILURE_STRING, failureParameters.methodName, failureParameters.className, failureParameters.decoratorName);
  27. };
  28. var Rule = (function (_super) {
  29. __extends(Rule, _super);
  30. function Rule() {
  31. return _super !== null && _super.apply(this, arguments) || this;
  32. }
  33. Rule.prototype.apply = function (sourceFile) {
  34. var walker = new Walker(sourceFile, this.getOptions());
  35. return this.applyWithWalker(walker);
  36. };
  37. Rule.metadata = {
  38. description: 'Ensures that classes use allowed lifecycle method in its body.',
  39. options: null,
  40. optionsDescription: 'Not configurable.',
  41. rationale: utils_1.dedent(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n Some lifecycle methods can only be used in certain class types.\n For example, ", "() method should not be used\n in an @", " class.\n "], ["\n Some lifecycle methods can only be used in certain class types.\n For example, ", "() method should not be used\n in an @", " class.\n "])), utils_2.AngularLifecycleMethods.ngOnInit, utils_2.AngularClassDecorators.Injectable),
  42. ruleName: 'contextual-lifecycle',
  43. type: 'functionality',
  44. typescriptOnly: true
  45. };
  46. Rule.FAILURE_STRING = 'The method "%s" is not allowed for class "%s" because it is decorated with "%s"';
  47. return Rule;
  48. }(rules_1.AbstractRule));
  49. exports.Rule = Rule;
  50. var Walker = (function (_super) {
  51. __extends(Walker, _super);
  52. function Walker() {
  53. return _super !== null && _super.apply(this, arguments) || this;
  54. }
  55. Walker.prototype.visitNgInjectable = function (metadata) {
  56. var injectableAllowedMethods = utils_2.ANGULAR_CLASS_DECORATOR_LIFECYCLE_METHOD_MAPPER.get(utils_2.AngularClassDecorators.Injectable);
  57. if (!injectableAllowedMethods)
  58. return;
  59. this.validateDecorator(metadata, injectableAllowedMethods);
  60. _super.prototype.visitNgInjectable.call(this, metadata);
  61. };
  62. Walker.prototype.visitNgModule = function (metadata) {
  63. var ngModuleAllowedMethods = utils_2.ANGULAR_CLASS_DECORATOR_LIFECYCLE_METHOD_MAPPER.get(utils_2.AngularClassDecorators.NgModule);
  64. if (!ngModuleAllowedMethods)
  65. return;
  66. this.validateDecorator(metadata, ngModuleAllowedMethods);
  67. _super.prototype.visitNgModule.call(this, metadata);
  68. };
  69. Walker.prototype.visitNgPipe = function (metadata) {
  70. var pipeAllowedMethods = utils_2.ANGULAR_CLASS_DECORATOR_LIFECYCLE_METHOD_MAPPER.get(utils_2.AngularClassDecorators.Pipe);
  71. if (!pipeAllowedMethods)
  72. return;
  73. this.validateDecorator(metadata, pipeAllowedMethods);
  74. _super.prototype.visitNgPipe.call(this, metadata);
  75. };
  76. Walker.prototype.validateDecorator = function (metadata, allowedMethods) {
  77. var className = utils_2.getClassName(metadata.controller);
  78. var decoratorName = utils_2.getDecoratorName(metadata.decorator);
  79. if (!decoratorName || !utils_2.isAngularClassDecorator(decoratorName))
  80. return;
  81. for (var _i = 0, _a = metadata.controller.members; _i < _a.length; _i++) {
  82. var member = _a[_i];
  83. var memberName = member.name;
  84. if (!memberName)
  85. continue;
  86. var methodName = memberName.getText();
  87. if (!utils_2.isAngularLifecycleMethod(methodName) || allowedMethods.has(methodName))
  88. continue;
  89. var failure = exports.getFailureMessage({ className: className, decoratorName: decoratorName, methodName: methodName });
  90. this.addFailureAtNode(member, failure);
  91. }
  92. };
  93. return Walker;
  94. }(ngWalker_1.NgWalker));
  95. var templateObject_1;