noConsecutiveBlankLinesRule.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 NoConsecutiveBlankLinesWalker(sourceFile, this.getOptions()));
  16. };
  17. Rule.FAILURE_STRING = "consecutive blank lines are disallowed";
  18. return Rule;
  19. }(Lint.Rules.AbstractRule));
  20. exports.Rule = Rule;
  21. var NoConsecutiveBlankLinesWalker = (function (_super) {
  22. __extends(NoConsecutiveBlankLinesWalker, _super);
  23. function NoConsecutiveBlankLinesWalker() {
  24. _super.apply(this, arguments);
  25. }
  26. NoConsecutiveBlankLinesWalker.prototype.visitSourceFile = function (node) {
  27. var _this = this;
  28. _super.prototype.visitSourceFile.call(this, node);
  29. var newLinesInARowSeenSoFar = 1;
  30. Lint.scanAllTokens(ts.createScanner(ts.ScriptTarget.ES5, false, ts.LanguageVariant.Standard, node.text), function (scanner) {
  31. var startPos = scanner.getStartPos();
  32. if (_this.tokensToSkipStartEndMap[startPos] != null) {
  33. scanner.setTextPos(_this.tokensToSkipStartEndMap[startPos]);
  34. newLinesInARowSeenSoFar = 0;
  35. return;
  36. }
  37. if (scanner.getToken() === ts.SyntaxKind.NewLineTrivia) {
  38. newLinesInARowSeenSoFar += 1;
  39. if (newLinesInARowSeenSoFar >= 3) {
  40. var failure = _this.createFailure(scanner.getStartPos(), 1, Rule.FAILURE_STRING);
  41. _this.addFailure(failure);
  42. }
  43. }
  44. else {
  45. newLinesInARowSeenSoFar = 0;
  46. }
  47. });
  48. };
  49. return NoConsecutiveBlankLinesWalker;
  50. }(Lint.SkippableTokenAwareRuleWalker));