noConsecutiveBlankLinesRule.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2013 Palantir Technologies, Inc.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. var tslib_1 = require("tslib");
  20. var utils = require("tsutils");
  21. var ts = require("typescript");
  22. var Lint = require("../index");
  23. var Rule = /** @class */ (function (_super) {
  24. tslib_1.__extends(Rule, _super);
  25. function Rule() {
  26. return _super !== null && _super.apply(this, arguments) || this;
  27. }
  28. /* tslint:enable:object-literal-sort-keys */
  29. Rule.FAILURE_STRING_FACTORY = function (allowed) {
  30. return allowed === 1
  31. ? "Consecutive blank lines are forbidden"
  32. : "Exceeds the " + allowed + " allowed consecutive blank lines";
  33. };
  34. /**
  35. * Disable the rule if the option is provided but non-numeric or less than the minimum.
  36. */
  37. Rule.prototype.isEnabled = function () {
  38. var option = this.ruleArguments[0];
  39. return _super.prototype.isEnabled.call(this) && (option === undefined || option > 0);
  40. };
  41. Rule.prototype.apply = function (sourceFile) {
  42. var limit = this.ruleArguments[0];
  43. return this.applyWithFunction(sourceFile, walk, limit !== undefined ? limit : Rule.DEFAULT_ALLOWED_BLANKS);
  44. };
  45. Rule.DEFAULT_ALLOWED_BLANKS = 1;
  46. /* tslint:disable:object-literal-sort-keys */
  47. Rule.metadata = {
  48. ruleName: "no-consecutive-blank-lines",
  49. description: "Disallows one or more blank lines in a row.",
  50. hasFix: true,
  51. rationale: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Helps maintain a readable style in your codebase.\n\n Extra blank lines take up extra space and add little to a semantic understanding of the code.\n It can be harder to read through files when fewer components can fit into the screen.\n If you find a file is so large you feel a need to split them up with extra blank lines or comments,\n consider splitting your file into smaller files.\n "], ["\n Helps maintain a readable style in your codebase.\n\n Extra blank lines take up extra space and add little to a semantic understanding of the code.\n It can be harder to read through files when fewer components can fit into the screen.\n If you find a file is so large you feel a need to split them up with extra blank lines or comments,\n consider splitting your file into smaller files.\n "]))),
  52. optionsDescription: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n An optional number of maximum allowed sequential blanks can be specified. If no value\n is provided, a default of ", " will be used."], ["\n An optional number of maximum allowed sequential blanks can be specified. If no value\n is provided, a default of ", " will be used."])), Rule.DEFAULT_ALLOWED_BLANKS),
  53. options: {
  54. type: "number",
  55. minimum: "1",
  56. },
  57. optionExamples: [true, [true, 2]],
  58. type: "style",
  59. typescriptOnly: false,
  60. };
  61. return Rule;
  62. }(Lint.Rules.AbstractRule));
  63. exports.Rule = Rule;
  64. function walk(ctx) {
  65. var sourceText = ctx.sourceFile.text;
  66. var threshold = ctx.options + 1;
  67. var possibleFailures = [];
  68. var consecutiveBlankLines = 0;
  69. for (var _i = 0, _a = utils.getLineRanges(ctx.sourceFile); _i < _a.length; _i++) {
  70. var line = _a[_i];
  71. if (line.contentLength === 0 || sourceText.substr(line.pos, line.contentLength).search(/\S/) === -1) {
  72. ++consecutiveBlankLines;
  73. if (consecutiveBlankLines === threshold) {
  74. possibleFailures.push({
  75. end: line.end,
  76. pos: line.pos,
  77. });
  78. }
  79. else if (consecutiveBlankLines > threshold) {
  80. possibleFailures[possibleFailures.length - 1].end = line.end;
  81. }
  82. }
  83. else {
  84. consecutiveBlankLines = 0;
  85. }
  86. }
  87. if (possibleFailures.length === 0) {
  88. return;
  89. }
  90. var failureString = Rule.FAILURE_STRING_FACTORY(ctx.options);
  91. var templateRanges = getTemplateRanges(ctx.sourceFile);
  92. var _loop_1 = function (possibleFailure) {
  93. if (!templateRanges.some(function (template) { return template.pos < possibleFailure.pos && possibleFailure.pos < template.end; })) {
  94. ctx.addFailureAt(possibleFailure.pos, 1, failureString, [
  95. Lint.Replacement.deleteFromTo(
  96. // special handling for fixing blank lines at the end of the file
  97. // to fix this we need to cut off the line break of the last allowed blank line, too
  98. possibleFailure.end === sourceText.length ? getStartOfLineBreak(sourceText, possibleFailure.pos) : possibleFailure.pos, possibleFailure.end),
  99. ]);
  100. }
  101. };
  102. for (var _b = 0, possibleFailures_1 = possibleFailures; _b < possibleFailures_1.length; _b++) {
  103. var possibleFailure = possibleFailures_1[_b];
  104. _loop_1(possibleFailure);
  105. }
  106. }
  107. function getStartOfLineBreak(sourceText, pos) {
  108. return sourceText[pos - 2] === "\r" ? pos - 1 : pos - 1;
  109. }
  110. function getTemplateRanges(sourceFile) {
  111. var intervals = [];
  112. var cb = function (node) {
  113. if (node.kind >= ts.SyntaxKind.FirstTemplateToken &&
  114. node.kind <= ts.SyntaxKind.LastTemplateToken) {
  115. intervals.push({
  116. end: node.end,
  117. pos: node.getStart(sourceFile),
  118. });
  119. }
  120. else {
  121. return ts.forEachChild(node, cb);
  122. }
  123. };
  124. ts.forEachChild(sourceFile, cb);
  125. return intervals;
  126. }
  127. exports.getTemplateRanges = getTemplateRanges;
  128. var templateObject_1, templateObject_2;