preferOutputReadonlyRule.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 lib_1 = require("tslint/lib");
  17. var typescript_1 = require("typescript/lib/typescript");
  18. var ngWalker_1 = require("./angular/ngWalker");
  19. var Rule = (function (_super) {
  20. __extends(Rule, _super);
  21. function Rule() {
  22. return _super !== null && _super.apply(this, arguments) || this;
  23. }
  24. Rule.prototype.apply = function (sourceFile) {
  25. var walker = new Walker(sourceFile, this.getOptions());
  26. return this.applyWithWalker(walker);
  27. };
  28. Rule.metadata = {
  29. description: 'Prefer to declare `@Output` as readonly since they are not supposed to be reassigned.',
  30. options: null,
  31. optionsDescription: 'Not configurable.',
  32. ruleName: 'prefer-output-readonly',
  33. type: 'maintainability',
  34. typescriptOnly: true
  35. };
  36. Rule.FAILURE_STRING = 'Prefer to declare `@Output` as readonly since they are not supposed to be reassigned';
  37. return Rule;
  38. }(lib_1.Rules.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.visitNgOutput = function (property, output, args) {
  46. this.validateOutput(property);
  47. _super.prototype.visitNgOutput.call(this, property, output, args);
  48. };
  49. Walker.prototype.validateOutput = function (property) {
  50. if (property.modifiers && property.modifiers.some(function (m) { return m.kind === typescript_1.SyntaxKind.ReadonlyKeyword; })) {
  51. return;
  52. }
  53. this.addFailureAtNode(property.name, Rule.FAILURE_STRING);
  54. };
  55. return Walker;
  56. }(ngWalker_1.NgWalker));