useComponentSelectorRule.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 rules_1 = require("tslint/lib/rules");
  18. var ngWalker_1 = require("./angular/ngWalker");
  19. exports.getFailureMessage = function (failureParameters) {
  20. return sprintf_js_1.sprintf(Rule.FAILURE_STRING, failureParameters.className);
  21. };
  22. var Rule = (function (_super) {
  23. __extends(Rule, _super);
  24. function Rule() {
  25. return _super !== null && _super.apply(this, arguments) || this;
  26. }
  27. Rule.prototype.apply = function (sourceFile) {
  28. var walker = new Walker(sourceFile, this.getOptions());
  29. return this.applyWithWalker(walker);
  30. };
  31. Rule.metadata = {
  32. description: 'Component selector must be declared.',
  33. options: null,
  34. optionsDescription: 'Not configurable.',
  35. rationale: 'Omit the component selector makes debugging difficult.',
  36. ruleName: 'use-component-selector',
  37. type: 'maintainability',
  38. typescriptOnly: true
  39. };
  40. Rule.FAILURE_STRING = 'The selector of the component "%s" is mandatory';
  41. return Rule;
  42. }(rules_1.AbstractRule));
  43. exports.Rule = Rule;
  44. var Walker = (function (_super) {
  45. __extends(Walker, _super);
  46. function Walker() {
  47. return _super !== null && _super.apply(this, arguments) || this;
  48. }
  49. Walker.prototype.visitNgComponent = function (metadata) {
  50. this.validateComponent(metadata);
  51. _super.prototype.visitNgComponent.call(this, metadata);
  52. };
  53. Walker.prototype.validateComponent = function (metadata) {
  54. var metadataDecorator = metadata.decorator, controllerName = metadata.controller.name, metadataSelector = metadata.selector;
  55. if (metadataSelector || !controllerName)
  56. return;
  57. var failure = exports.getFailureMessage({ className: controllerName.text });
  58. this.addFailureAtNode(metadataDecorator, failure);
  59. };
  60. return Walker;
  61. }(ngWalker_1.NgWalker));