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