componentClassSuffixRule.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 function_1 = require("./util/function");
  19. var walkerFactory_1 = require("./walkerFactory/walkerFactory");
  20. var walkerFn_1 = require("./walkerFactory/walkerFn");
  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, suffixList) {
  27. return suffixList.some(function (suffix) { return className.endsWith(suffix); });
  28. };
  29. Rule.prototype.apply = function (sourceFile) {
  30. var walker = Rule.walkerBuilder(sourceFile, this.getOptions());
  31. return this.applyWithWalker(walker);
  32. };
  33. Rule.metadata = {
  34. description: 'Classes decorated with @Component must have suffix "Component" (or custom) in their name.',
  35. descriptionDetails: 'See more at https://angular.io/styleguide#style-02-03.',
  36. optionExamples: [true, [true, 'Component', 'View']],
  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 "Component".',
  45. rationale: 'Consistent conventions make it easy to quickly identify and reference assets of different types.',
  46. ruleName: 'component-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. Rule.walkerBuilder = walkerFn_1.all(walkerFn_1.validateComponent(function (meta, suffixList) {
  52. if (suffixList === void 0) { suffixList = []; }
  53. return function_1.Maybe.lift(meta.controller)
  54. .fmap(function (controller) { return controller.name; })
  55. .fmap(function (name) {
  56. var text = name.text;
  57. var failures = [];
  58. var suffixes = suffixList.length > 0 ? suffixList : ['Component'];
  59. if (!Rule.validate(text, suffixes)) {
  60. failures.push(new walkerFactory_1.Failure(name, sprintf_js_1.sprintf(Rule.FAILURE_STRING, text, suffixes)));
  61. }
  62. return failures;
  63. });
  64. }));
  65. return Rule;
  66. }(Lint.Rules.AbstractRule));
  67. exports.Rule = Rule;