noAttributeDecoratorRule.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 ATTRIBUTE = 'Attribute';
  20. var Rule = (function (_super) {
  21. __extends(Rule, _super);
  22. function Rule() {
  23. return _super !== null && _super.apply(this, arguments) || this;
  24. }
  25. Rule.prototype.apply = function (sourceFile) {
  26. return this.applyWithFunction(sourceFile, walk);
  27. };
  28. Rule.metadata = {
  29. description: "Disallows usage of @" + ATTRIBUTE + " decorator.",
  30. options: null,
  31. optionsDescription: 'Not configurable.',
  32. ruleName: 'no-attribute-decorator',
  33. type: 'functionality',
  34. typescriptOnly: true
  35. };
  36. Rule.FAILURE_STRING = "@" + ATTRIBUTE + " is considered bad practice. Use @Input instead.";
  37. return Rule;
  38. }(rules_1.AbstractRule));
  39. exports.Rule = Rule;
  40. var callbackHandler = function (walkContext, node) {
  41. if (typescript_1.isConstructorDeclaration(node))
  42. validateConstructor(walkContext, node);
  43. };
  44. var isAttributeDecorator = function (decorator) { return utils_1.getDecoratorName(decorator) === ATTRIBUTE; };
  45. var validateConstructor = function (walkContext, node) {
  46. node.parameters.forEach(function (parameter) { return validateParameter(walkContext, parameter); });
  47. };
  48. var validateDecorator = function (walkContext, decorator) {
  49. if (!isAttributeDecorator(decorator))
  50. return;
  51. walkContext.addFailureAtNode(decorator, Rule.FAILURE_STRING);
  52. };
  53. var validateParameter = function (walkContext, node) {
  54. typescript_1.createNodeArray(node.decorators).forEach(function (decorator) { return validateDecorator(walkContext, decorator); });
  55. };
  56. var walk = function (walkContext) {
  57. var sourceFile = walkContext.sourceFile;
  58. var callback = function (node) {
  59. callbackHandler(walkContext, node);
  60. typescript_1.forEachChild(node, callback);
  61. };
  62. typescript_1.forEachChild(sourceFile, callback);
  63. };