Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. messages: {
  42. blankBeginningOfFile: "Too many blank lines at the beginning of file. Max of {{max}} allowed.",
  43. blankEndOfFile: "Too many blank lines at the end of file. Max of {{max}} allowed.",
  44. consecutiveBlank: "More than {{max}} blank {{pluralizedLines}} not allowed."
  45. }
  46. },
  47. create(context) {
  48. // Use options.max or 2 as default
  49. let max = 2,
  50. maxEOF = max,
  51. maxBOF = max;
  52. if (context.options.length) {
  53. max = context.options[0].max;
  54. maxEOF = typeof context.options[0].maxEOF !== "undefined" ? context.options[0].maxEOF : max;
  55. maxBOF = typeof context.options[0].maxBOF !== "undefined" ? context.options[0].maxBOF : max;
  56. }
  57. const sourceCode = context.getSourceCode();
  58. // Swallow the final newline, as some editors add it automatically and we don't want it to cause an issue
  59. const allLines = sourceCode.lines[sourceCode.lines.length - 1] === "" ? sourceCode.lines.slice(0, -1) : sourceCode.lines;
  60. const templateLiteralLines = new Set();
  61. //--------------------------------------------------------------------------
  62. // Public
  63. //--------------------------------------------------------------------------
  64. return {
  65. TemplateLiteral(node) {
  66. node.quasis.forEach(literalPart => {
  67. // Empty lines have a semantic meaning if they're inside template literals. Don't count these as empty lines.
  68. for (let ignoredLine = literalPart.loc.start.line; ignoredLine < literalPart.loc.end.line; ignoredLine++) {
  69. templateLiteralLines.add(ignoredLine);
  70. }
  71. });
  72. },
  73. "Program:exit"(node) {
  74. return allLines
  75. // Given a list of lines, first get a list of line numbers that are non-empty.
  76. .reduce((nonEmptyLineNumbers, line, index) => {
  77. if (line.trim() || templateLiteralLines.has(index + 1)) {
  78. nonEmptyLineNumbers.push(index + 1);
  79. }
  80. return nonEmptyLineNumbers;
  81. }, [])
  82. // Add a value at the end to allow trailing empty lines to be checked.
  83. .concat(allLines.length + 1)
  84. // Given two line numbers of non-empty lines, report the lines between if the difference is too large.
  85. .reduce((lastLineNumber, lineNumber) => {
  86. let messageId, maxAllowed;
  87. if (lastLineNumber === 0) {
  88. messageId = "blankBeginningOfFile";
  89. maxAllowed = maxBOF;
  90. } else if (lineNumber === allLines.length + 1) {
  91. messageId = "blankEndOfFile";
  92. maxAllowed = maxEOF;
  93. } else {
  94. messageId = "consecutiveBlank";
  95. maxAllowed = max;
  96. }
  97. if (lineNumber - lastLineNumber - 1 > maxAllowed) {
  98. context.report({
  99. node,
  100. loc: {
  101. start: { line: lastLineNumber + maxAllowed + 1, column: 0 },
  102. end: { line: lineNumber, column: 0 }
  103. },
  104. messageId,
  105. data: {
  106. max: maxAllowed,
  107. pluralizedLines: maxAllowed === 1 ? "line" : "lines"
  108. },
  109. fix(fixer) {
  110. const rangeStart = sourceCode.getIndexFromLoc({ line: lastLineNumber + 1, column: 0 });
  111. /*
  112. * The end of the removal range is usually the start index of the next line.
  113. * However, at the end of the file there is no next line, so the end of the
  114. * range is just the length of the text.
  115. */
  116. const lineNumberAfterRemovedLines = lineNumber - maxAllowed;
  117. const rangeEnd = lineNumberAfterRemovedLines <= allLines.length
  118. ? sourceCode.getIndexFromLoc({ line: lineNumberAfterRemovedLines, column: 0 })
  119. : sourceCode.text.length;
  120. return fixer.removeRange([rangeStart, rangeEnd]);
  121. }
  122. });
  123. }
  124. return lineNumber;
  125. }, 0);
  126. }
  127. };
  128. }
  129. };