pipePrefixRule.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
  16. if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
  17. return cooked;
  18. };
  19. Object.defineProperty(exports, "__esModule", { value: true });
  20. var sprintf_js_1 = require("sprintf-js");
  21. var Lint = require("tslint");
  22. var ts = require("typescript");
  23. var ngWalker_1 = require("./angular/ngWalker");
  24. var selectorValidator_1 = require("./util/selectorValidator");
  25. var utils_1 = require("./util/utils");
  26. var Rule = (function (_super) {
  27. __extends(Rule, _super);
  28. function Rule(options) {
  29. var _this = _super.call(this, options) || this;
  30. var args = options.ruleArguments;
  31. if (!(args instanceof Array)) {
  32. args = [args];
  33. }
  34. _this.prefix = args.join(',');
  35. var prefixExpression = args.join('|');
  36. _this.prefixChecker = selectorValidator_1.SelectorValidator.prefix(prefixExpression, 'camelCase');
  37. return _this;
  38. }
  39. Rule.prototype.apply = function (sourceFile) {
  40. var walker = new Walker(sourceFile, this);
  41. return this.applyWithWalker(walker);
  42. };
  43. Rule.prototype.isEnabled = function () {
  44. var minLength = Rule.metadata.options.minLength;
  45. var length = this.ruleArguments.length;
  46. return _super.prototype.isEnabled.call(this) && length >= minLength;
  47. };
  48. Rule.prototype.validatePrefix = function (prefix) {
  49. return this.prefixChecker(prefix);
  50. };
  51. Rule.metadata = {
  52. description: 'Enforce consistent prefix for pipes.',
  53. optionExamples: [[true, 'myPrefix'], [true, 'myPrefix', 'myOtherPrefix']],
  54. options: {
  55. items: [
  56. {
  57. type: 'string'
  58. }
  59. ],
  60. minLength: 1,
  61. type: 'array'
  62. },
  63. optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n * The list of arguments are supported prefixes (given as strings).\n "], ["\n * The list of arguments are supported prefixes (given as strings).\n "]))),
  64. rationale: 'Consistent conventions make it easy to quickly identify and reference assets of different types.',
  65. ruleName: 'pipe-prefix',
  66. type: 'style',
  67. typescriptOnly: true
  68. };
  69. Rule.FAILURE_STRING = "The name of the Pipe decorator of class %s should start with prefix %s, however its value is \"%s\"";
  70. return Rule;
  71. }(Lint.Rules.AbstractRule));
  72. exports.Rule = Rule;
  73. var Walker = (function (_super) {
  74. __extends(Walker, _super);
  75. function Walker(sourceFile, rule) {
  76. var _this = _super.call(this, sourceFile, rule.getOptions()) || this;
  77. _this.rule = rule;
  78. return _this;
  79. }
  80. Walker.prototype.visitNgPipe = function (metadata) {
  81. var className = metadata.controller.name.text;
  82. this.validateProperties(className, metadata.decorator);
  83. _super.prototype.visitNgPipe.call(this, metadata);
  84. };
  85. Walker.prototype.validateProperties = function (className, pipe) {
  86. var argument = utils_1.getDecoratorArgument(pipe);
  87. argument.properties
  88. .filter(function (p) { return p.name && ts.isIdentifier(p.name) && p.name.text === 'name'; })
  89. .forEach(this.validateProperty.bind(this, className));
  90. };
  91. Walker.prototype.validateProperty = function (className, property) {
  92. var initializer = ts.isPropertyAssignment(property) ? property.initializer : undefined;
  93. if (initializer && ts.isStringLiteral(initializer)) {
  94. var propName = initializer.text;
  95. var isValid = this.rule.validatePrefix(propName);
  96. if (!isValid) {
  97. this.addFailureAtNode(property, sprintf_js_1.sprintf(Rule.FAILURE_STRING, className, this.rule.prefix, propName));
  98. }
  99. }
  100. };
  101. return Walker;
  102. }(ngWalker_1.NgWalker));
  103. var templateObject_1;