项目原始demo,不改动
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
Den här utvecklingskatalogen är arkiverad. Du kan se filer och klona katalogen, men inte öppna ärenden eller genomföra push- eller pull-förfrågningar.
 
 
 
 

138 rader
5.7 KiB

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