preferOnPushComponentChangeDetectionRule.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 rules_1 = require("tslint/lib/rules");
  21. var utils_1 = require("tslint/lib/utils");
  22. var typescript_1 = require("typescript/lib/typescript");
  23. var ngWalker_1 = require("./angular/ngWalker");
  24. var utils_2 = require("./util/utils");
  25. var ON_PUSH = 'OnPush';
  26. var Rule = (function (_super) {
  27. __extends(Rule, _super);
  28. function Rule() {
  29. return _super !== null && _super.apply(this, arguments) || this;
  30. }
  31. Rule.prototype.apply = function (sourceFile) {
  32. var walker = new Walker(sourceFile, this.getOptions());
  33. return this.applyWithWalker(walker);
  34. };
  35. Rule.metadata = {
  36. description: "Enforces component's change detection to ChangeDetectionStrategy." + ON_PUSH + ".",
  37. options: null,
  38. optionsDescription: 'Not configurable.',
  39. rationale: utils_1.dedent(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n By default Angular uses the ChangeDetectionStrategy.Default.\n\n This strategy doesn\u2019t assume anything about the application, therefore every time something changes in our application, as a result of various user events, timers, XHR, promises, etc., a change detection will run on all components.\n\n By using ChangeDetectionStrategy.", ", Angular will only run the change detection cycle in the following cases:\n * Inputs references change.\n * An event originated from the component or one of its children.\n * If manually called.\n "], ["\n By default Angular uses the ChangeDetectionStrategy.Default.\n\n This strategy doesn\u2019t assume anything about the application, therefore every time something changes in our application, as a result of various user events, timers, XHR, promises, etc., a change detection will run on all components.\n\n By using ChangeDetectionStrategy.", ", Angular will only run the change detection cycle in the following cases:\n * Inputs references change.\n * An event originated from the component or one of its children.\n * If manually called.\n "])), ON_PUSH),
  40. ruleName: 'prefer-on-push-component-change-detection',
  41. type: 'functionality',
  42. typescriptOnly: true
  43. };
  44. Rule.FAILURE_STRING = "The changeDetection value of a component should be set to ChangeDetectionStrategy." + ON_PUSH;
  45. return Rule;
  46. }(rules_1.AbstractRule));
  47. exports.Rule = Rule;
  48. var Walker = (function (_super) {
  49. __extends(Walker, _super);
  50. function Walker() {
  51. return _super !== null && _super.apply(this, arguments) || this;
  52. }
  53. Walker.prototype.visitNgComponent = function (metadata) {
  54. this.validateComponent(metadata);
  55. _super.prototype.visitNgComponent.call(this, metadata);
  56. };
  57. Walker.prototype.validateComponent = function (metadata) {
  58. var metadataDecorator = metadata.decorator;
  59. var changeDetectionExpression = utils_2.getDecoratorPropertyInitializer(metadataDecorator, 'changeDetection');
  60. if (!changeDetectionExpression) {
  61. this.addFailureAtNode(metadataDecorator, Rule.FAILURE_STRING);
  62. }
  63. else if (typescript_1.isPropertyAccessExpression(changeDetectionExpression) && changeDetectionExpression.name.text !== ON_PUSH) {
  64. this.addFailureAtNode(changeDetectionExpression, Rule.FAILURE_STRING);
  65. }
  66. };
  67. return Walker;
  68. }(ngWalker_1.NgWalker));
  69. var templateObject_1;