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