noEmptyRule.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 BlockWalker(sourceFile, this.getOptions()));
  16. };
  17. Rule.FAILURE_STRING = "block is empty";
  18. return Rule;
  19. }(Lint.Rules.AbstractRule));
  20. exports.Rule = Rule;
  21. var BlockWalker = (function (_super) {
  22. __extends(BlockWalker, _super);
  23. function BlockWalker() {
  24. _super.apply(this, arguments);
  25. this.ignoredBlocks = [];
  26. }
  27. BlockWalker.prototype.visitBlock = function (node) {
  28. var openBrace = node.getChildAt(0);
  29. var closeBrace = node.getChildAt(node.getChildCount() - 1);
  30. var sourceFileText = node.getSourceFile().text;
  31. var hasCommentAfter = ts.getTrailingCommentRanges(sourceFileText, openBrace.getEnd()) != null;
  32. var hasCommentBefore = ts.getLeadingCommentRanges(sourceFileText, closeBrace.getFullStart()) != null;
  33. var isSkipped = this.ignoredBlocks.indexOf(node) !== -1;
  34. if (node.statements.length <= 0 && !hasCommentAfter && !hasCommentBefore && !isSkipped) {
  35. this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING));
  36. }
  37. _super.prototype.visitBlock.call(this, node);
  38. };
  39. BlockWalker.prototype.visitConstructorDeclaration = function (node) {
  40. var parameters = node.parameters;
  41. var isSkipped = false;
  42. for (var _i = 0, parameters_1 = parameters; _i < parameters_1.length; _i++) {
  43. var param = parameters_1[_i];
  44. var hasPropertyAccessModifier = Lint.hasModifier(param.modifiers, ts.SyntaxKind.PrivateKeyword, ts.SyntaxKind.ProtectedKeyword, ts.SyntaxKind.PublicKeyword);
  45. if (hasPropertyAccessModifier) {
  46. isSkipped = true;
  47. this.ignoredBlocks.push(node.body);
  48. break;
  49. }
  50. if (isSkipped) {
  51. break;
  52. }
  53. }
  54. _super.prototype.visitConstructorDeclaration.call(this, node);
  55. };
  56. return BlockWalker;
  57. }(Lint.RuleWalker));