Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

no-multiple-empty-lines.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * @fileoverview Disallows multiple blank lines.
  3. * implementation adapted from the no-trailing-spaces rule.
  4. * @author Greg Cochard
  5. */
  6. "use strict";
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. type: "layout",
  13. docs: {
  14. description: "disallow multiple empty lines",
  15. category: "Stylistic Issues",
  16. recommended: false,
  17. url: "https://eslint.org/docs/rules/no-multiple-empty-lines"
  18. },
  19. fixable: "whitespace",
  20. schema: [
  21. {
  22. type: "object",
  23. properties: {
  24. max: {
  25. type: "integer",
  26. minimum: 0
  27. },
  28. maxEOF: {
  29. type: "integer",
  30. minimum: 0
  31. },
  32. maxBOF: {
  33. type: "integer",
  34. minimum: 0
  35. }
  36. },
  37. required: ["max"],
  38. additionalProperties: false
  39. }
  40. ]
  41. },
  42. create(context) {
  43. // Use options.max or 2 as default
  44. let max = 2,
  45. maxEOF = max,
  46. maxBOF = max;
  47. if (context.options.length) {
  48. max = context.options[0].max;
  49. maxEOF = typeof context.options[0].maxEOF !== "undefined" ? context.options[0].maxEOF : max;
  50. maxBOF = typeof context.options[0].maxBOF !== "undefined" ? context.options[0].maxBOF : max;
  51. }
  52. const sourceCode = context.getSourceCode();
  53. // Swallow the final newline, as some editors add it automatically and we don't want it to cause an issue
  54. const allLines = sourceCode.lines[sourceCode.lines.length - 1] === "" ? sourceCode.lines.slice(0, -1) : sourceCode.lines;
  55. const templateLiteralLines = new Set();
  56. //--------------------------------------------------------------------------
  57. // Public
  58. //--------------------------------------------------------------------------
  59. return {
  60. TemplateLiteral(node) {
  61. node.quasis.forEach(literalPart => {
  62. // Empty lines have a semantic meaning if they're inside template literals. Don't count these as empty lines.
  63. for (let ignoredLine = literalPart.loc.start.line; ignoredLine < literalPart.loc.end.line; ignoredLine++) {
  64. templateLiteralLines.add(ignoredLine);
  65. }
  66. });
  67. },
  68. "Program:exit"(node) {
  69. return allLines
  70. // Given a list of lines, first get a list of line numbers that are non-empty.
  71. .reduce((nonEmptyLineNumbers, line, index) => {
  72. if (line.trim() || templateLiteralLines.has(index + 1)) {
  73. nonEmptyLineNumbers.push(index + 1);
  74. }
  75. return nonEmptyLineNumbers;
  76. }, [])
  77. // Add a value at the end to allow trailing empty lines to be checked.
  78. .concat(allLines.length + 1)
  79. // Given two line numbers of non-empty lines, report the lines between if the difference is too large.
  80. .reduce((lastLineNumber, lineNumber) => {
  81. let message, maxAllowed;
  82. if (lastLineNumber === 0) {
  83. message = "Too many blank lines at the beginning of file. Max of {{max}} allowed.";
  84. maxAllowed = maxBOF;
  85. } else if (lineNumber === allLines.length + 1) {
  86. message = "Too many blank lines at the end of file. Max of {{max}} allowed.";
  87. maxAllowed = maxEOF;
  88. } else {
  89. message = "More than {{max}} blank {{pluralizedLines}} not allowed.";
  90. maxAllowed = max;
  91. }
  92. if (lineNumber - lastLineNumber - 1 > maxAllowed) {
  93. context.report({
  94. node,
  95. loc: { start: { line: lastLineNumber + 1, column: 0 }, end: { line: lineNumber, column: 0 } },
  96. message,
  97. data: { max: maxAllowed, pluralizedLines: maxAllowed === 1 ? "line" : "lines" },
  98. fix(fixer) {
  99. const rangeStart = sourceCode.getIndexFromLoc({ line: lastLineNumber + 1, column: 0 });
  100. /*
  101. * The end of the removal range is usually the start index of the next line.
  102. * However, at the end of the file there is no next line, so the end of the
  103. * range is just the length of the text.
  104. */
  105. const lineNumberAfterRemovedLines = lineNumber - maxAllowed;
  106. const rangeEnd = lineNumberAfterRemovedLines <= allLines.length
  107. ? sourceCode.getIndexFromLoc({ line: lineNumberAfterRemovedLines, column: 0 })
  108. : sourceCode.text.length;
  109. return fixer.removeRange([rangeStart, rangeEnd]);
  110. }
  111. });
  112. }
  113. return lineNumber;
  114. }, 0);
  115. }
  116. };
  117. }
  118. };