noOutputOnPrefixRule.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 sprintf_js_1 = require("sprintf-js");
  17. var Lint = require("tslint");
  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: 'Name events without the prefix on.',
  31. descriptionDetails: 'See more at https://angular.io/guide/styleguide#dont-prefix-output-properties.',
  32. options: null,
  33. optionsDescription: 'Not configurable.',
  34. rationale: 'Angular allows for an alternative syntax on-*. If the event itself was prefixed with on this would result in an on-onEvent binding expression.',
  35. ruleName: 'no-output-on-prefix',
  36. type: 'maintainability',
  37. typescriptOnly: true
  38. };
  39. Rule.FAILURE_STRING = 'In the class "%s", the output property "%s" should not be prefixed with on';
  40. return Rule;
  41. }(Lint.Rules.AbstractRule));
  42. exports.Rule = Rule;
  43. var Walker = (function (_super) {
  44. __extends(Walker, _super);
  45. function Walker() {
  46. return _super !== null && _super.apply(this, arguments) || this;
  47. }
  48. Walker.prototype.visitNgOutput = function (property, output, args) {
  49. this.validateOutput(property);
  50. _super.prototype.visitNgOutput.call(this, property, output, args);
  51. };
  52. Walker.prototype.validateOutput = function (property) {
  53. var className = utils_1.getClassName(property);
  54. var memberName = property.name.getText();
  55. if (!memberName || !/^on((?![a-z])|(?=$))/.test(memberName)) {
  56. return;
  57. }
  58. var failure = sprintf_js_1.sprintf(Rule.FAILURE_STRING, className, memberName);
  59. this.addFailureAtNode(property, failure);
  60. };
  61. return Walker;
  62. }(ngWalker_1.NgWalker));