directiveClassSuffixRule.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 ValidatorSuffix = 'Validator';
  21. var Rule = (function (_super) {
  22. __extends(Rule, _super);
  23. function Rule() {
  24. return _super !== null && _super.apply(this, arguments) || this;
  25. }
  26. Rule.validate = function (className, suffixes) {
  27. return suffixes.some(function (s) { return className.endsWith(s); });
  28. };
  29. Rule.prototype.apply = function (sourceFile) {
  30. var walker = new Walker(sourceFile, this.getOptions());
  31. return this.applyWithWalker(walker);
  32. };
  33. Rule.metadata = {
  34. description: 'Classes decorated with @Directive must have suffix "Directive" (or custom) in their name.',
  35. descriptionDetails: 'See more at https://angular.io/styleguide#style-02-03.',
  36. optionExamples: [true, [true, 'Directive', 'MySuffix']],
  37. options: {
  38. items: {
  39. type: 'string'
  40. },
  41. minLength: 0,
  42. type: 'array'
  43. },
  44. optionsDescription: 'Supply a list of allowed component suffixes. Defaults to "Directive".',
  45. rationale: 'Consistent conventions make it easy to quickly identify and reference assets of different types.',
  46. ruleName: 'directive-class-suffix',
  47. type: 'style',
  48. typescriptOnly: true
  49. };
  50. Rule.FAILURE_STRING = 'The name of the class %s should end with the suffix %s (https://angular.io/styleguide#style-02-03)';
  51. return Rule;
  52. }(Lint.Rules.AbstractRule));
  53. exports.Rule = Rule;
  54. var Walker = (function (_super) {
  55. __extends(Walker, _super);
  56. function Walker() {
  57. return _super !== null && _super.apply(this, arguments) || this;
  58. }
  59. Walker.prototype.visitNgDirective = function (metadata) {
  60. var name = metadata.controller.name;
  61. var className = name.text;
  62. var options = this.getOptions();
  63. var suffixes = options.length ? options : ['Directive'];
  64. var declaredInterfaceNames = utils_1.getDeclaredInterfaceNames(metadata.controller);
  65. var hasValidatorInterface = declaredInterfaceNames.some(function (interfaceName) { return interfaceName.endsWith(ValidatorSuffix); });
  66. if (hasValidatorInterface) {
  67. suffixes.push(ValidatorSuffix);
  68. }
  69. if (!Rule.validate(className, suffixes)) {
  70. this.addFailureAtNode(name, sprintf_js_1.sprintf(Rule.FAILURE_STRING, className, suffixes.join(', ')));
  71. }
  72. _super.prototype.visitNgDirective.call(this, metadata);
  73. };
  74. return Walker;
  75. }(ngWalker_1.NgWalker));