memberAccessRule.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 ts = require("typescript");
  8. var Lint = require("../lint");
  9. var Rule = (function (_super) {
  10. __extends(Rule, _super);
  11. function Rule() {
  12. _super.apply(this, arguments);
  13. }
  14. Rule.prototype.apply = function (sourceFile) {
  15. return this.applyWithWalker(new MemberAccessWalker(sourceFile, this.getOptions()));
  16. };
  17. Rule.FAILURE_STRING = "default access modifier on member/method not allowed";
  18. return Rule;
  19. }(Lint.Rules.AbstractRule));
  20. exports.Rule = Rule;
  21. var MemberAccessWalker = (function (_super) {
  22. __extends(MemberAccessWalker, _super);
  23. function MemberAccessWalker(sourceFile, options) {
  24. _super.call(this, sourceFile, options);
  25. }
  26. MemberAccessWalker.prototype.visitConstructorDeclaration = function (node) {
  27. if (this.hasOption("check-constructor")) {
  28. this.validateVisibilityModifiers(node);
  29. }
  30. _super.prototype.visitConstructorDeclaration.call(this, node);
  31. };
  32. MemberAccessWalker.prototype.visitMethodDeclaration = function (node) {
  33. this.validateVisibilityModifiers(node);
  34. _super.prototype.visitMethodDeclaration.call(this, node);
  35. };
  36. MemberAccessWalker.prototype.visitPropertyDeclaration = function (node) {
  37. this.validateVisibilityModifiers(node);
  38. _super.prototype.visitPropertyDeclaration.call(this, node);
  39. };
  40. MemberAccessWalker.prototype.visitGetAccessor = function (node) {
  41. if (this.hasOption("check-accessor")) {
  42. this.validateVisibilityModifiers(node);
  43. }
  44. _super.prototype.visitGetAccessor.call(this, node);
  45. };
  46. MemberAccessWalker.prototype.visitSetAccessor = function (node) {
  47. if (this.hasOption("check-accessor")) {
  48. this.validateVisibilityModifiers(node);
  49. }
  50. _super.prototype.visitSetAccessor.call(this, node);
  51. };
  52. MemberAccessWalker.prototype.validateVisibilityModifiers = function (node) {
  53. if (node.parent.kind === ts.SyntaxKind.ObjectLiteralExpression) {
  54. return;
  55. }
  56. var hasAnyVisibilityModifiers = Lint.hasModifier(node.modifiers, ts.SyntaxKind.PublicKeyword, ts.SyntaxKind.PrivateKeyword, ts.SyntaxKind.ProtectedKeyword);
  57. if (!hasAnyVisibilityModifiers) {
  58. this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING));
  59. }
  60. };
  61. return MemberAccessWalker;
  62. }(Lint.RuleWalker));
  63. exports.MemberAccessWalker = MemberAccessWalker;