memberOrderingRule.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  4. function __() { this.constructor = d; }
  5. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  6. };
  7. var Lint = require("../lint");
  8. var ts = require("typescript");
  9. var OPTION_VARIABLES_BEFORE_FUNCTIONS = "variables-before-functions";
  10. var OPTION_STATIC_BEFORE_INSTANCE = "static-before-instance";
  11. var OPTION_PUBLIC_BEFORE_PRIVATE = "public-before-private";
  12. var Rule = (function (_super) {
  13. __extends(Rule, _super);
  14. function Rule() {
  15. _super.apply(this, arguments);
  16. }
  17. Rule.prototype.apply = function (sourceFile) {
  18. return this.applyWithWalker(new MemberOrderingWalker(sourceFile, this.getOptions()));
  19. };
  20. return Rule;
  21. }(Lint.Rules.AbstractRule));
  22. exports.Rule = Rule;
  23. function getModifiers(isMethod, modifiers) {
  24. return {
  25. isInstance: !Lint.hasModifier(modifiers, ts.SyntaxKind.StaticKeyword),
  26. isMethod: isMethod,
  27. isPrivate: Lint.hasModifier(modifiers, ts.SyntaxKind.PrivateKeyword)
  28. };
  29. }
  30. function toString(modifiers) {
  31. return [
  32. modifiers.isPrivate ? "private" : "public",
  33. modifiers.isInstance ? "instance" : "static",
  34. "member",
  35. modifiers.isMethod ? "function" : "variable"
  36. ].join(" ");
  37. }
  38. var MemberOrderingWalker = (function (_super) {
  39. __extends(MemberOrderingWalker, _super);
  40. function MemberOrderingWalker(sourceFile, options) {
  41. _super.call(this, sourceFile, options);
  42. }
  43. MemberOrderingWalker.prototype.visitClassDeclaration = function (node) {
  44. this.resetPreviousModifiers();
  45. _super.prototype.visitClassDeclaration.call(this, node);
  46. };
  47. MemberOrderingWalker.prototype.visitInterfaceDeclaration = function (node) {
  48. this.resetPreviousModifiers();
  49. _super.prototype.visitInterfaceDeclaration.call(this, node);
  50. };
  51. MemberOrderingWalker.prototype.visitMethodDeclaration = function (node) {
  52. this.checkModifiersAndSetPrevious(node, getModifiers(true, node.modifiers));
  53. _super.prototype.visitMethodDeclaration.call(this, node);
  54. };
  55. MemberOrderingWalker.prototype.visitMethodSignature = function (node) {
  56. this.checkModifiersAndSetPrevious(node, getModifiers(true, node.modifiers));
  57. _super.prototype.visitMethodSignature.call(this, node);
  58. };
  59. MemberOrderingWalker.prototype.visitPropertyDeclaration = function (node) {
  60. this.checkModifiersAndSetPrevious(node, getModifiers(false, node.modifiers));
  61. _super.prototype.visitPropertyDeclaration.call(this, node);
  62. };
  63. MemberOrderingWalker.prototype.visitPropertySignature = function (node) {
  64. this.checkModifiersAndSetPrevious(node, getModifiers(false, node.modifiers));
  65. _super.prototype.visitPropertySignature.call(this, node);
  66. };
  67. MemberOrderingWalker.prototype.visitTypeLiteral = function (node) {
  68. };
  69. MemberOrderingWalker.prototype.resetPreviousModifiers = function () {
  70. this.previousMember = {
  71. isInstance: false,
  72. isMethod: false,
  73. isPrivate: false
  74. };
  75. };
  76. MemberOrderingWalker.prototype.checkModifiersAndSetPrevious = function (node, currentMember) {
  77. if (!this.canAppearAfter(this.previousMember, currentMember)) {
  78. var failure = this.createFailure(node.getStart(), node.getWidth(), "Declaration of " + toString(currentMember) + " not allowed to appear after declaration of " + toString(this.previousMember));
  79. this.addFailure(failure);
  80. }
  81. this.previousMember = currentMember;
  82. };
  83. MemberOrderingWalker.prototype.canAppearAfter = function (previousMember, currentMember) {
  84. if (previousMember == null || currentMember == null) {
  85. return true;
  86. }
  87. if (this.hasOption(OPTION_VARIABLES_BEFORE_FUNCTIONS) && previousMember.isMethod !== currentMember.isMethod) {
  88. return Number(previousMember.isMethod) < Number(currentMember.isMethod);
  89. }
  90. if (this.hasOption(OPTION_STATIC_BEFORE_INSTANCE) && previousMember.isInstance !== currentMember.isInstance) {
  91. return Number(previousMember.isInstance) < Number(currentMember.isInstance);
  92. }
  93. if (this.hasOption(OPTION_PUBLIC_BEFORE_PRIVATE) && previousMember.isPrivate !== currentMember.isPrivate) {
  94. return Number(previousMember.isPrivate) < Number(currentMember.isPrivate);
  95. }
  96. return true;
  97. };
  98. return MemberOrderingWalker;
  99. }(Lint.RuleWalker));
  100. exports.MemberOrderingWalker = MemberOrderingWalker;