useInjectableProvidedInRule.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 Rule = (function (_super) {
  19. __extends(Rule, _super);
  20. function Rule() {
  21. return _super !== null && _super.apply(this, arguments) || this;
  22. }
  23. Rule.prototype.apply = function (sourceFile) {
  24. var walker = new Walker(sourceFile, this.getOptions());
  25. return this.applyWithWalker(walker);
  26. };
  27. Rule.metadata = {
  28. description: "Enforces classes decorated with @Injectable to use the 'providedIn' property.",
  29. options: null,
  30. optionsDescription: 'Not configurable.',
  31. rationale: "Using the 'providedIn' property makes classes decorated with @Injectable tree shakeable.",
  32. ruleName: 'use-injectable-provided-in',
  33. type: 'functionality',
  34. typescriptOnly: true
  35. };
  36. Rule.FAILURE_STRING = "Classes decorated with @Injectable should use the 'providedIn' property";
  37. return Rule;
  38. }(rules_1.AbstractRule));
  39. exports.Rule = Rule;
  40. var Walker = (function (_super) {
  41. __extends(Walker, _super);
  42. function Walker() {
  43. return _super !== null && _super.apply(this, arguments) || this;
  44. }
  45. Walker.prototype.visitNgInjectable = function (metadata) {
  46. this.validateInjectable(metadata);
  47. _super.prototype.visitNgInjectable.call(this, metadata);
  48. };
  49. Walker.prototype.validateInjectable = function (metadata) {
  50. if (metadata.providedIn)
  51. return;
  52. this.addFailureAtNode(metadata.decorator, Rule.FAILURE_STRING);
  53. };
  54. return Walker;
  55. }(ngWalker_1.NgWalker));