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