contextualDecoratorRule.js 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 rules_1 = require("tslint/lib/rules");
  22. var utils_1 = require("tslint/lib/utils");
  23. var typescript_1 = require("typescript");
  24. var isNotNullOrUndefined_1 = require("./util/isNotNullOrUndefined");
  25. var utils_2 = require("./util/utils");
  26. exports.getFailureMessage = function (failureParameters) {
  27. return sprintf_js_1.sprintf(Rule.FAILURE_STRING, failureParameters.classDecoratorName);
  28. };
  29. var Rule = (function (_super) {
  30. __extends(Rule, _super);
  31. function Rule() {
  32. return _super !== null && _super.apply(this, arguments) || this;
  33. }
  34. Rule.prototype.apply = function (sourceFile) {
  35. return this.applyWithFunction(sourceFile, walk);
  36. };
  37. Rule.metadata = {
  38. description: 'Ensures that classes use contextual decorators in its body.',
  39. options: null,
  40. optionsDescription: 'Not configurable.',
  41. rationale: utils_1.dedent(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n Some decorators should only be used in certain class types. For example,\n the decorator @", "() should\n not be used in a class decorated with @", "().\n "], ["\n Some decorators should only be used in certain class types. For example,\n the decorator @", "() should\n not be used in a class decorated with @", "().\n "])), utils_2.AngularInnerClassDecorators.Input, utils_2.AngularClassDecorators.Injectable),
  42. ruleName: 'contextual-decorator',
  43. type: 'functionality',
  44. typescriptOnly: true
  45. };
  46. Rule.FAILURE_STRING = 'Decorator out of context for "@%s()"';
  47. return Rule;
  48. }(rules_1.AbstractRule));
  49. exports.Rule = Rule;
  50. var callbackHandler = function (walkContext, node) {
  51. if (isDeclarationLike(node))
  52. validateDeclaration(walkContext, node);
  53. };
  54. var getClassDecoratorName = function (klass) {
  55. return typescript_1.createNodeArray(klass.decorators)
  56. .map(utils_2.getDecoratorName)
  57. .filter(isNotNullOrUndefined_1.isNotNullOrUndefined)
  58. .find(utils_2.isAngularClassDecorator);
  59. };
  60. var isDeclarationLike = function (node) {
  61. return typescript_1.isAccessor(node) || typescript_1.isMethodDeclaration(node) || typescript_1.isParameterPropertyDeclaration(node, node.parent) || typescript_1.isPropertyDeclaration(node);
  62. };
  63. var validateDeclaration = function (walkContext, node) {
  64. var klass = utils_2.getNextToLastParentNode(node);
  65. var classDecoratorName = getClassDecoratorName(klass);
  66. if (!classDecoratorName)
  67. return;
  68. typescript_1.createNodeArray(node.decorators).forEach(function (decorator) { return validateDecorator(walkContext, decorator, classDecoratorName); });
  69. };
  70. var validateDecorator = function (walkContext, node, classDecoratorName) {
  71. var decoratorName = utils_2.getDecoratorName(node);
  72. if (!decoratorName || !utils_2.isAngularInnerClassDecorator(decoratorName))
  73. return;
  74. var allowedDecorators = utils_2.ANGULAR_CLASS_DECORATOR_MAPPER.get(classDecoratorName);
  75. if (!allowedDecorators || allowedDecorators.has(decoratorName))
  76. return;
  77. var failure = exports.getFailureMessage({ classDecoratorName: classDecoratorName });
  78. walkContext.addFailureAtNode(node, failure);
  79. };
  80. var walk = function (walkContext) {
  81. var sourceFile = walkContext.sourceFile;
  82. var callback = function (node) {
  83. callbackHandler(walkContext, node);
  84. typescript_1.forEachChild(node, callback);
  85. };
  86. typescript_1.forEachChild(sourceFile, callback);
  87. };
  88. var templateObject_1;