41e6be3a289584f0a6d8ee0d96a4572fb7226af6
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / eslint / lib / rules / no-multiple-empty-lines.js
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
12 module.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
48     create(context) {
49
50         // Use options.max or 2 as default
51         let max = 2,
52             maxEOF = max,
53             maxBOF = max;
54
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;
59         }
60
61         const sourceCode = context.getSourceCode();
62
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();
66
67         //--------------------------------------------------------------------------
68         // Public
69         //--------------------------------------------------------------------------
70
71         return {
72             TemplateLiteral(node) {
73                 node.quasis.forEach(literalPart => {
74
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);
78                     }
79                 });
80             },
81             "Program:exit"(node) {
82                 return allLines
83
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);
88                         }
89                         return nonEmptyLineNumbers;
90                     }, [])
91
92                     // Add a value at the end to allow trailing empty lines to be checked.
93                     .concat(allLines.length + 1)
94
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;
98
99                         if (lastLineNumber === 0) {
100                             message = "Too many blank lines at the beginning of file. Max of {{max}} allowed.";
101                             maxAllowed = maxBOF;
102                         } else if (lineNumber === allLines.length + 1) {
103                             message = "Too many blank lines at the end of file. Max of {{max}} allowed.";
104                             maxAllowed = maxEOF;
105                         } else {
106                             message = "More than {{max}} blank {{pluralizedLines}} not allowed.";
107                             maxAllowed = max;
108                         }
109
110                         if (lineNumber - lastLineNumber - 1 > maxAllowed) {
111                             context.report({
112                                 node,
113                                 loc: { start: { line: lastLineNumber + maxAllowed + 1, column: 0 }, end: { line: lineNumber, column: 0 } },
114                                 message,
115                                 data: { max: maxAllowed, pluralizedLines: maxAllowed === 1 ? "line" : "lines" },
116                                 fix(fixer) {
117                                     const rangeStart = sourceCode.getIndexFromLoc({ line: lastLineNumber + 1, column: 0 });
118
119                                     /*
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.
123                                      */
124                                     const lineNumberAfterRemovedLines = lineNumber - maxAllowed;
125                                     const rangeEnd = lineNumberAfterRemovedLines <= allLines.length
126                                         ? sourceCode.getIndexFromLoc({ line: lineNumberAfterRemovedLines, column: 0 })
127                                         : sourceCode.text.length;
128
129                                     return fixer.removeRange([rangeStart, rangeEnd]);
130                                 }
131                             });
132                         }
133
134                         return lineNumber;
135                     }, 0);
136             }
137         };
138     }
139 };