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