]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-multiple-empty-lines.js
5291fbbf0a830bc1f81784ad76fc9a3e64f028f4
[pve-eslint.git] / eslint / tests / lib / rules / no-multiple-empty-lines.js
1 /**
2 * @fileoverview Disallows multiple blank lines.
3 * @author Greg Cochard
4 */
5 "use strict";
6
7 //------------------------------------------------------------------------------
8 // Requirements
9 //------------------------------------------------------------------------------
10
11 const rule = require("../../../lib/rules/no-multiple-empty-lines"),
12 { RuleTester } = require("../../../lib/rule-tester");
13
14 //------------------------------------------------------------------------------
15 // Tests
16 //------------------------------------------------------------------------------
17
18 const ruleTester = new RuleTester();
19
20 /**
21 * Creates the expected error message object for the specified number of lines
22 * @param {lines} lines The number of lines expected.
23 * @returns {Object} the expected error message object
24 * @private
25 */
26 function getExpectedError(lines) {
27 return {
28 messageId: "consecutiveBlank",
29 data: {
30 max: lines,
31 pluralizedLines: lines === 1 ? "line" : "lines"
32 },
33 type: "Program",
34 column: 1
35 };
36 }
37
38 /**
39 * Creates the expected error message object for the specified number of lines
40 * @param {lines} lines The number of lines expected.
41 * @returns {Object} the expected error message object
42 * @private
43 */
44 function getExpectedErrorEOF(lines) {
45 return {
46 messageId: "blankEndOfFile",
47 data: {
48 max: lines
49 },
50 type: "Program",
51 column: 1
52 };
53 }
54
55 /**
56 * Creates the expected error message object for the specified number of lines
57 * @param {lines} lines The number of lines expected.
58 * @returns {Object} the expected error message object
59 * @private
60 */
61 function getExpectedErrorBOF(lines) {
62 return {
63 messageId: "blankBeginningOfFile",
64 data: {
65 max: lines
66 },
67 type: "Program",
68 column: 1
69 };
70 }
71
72
73 ruleTester.run("no-multiple-empty-lines", rule, {
74
75 valid: [
76 {
77 code: "// valid 1\nvar a = 5;\nvar b = 3;\n\n",
78 options: [{ max: 1 }]
79 },
80 {
81 code: "// valid 2\n\nvar a = 5;\n\nvar b = 3;",
82 options: [{ max: 1 }]
83 },
84 {
85 code: "// valid 3\nvar a = 5;\n\nvar b = 3;\n\n\n",
86 options: [{ max: 2 }]
87 },
88 {
89 code: "// valid 4\nvar a = 5,\n b = 3;",
90 options: [{ max: 2 }]
91 },
92 {
93 code: "// valid 5\nvar a = 5;\n\n\n\n\nvar b = 3;\n\n\n\n\n",
94 options: [{ max: 4 }]
95 },
96 {
97 code: "// valid 6\nvar a = 5;\n/* comment */\nvar b = 5;",
98 options: [{ max: 0 }]
99 },
100 {
101 code: "// valid 7\nvar a = 5;\n",
102 options: [{ max: 0 }]
103 },
104 {
105 code: "// valid 8\nvar a = 5;\n",
106 options: [{ max: 0, maxEOF: 0 }]
107 },
108 {
109 code: "// valid 9\nvar a = 1;\n\n",
110 options: [{ max: 2, maxEOF: 1 }]
111 },
112 {
113 code: "// valid 10\nvar a = 5;\n",
114 options: [{ max: 0, maxBOF: 0 }]
115 },
116 {
117 code: "\n// valid 11\nvar a = 1;\n",
118 options: [{ max: 2, maxBOF: 1 }]
119 },
120 {
121 code: "// valid 12\r\n// windows line endings\r\nvar a = 5;\r\nvar b = 3;\r\n\r\n",
122 options: [{ max: 1 }]
123 },
124
125 // template strings
126 {
127 code: "// valid 12\nx = `\n\n\n\nhi\n\n\n\n`",
128 options: [{ max: 2 }],
129 parserOptions: { ecmaVersion: 6 }
130 },
131 {
132 code: "// valid 13\n`\n\n`",
133 options: [{ max: 0 }],
134 parserOptions: { ecmaVersion: 6 }
135 },
136 {
137 code: "// valid 14\nvar a = 5;`\n\n\n\n\n`",
138 options: [{ max: 0, maxEOF: 0 }],
139 parserOptions: { ecmaVersion: 6 }
140 },
141 {
142 code: "`\n\n\n\n\n`\n// valid 15\nvar a = 5;",
143 options: [{ max: 0, maxBOF: 0 }],
144 parserOptions: { ecmaVersion: 6 }
145 },
146 {
147 code: "\n\n\n\n// valid 16\nvar a = 5;\n",
148 options: [{ max: 0, maxBOF: 4 }]
149 },
150 {
151 code: "// valid 17\nvar a = 5;\n\n",
152 options: [{ max: 0, maxEOF: 1 }]
153 },
154 {
155 code: "var a = 5;",
156 options: [{ max: 1 }]
157 }
158 ],
159
160 invalid: [
161 {
162 code: "// invalid 1\nvar a = 5;\n\n\nvar b = 3;",
163 output: "// invalid 1\nvar a = 5;\n\nvar b = 3;",
164 options: [{ max: 1 }],
165 errors: [getExpectedError(1)]
166 },
167 {
168 code: "// invalid 2\n\n\n\n\nvar a = 5;",
169 output: "// invalid 2\n\n\nvar a = 5;",
170 options: [{ max: 2 }],
171 errors: [getExpectedError(2)]
172 },
173 {
174 code: "// invalid 3\nvar a = 5;\n\n\n\n",
175 output: "// invalid 3\nvar a = 5;\n\n\n",
176 options: [{ max: 2 }],
177 errors: [getExpectedErrorEOF(2)]
178 },
179 {
180 code: "// invalid 4\nvar a = 5;\n \n \n \n",
181 output: "// invalid 4\nvar a = 5;\n \n \n",
182 options: [{ max: 2 }],
183 errors: [getExpectedErrorEOF(2)]
184 },
185 {
186 code: "// invalid 5\nvar a=5;\n\n\n\nvar b = 3;",
187 output: "// invalid 5\nvar a=5;\n\n\nvar b = 3;",
188 options: [{ max: 2 }],
189 errors: [getExpectedError(2)]
190 },
191 {
192 code: "// invalid 6\nvar a=5;\n\n\n\nvar b = 3;\n",
193 output: "// invalid 6\nvar a=5;\n\n\nvar b = 3;\n",
194 options: [{ max: 2 }],
195 errors: [getExpectedError(2)]
196 },
197 {
198 code: "// invalid 7\nvar a = 5;\n\n\n\nb = 3;\nvar c = 5;\n\n\n\nvar d = 3;",
199 output: "// invalid 7\nvar a = 5;\n\n\nb = 3;\nvar c = 5;\n\n\nvar d = 3;",
200 options: [{ max: 2 }],
201 errors: 2
202 },
203 {
204 code: "// invalid 8\nvar a = 5;\n\n\n\n\n\n\n\n\n\n\n\n\n\nb = 3;",
205 output: "// invalid 8\nvar a = 5;\n\n\nb = 3;",
206 options: [{ max: 2 }],
207 errors: [getExpectedError(2)]
208 },
209 {
210 code: "// invalid 9\nvar a=5;\n\n\n\n\n",
211 output: "// invalid 9\nvar a=5;\n\n\n",
212 options: [{ max: 2 }],
213 errors: [getExpectedErrorEOF(2)]
214 },
215 {
216 code: "// invalid 10\nvar a = 5;\n\nvar b = 3;",
217 output: "// invalid 10\nvar a = 5;\nvar b = 3;",
218 options: [{ max: 0 }],
219 errors: [getExpectedError(0)]
220 },
221 {
222 code: "// invalid 11\nvar a = 5;\n\n\n",
223 output: "// invalid 11\nvar a = 5;\n\n",
224 options: [{ max: 5, maxEOF: 1 }],
225 errors: [getExpectedErrorEOF(1)]
226 },
227 {
228 code: "// invalid 12\nvar a = 5;\n\n\n\n\n\n",
229 output: "// invalid 12\nvar a = 5;\n\n\n\n\n",
230 options: [{ max: 0, maxEOF: 4 }],
231 errors: [getExpectedErrorEOF(4)]
232 },
233 {
234 code: "// invalid 13\n\n\n\n\n\n\n\n\nvar a = 5;\n\n\n",
235 output: "// invalid 13\n\n\n\n\n\n\n\n\nvar a = 5;\n\n",
236 options: [{ max: 10, maxEOF: 1 }],
237 errors: [getExpectedErrorEOF(1)]
238 },
239 {
240 code: "// invalid 14\nvar a = 5;\n\n",
241 output: "// invalid 14\nvar a = 5;\n",
242 options: [{ max: 2, maxEOF: 0 }],
243 errors: [getExpectedErrorEOF(0)]
244 },
245 {
246 code: "\n\n// invalid 15\nvar a = 5;\n",
247 output: "\n// invalid 15\nvar a = 5;\n",
248 options: [{ max: 5, maxBOF: 1 }],
249 errors: [getExpectedErrorBOF(1)]
250 },
251 {
252 code: "\n\n\n\n\n// invalid 16\nvar a = 5;\n",
253 output: "\n\n\n\n// invalid 16\nvar a = 5;\n",
254 options: [{ max: 0, maxBOF: 4 }],
255 errors: [getExpectedErrorBOF(4)]
256 },
257 {
258 code: "\n\n// invalid 17\n\n\n\n\n\n\n\n\nvar a = 5;\n",
259 output: "\n// invalid 17\n\n\n\n\n\n\n\n\nvar a = 5;\n",
260 options: [{ max: 10, maxBOF: 1 }],
261 errors: [getExpectedErrorBOF(1)]
262 },
263 {
264 code: "\n// invalid 18\nvar a = 5;\n",
265 output: "// invalid 18\nvar a = 5;\n",
266 options: [{ max: 2, maxBOF: 0 }],
267 errors: [getExpectedErrorBOF(0)]
268 },
269 {
270 code: "\n\n\n// invalid 19\nvar a = 5;\n\n",
271 output: "// invalid 19\nvar a = 5;\n",
272 options: [{ max: 2, maxBOF: 0, maxEOF: 0 }],
273 errors: [getExpectedErrorBOF(0),
274 getExpectedErrorEOF(0)]
275 },
276 {
277 code: "// invalid 20\r\n// windows line endings\r\nvar a = 5;\r\nvar b = 3;\r\n\r\n\r\n",
278 output: "// invalid 20\r\n// windows line endings\r\nvar a = 5;\r\nvar b = 3;\r\n\r\n",
279 options: [{ max: 1 }],
280 errors: [getExpectedErrorEOF(1)]
281 },
282 {
283 code: "// invalid 21\n// unix line endings\nvar a = 5;\nvar b = 3;\n\n\n",
284 output: "// invalid 21\n// unix line endings\nvar a = 5;\nvar b = 3;\n\n",
285 options: [{ max: 1 }],
286 errors: [getExpectedErrorEOF(1)]
287 },
288 {
289 code:
290 "'foo';\n" +
291 "\n" +
292 "\n" +
293 "`bar`;\n" +
294 "`baz`;",
295 output:
296 "'foo';\n" +
297 "\n" +
298 "`bar`;\n" +
299 "`baz`;",
300 options: [{ max: 1 }],
301 parserOptions: { ecmaVersion: 6 },
302 errors: [getExpectedError(1)]
303 },
304 {
305 code: "`template ${foo\n\n\n} literal`;",
306 output: "`template ${foo\n\n} literal`;",
307 options: [{ max: 1 }],
308 parserOptions: { ecmaVersion: 6 },
309 errors: [getExpectedError(1)]
310 },
311 {
312
313 // https://github.com/eslint/eslint/issues/7893
314 code: `a\n\n\n\n${"a".repeat(1e5)}`,
315 output: `a\n\n\n${"a".repeat(1e5)}`,
316 errors: [getExpectedError(2)]
317 },
318 {
319
320 // https://github.com/eslint/eslint/issues/8401
321 code: "foo\n ",
322 output: "foo\n",
323 options: [{ max: 1, maxEOF: 0 }],
324 errors: [getExpectedErrorEOF(0)]
325 },
326 {
327
328 // https://github.com/eslint/eslint/pull/12594
329 code: "var a;\n\n\n\n\nvar b;",
330 output: "var a;\n\nvar b;",
331 options: [{ max: 1 }],
332 errors: [{
333 messageId: "consecutiveBlank",
334 data: {
335 max: 1,
336 pluralizedLines: "line"
337 },
338 type: "Program",
339 line: 3,
340 column: 1
341 }]
342 },
343 {
344
345 // https://github.com/eslint/eslint/pull/12594
346 code: "var a;\n\n\n\n\nvar b;",
347 output: "var a;\n\n\nvar b;",
348 options: [{ max: 2 }],
349 errors: [{
350 messageId: "consecutiveBlank",
351 data: {
352 max: 2,
353 pluralizedLines: "lines"
354 },
355 type: "Program",
356 line: 4,
357 column: 1
358 }]
359 }
360 ]
361 });