metadataPropertyBase.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 rules_1 = require("tslint/lib/rules");
  17. var typescript_1 = require("typescript");
  18. var utils_1 = require("./util/utils");
  19. var COMPONENT_DIRECTIVE_PATTERN = /^(Component|Directive)$/;
  20. var MetadataPropertyBase = (function (_super) {
  21. __extends(MetadataPropertyBase, _super);
  22. function MetadataPropertyBase(config, options) {
  23. var _this = _super.call(this, options) || this;
  24. _this.config = config;
  25. return _this;
  26. }
  27. MetadataPropertyBase.prototype.apply = function (sourceFile) {
  28. return this.applyWithFunction(sourceFile, walk, this.config);
  29. };
  30. return MetadataPropertyBase;
  31. }(rules_1.AbstractRule));
  32. exports.MetadataPropertyBase = MetadataPropertyBase;
  33. var validateClassDeclaration = function (context, node) {
  34. return typescript_1.createNodeArray(node.decorators).forEach(function (decorator) { return validateDecorator(context, decorator); });
  35. };
  36. var validateDecorator = function (context, decorator) {
  37. var _a = context.options, errorMessage = _a.errorMessage, propertyName = _a.propertyName;
  38. var propertyExpression = utils_1.getDecoratorPropertyInitializer(decorator, propertyName);
  39. var decoratorName = utils_1.getDecoratorName(decorator);
  40. if (!decoratorName || !propertyExpression || !COMPONENT_DIRECTIVE_PATTERN.test(decoratorName)) {
  41. return;
  42. }
  43. context.addFailureAtNode(propertyExpression.parent, errorMessage);
  44. };
  45. var walk = function (context) {
  46. var sourceFile = context.sourceFile;
  47. var callback = function (node) {
  48. if (typescript_1.isClassDeclaration(node))
  49. validateClassDeclaration(context, node);
  50. typescript_1.forEachChild(node, callback);
  51. };
  52. typescript_1.forEachChild(sourceFile, callback);
  53. };