2 * @fileoverview Disallows multiple blank lines.
3 * implementation adapted from the no-trailing-spaces rule.
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
17 description: "disallow multiple empty lines",
18 category: "Stylistic Issues",
20 url: "https://eslint.org/docs/rules/no-multiple-empty-lines"
23 fixable: "whitespace",
43 additionalProperties: false
50 // Use options.max or 2 as default
55 if (context.options.length) {
56 max = context.options[0].max;
57 maxEOF = typeof context.options[0].maxEOF !== "undefined" ? context.options[0].maxEOF : max;
58 maxBOF = typeof context.options[0].maxBOF !== "undefined" ? context.options[0].maxBOF : max;
61 const sourceCode = context.getSourceCode();
63 // Swallow the final newline, as some editors add it automatically and we don't want it to cause an issue
64 const allLines = sourceCode.lines[sourceCode.lines.length - 1] === "" ? sourceCode.lines.slice(0, -1) : sourceCode.lines;
65 const templateLiteralLines = new Set();
67 //--------------------------------------------------------------------------
69 //--------------------------------------------------------------------------
72 TemplateLiteral(node) {
73 node.quasis.forEach(literalPart => {
75 // Empty lines have a semantic meaning if they're inside template literals. Don't count these as empty lines.
76 for (let ignoredLine = literalPart.loc.start.line; ignoredLine < literalPart.loc.end.line; ignoredLine++) {
77 templateLiteralLines.add(ignoredLine);
81 "Program:exit"(node) {
84 // Given a list of lines, first get a list of line numbers that are non-empty.
85 .reduce((nonEmptyLineNumbers, line, index) => {
86 if (line.trim() || templateLiteralLines.has(index + 1)) {
87 nonEmptyLineNumbers.push(index + 1);
89 return nonEmptyLineNumbers;
92 // Add a value at the end to allow trailing empty lines to be checked.
93 .concat(allLines.length + 1)
95 // Given two line numbers of non-empty lines, report the lines between if the difference is too large.
96 .reduce((lastLineNumber, lineNumber) => {
97 let message, maxAllowed;
99 if (lastLineNumber === 0) {
100 message = "Too many blank lines at the beginning of file. Max of {{max}} allowed.";
102 } else if (lineNumber === allLines.length + 1) {
103 message = "Too many blank lines at the end of file. Max of {{max}} allowed.";
106 message = "More than {{max}} blank {{pluralizedLines}} not allowed.";
110 if (lineNumber - lastLineNumber - 1 > maxAllowed) {
113 loc: { start: { line: lastLineNumber + maxAllowed + 1, column: 0 }, end: { line: lineNumber, column: 0 } },
115 data: { max: maxAllowed, pluralizedLines: maxAllowed === 1 ? "line" : "lines" },
117 const rangeStart = sourceCode.getIndexFromLoc({ line: lastLineNumber + 1, column: 0 });
120 * The end of the removal range is usually the start index of the next line.
121 * However, at the end of the file there is no next line, so the end of the
122 * range is just the length of the text.
124 const lineNumberAfterRemovedLines = lineNumber - maxAllowed;
125 const rangeEnd = lineNumberAfterRemovedLines <= allLines.length
126 ? sourceCode.getIndexFromLoc({ line: lineNumberAfterRemovedLines, column: 0 })
127 : sourceCode.text.length;
129 return fixer.removeRange([rangeStart, rangeEnd]);