]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | /** |
2 | * @fileoverview enforce a maximum file length | |
3 | * @author Alberto RodrÃguez | |
4 | */ | |
5 | "use strict"; | |
6 | ||
7 | //------------------------------------------------------------------------------ | |
8 | // Requirements | |
9 | //------------------------------------------------------------------------------ | |
10 | ||
11 | const rule = require("../../../lib/rules/max-lines"), | |
eb39fafa DC |
12 | { RuleTester } = require("../../../lib/rule-tester"); |
13 | ||
eb39fafa DC |
14 | //------------------------------------------------------------------------------ |
15 | // Tests | |
16 | //------------------------------------------------------------------------------ | |
17 | ||
18 | const ruleTester = new RuleTester(); | |
19 | ||
20 | ruleTester.run("max-lines", rule, { | |
21 | valid: [ | |
22 | "var x;", | |
23 | "var xy;\nvar xy;", | |
6f036462 TL |
24 | { code: "A", options: [1] }, |
25 | { code: "A\n", options: [1] }, | |
26 | { code: "A\r", options: [1] }, | |
27 | { code: "A\r\n", options: [1] }, | |
eb39fafa | 28 | { code: "var xy;\nvar xy;", options: [2] }, |
6f036462 | 29 | { code: "var xy;\nvar xy;\n", options: [2] }, |
eb39fafa | 30 | { code: "var xy;\nvar xy;", options: [{ max: 2 }] }, |
6f036462 TL |
31 | { code: "// comment\n", options: [{ max: 0, skipComments: true }] }, |
32 | { code: "foo;\n /* comment */\n", options: [{ max: 1, skipComments: true }] }, | |
eb39fafa DC |
33 | { |
34 | code: [ | |
35 | "//a single line comment", | |
36 | "var xy;", | |
37 | "var xy;", | |
38 | " /* a multiline", | |
39 | " really really", | |
40 | " long comment*/ " | |
41 | ].join("\n"), | |
42 | options: [{ max: 2, skipComments: true }] | |
43 | }, | |
44 | { | |
45 | code: [ | |
46 | "var x; /* inline comment", | |
47 | " spanning multiple lines */ var z;" | |
48 | ].join("\n"), | |
49 | options: [{ max: 2, skipComments: true }] | |
50 | }, | |
51 | { | |
52 | code: [ | |
53 | "var x; /* inline comment", | |
54 | " spanning multiple lines */", | |
55 | "var z;" | |
56 | ].join("\n"), | |
57 | options: [{ max: 2, skipComments: true }] | |
58 | }, | |
59 | { | |
60 | code: [ | |
61 | "var x;", | |
62 | "", | |
63 | "\t", | |
64 | "\t ", | |
65 | "var y;" | |
66 | ].join("\n"), | |
67 | options: [{ max: 2, skipBlankLines: true }] | |
68 | }, | |
69 | { | |
70 | code: [ | |
71 | "//a single line comment", | |
72 | "var xy;", | |
73 | " ", | |
74 | "var xy;", | |
75 | " ", | |
76 | " /* a multiline", | |
77 | " really really", | |
78 | " long comment*/" | |
79 | ].join("\n"), | |
80 | options: [{ max: 2, skipComments: true, skipBlankLines: true }] | |
81 | } | |
82 | ], | |
83 | invalid: [ | |
84 | { | |
85 | code: "var xyz;\nvar xyz;\nvar xyz;", | |
86 | options: [2], | |
6f036462 TL |
87 | errors: [ |
88 | { | |
89 | messageId: "exceed", | |
90 | data: { max: 2, actual: 3 }, | |
91 | line: 3, | |
92 | column: 1, | |
93 | endLine: 3, | |
94 | endColumn: 9 | |
95 | } | |
96 | ] | |
eb39fafa DC |
97 | }, |
98 | { | |
99 | code: "/* a multiline comment\n that goes to many lines*/\nvar xy;\nvar xy;", | |
100 | options: [2], | |
6f036462 TL |
101 | errors: [ |
102 | { | |
103 | messageId: "exceed", | |
104 | data: { max: 2, actual: 4 }, | |
105 | line: 3, | |
106 | column: 1, | |
107 | endLine: 4, | |
108 | endColumn: 8 | |
109 | } | |
110 | ] | |
eb39fafa DC |
111 | }, |
112 | { | |
113 | code: "//a single line comment\nvar xy;\nvar xy;", | |
114 | options: [2], | |
6f036462 TL |
115 | errors: [ |
116 | { | |
117 | messageId: "exceed", | |
118 | data: { max: 2, actual: 3 }, | |
119 | line: 3, | |
120 | column: 1, | |
121 | endLine: 3, | |
122 | endColumn: 8 | |
123 | } | |
124 | ] | |
eb39fafa DC |
125 | }, |
126 | { | |
127 | code: [ | |
128 | "var x;", | |
129 | "", | |
130 | "", | |
131 | "", | |
132 | "var y;" | |
133 | ].join("\n"), | |
134 | options: [{ max: 2 }], | |
6f036462 TL |
135 | errors: [ |
136 | { | |
137 | messageId: "exceed", | |
138 | data: { max: 2, actual: 5 }, | |
139 | line: 3, | |
140 | column: 1, | |
141 | endLine: 5, | |
142 | endColumn: 7 | |
143 | } | |
144 | ] | |
eb39fafa DC |
145 | }, |
146 | { | |
147 | code: [ | |
148 | "//a single line comment", | |
149 | "var xy;", | |
150 | " ", | |
151 | "var xy;", | |
152 | " ", | |
153 | " /* a multiline", | |
154 | " really really", | |
155 | " long comment*/" | |
156 | ].join("\n"), | |
157 | options: [{ max: 2, skipComments: true }], | |
6f036462 TL |
158 | errors: [ |
159 | { | |
160 | messageId: "exceed", | |
161 | data: { max: 2, actual: 4 }, | |
162 | line: 4, | |
163 | column: 1, | |
164 | endLine: 8, | |
165 | endColumn: 16 | |
166 | } | |
167 | ] | |
eb39fafa DC |
168 | }, |
169 | { | |
170 | code: [ | |
171 | "var x; // inline comment", | |
172 | "var y;", | |
173 | "var z;" | |
174 | ].join("\n"), | |
175 | options: [{ max: 2, skipComments: true }], | |
6f036462 TL |
176 | errors: [ |
177 | { | |
178 | messageId: "exceed", | |
179 | data: { max: 2, actual: 3 }, | |
180 | line: 3, | |
181 | column: 1, | |
182 | endLine: 3, | |
183 | endColumn: 7 | |
184 | } | |
185 | ] | |
eb39fafa DC |
186 | }, |
187 | { | |
188 | code: [ | |
189 | "var x; /* inline comment", | |
190 | " spanning multiple lines */", | |
191 | "var y;", | |
192 | "var z;" | |
193 | ].join("\n"), | |
194 | options: [{ max: 2, skipComments: true }], | |
6f036462 TL |
195 | errors: [ |
196 | { | |
197 | messageId: "exceed", | |
198 | data: { max: 2, actual: 3 }, | |
199 | line: 4, | |
200 | column: 1, | |
201 | endLine: 4, | |
202 | endColumn: 7 | |
203 | } | |
204 | ] | |
eb39fafa DC |
205 | }, |
206 | { | |
207 | code: [ | |
208 | "//a single line comment", | |
209 | "var xy;", | |
210 | " ", | |
211 | "var xy;", | |
212 | " ", | |
213 | " /* a multiline", | |
214 | " really really", | |
215 | " long comment*/" | |
216 | ].join("\n"), | |
217 | options: [{ max: 2, skipBlankLines: true }], | |
6f036462 TL |
218 | errors: [ |
219 | { | |
220 | messageId: "exceed", | |
221 | data: { max: 2, actual: 6 }, | |
222 | line: 4, | |
223 | column: 1, | |
224 | endLine: 8, | |
225 | endColumn: 16 | |
226 | } | |
227 | ] | |
eb39fafa DC |
228 | }, |
229 | { | |
230 | code: "AAAAAAAA\n".repeat(301).trim(), | |
231 | options: [{}], | |
6f036462 TL |
232 | errors: [ |
233 | { | |
234 | messageId: "exceed", | |
235 | data: { max: 300, actual: 301 }, | |
236 | line: 301, | |
237 | column: 1, | |
238 | endLine: 301, | |
239 | endColumn: 9 | |
240 | } | |
241 | ] | |
242 | }, | |
243 | { | |
244 | ||
245 | // Questionable. Makes sense to report this, and makes sense to not report this. | |
246 | code: "", | |
247 | options: [{ max: 0 }], | |
248 | errors: [ | |
249 | { | |
250 | messageId: "exceed", | |
251 | data: { max: 0, actual: 1 }, | |
252 | line: 1, | |
253 | column: 1, | |
254 | endLine: 1, | |
255 | endColumn: 1 | |
256 | } | |
257 | ] | |
258 | }, | |
259 | { | |
260 | code: " ", | |
261 | options: [{ max: 0 }], | |
262 | errors: [ | |
263 | { | |
264 | messageId: "exceed", | |
265 | data: { max: 0, actual: 1 }, | |
266 | line: 1, | |
267 | column: 1, | |
268 | endLine: 1, | |
269 | endColumn: 2 | |
270 | } | |
271 | ] | |
272 | }, | |
273 | { | |
274 | code: "\n", | |
275 | options: [{ max: 0 }], | |
276 | errors: [ | |
277 | { | |
278 | messageId: "exceed", | |
279 | data: { max: 0, actual: 1 }, | |
280 | line: 1, | |
281 | column: 1, | |
282 | endLine: 2, | |
283 | endColumn: 1 | |
284 | } | |
285 | ] | |
eb39fafa DC |
286 | }, |
287 | { | |
288 | code: "A", | |
289 | options: [{ max: 0 }], | |
6f036462 TL |
290 | errors: [ |
291 | { | |
292 | messageId: "exceed", | |
293 | data: { max: 0, actual: 1 }, | |
294 | line: 1, | |
295 | column: 1, | |
296 | endLine: 1, | |
297 | endColumn: 2 | |
298 | } | |
299 | ] | |
300 | }, | |
301 | { | |
302 | code: "A\n", | |
303 | options: [{ max: 0 }], | |
304 | errors: [ | |
305 | { | |
306 | messageId: "exceed", | |
307 | data: { max: 0, actual: 1 }, | |
308 | line: 1, | |
309 | column: 1, | |
310 | endLine: 2, | |
311 | endColumn: 1 | |
312 | } | |
313 | ] | |
314 | }, | |
315 | { | |
316 | code: "A\n ", | |
317 | options: [{ max: 0 }], | |
318 | errors: [ | |
319 | { | |
320 | messageId: "exceed", | |
321 | data: { max: 0, actual: 2 }, | |
322 | line: 1, | |
323 | column: 1, | |
324 | endLine: 2, | |
325 | endColumn: 2 | |
326 | } | |
327 | ] | |
328 | }, | |
329 | { | |
330 | code: "A\n ", | |
331 | options: [{ max: 1 }], | |
332 | errors: [ | |
333 | { | |
334 | messageId: "exceed", | |
335 | data: { max: 1, actual: 2 }, | |
336 | line: 2, | |
337 | column: 1, | |
338 | endLine: 2, | |
339 | endColumn: 2 | |
340 | } | |
341 | ] | |
342 | }, | |
343 | { | |
344 | code: "A\n\n", | |
345 | options: [{ max: 1 }], | |
346 | errors: [ | |
347 | { | |
348 | messageId: "exceed", | |
349 | data: { max: 1, actual: 2 }, | |
350 | line: 2, | |
351 | column: 1, | |
352 | endLine: 3, | |
353 | endColumn: 1 | |
354 | } | |
355 | ] | |
356 | }, | |
357 | { | |
358 | code: ["var a = 'a'; ", "var x", "var c;", "console.log"].join( | |
359 | "\n" | |
360 | ), | |
361 | options: [{ max: 2 }], | |
362 | errors: [ | |
363 | { | |
364 | messageId: "exceed", | |
365 | data: { max: 2, actual: 4 }, | |
366 | line: 3, | |
367 | column: 1, | |
368 | endLine: 4, | |
369 | endColumn: 12 | |
370 | } | |
371 | ] | |
372 | }, | |
373 | { | |
374 | code: "var a = 'a',\nc,\nx;\r", | |
375 | options: [{ max: 2 }], | |
376 | errors: [ | |
377 | { | |
378 | messageId: "exceed", | |
379 | data: { max: 2, actual: 3 }, | |
380 | line: 3, | |
381 | column: 1, | |
382 | endLine: 4, | |
383 | endColumn: 1 | |
384 | } | |
385 | ] | |
386 | }, | |
387 | { | |
388 | code: "var a = 'a',\nc,\nx;\n", | |
389 | options: [{ max: 2 }], | |
390 | errors: [ | |
391 | { | |
392 | messageId: "exceed", | |
393 | data: { max: 2, actual: 3 }, | |
394 | line: 3, | |
395 | column: 1, | |
396 | endLine: 4, | |
397 | endColumn: 1 | |
398 | } | |
399 | ] | |
400 | }, | |
401 | { | |
402 | code: "\n\nvar a = 'a',\nc,\nx;\n", | |
403 | options: [{ max: 2, skipBlankLines: true }], | |
404 | errors: [ | |
405 | { | |
406 | messageId: "exceed", | |
407 | data: { max: 2, actual: 3 }, | |
408 | line: 5, | |
409 | column: 1, | |
410 | endLine: 6, | |
411 | endColumn: 1 | |
412 | } | |
413 | ] | |
414 | }, | |
415 | { | |
416 | code: [ | |
417 | "var a = 'a'; ", | |
418 | "var x", | |
419 | "var c;", | |
420 | "console.log", | |
421 | "// some block ", | |
422 | "// comments" | |
423 | ].join("\n"), | |
424 | options: [{ max: 2, skipComments: true }], | |
425 | errors: [ | |
426 | { | |
427 | messageId: "exceed", | |
428 | data: { max: 2, actual: 4 }, | |
429 | line: 3, | |
430 | column: 1, | |
431 | endLine: 6, | |
432 | endColumn: 12 | |
433 | } | |
434 | ] | |
435 | }, | |
436 | { | |
437 | code: [ | |
438 | "var a = 'a'; ", | |
439 | "var x", | |
440 | "var c;", | |
441 | "console.log", | |
442 | "/* block comments */" | |
443 | ].join("\n"), | |
444 | options: [{ max: 2, skipComments: true }], | |
445 | errors: [ | |
446 | { | |
447 | messageId: "exceed", | |
448 | data: { max: 2, actual: 4 }, | |
449 | line: 3, | |
450 | column: 1, | |
451 | endLine: 5, | |
452 | endColumn: 21 | |
453 | } | |
454 | ] | |
455 | }, | |
456 | { | |
457 | code: [ | |
458 | "var a = 'a'; ", | |
459 | "var x", | |
460 | "var c;", | |
461 | "console.log", | |
462 | "/* block comments */\n" | |
463 | ].join("\n"), | |
464 | options: [{ max: 2, skipComments: true }], | |
465 | errors: [ | |
466 | { | |
467 | messageId: "exceed", | |
468 | data: { max: 2, actual: 4 }, | |
469 | line: 3, | |
470 | column: 1, | |
471 | endLine: 6, | |
472 | endColumn: 1 | |
473 | } | |
474 | ] | |
475 | }, | |
476 | { | |
477 | code: [ | |
478 | "var a = 'a'; ", | |
479 | "var x", | |
480 | "var c;", | |
481 | "console.log", | |
482 | "/** block \n\n comments */" | |
483 | ].join("\n"), | |
484 | options: [{ max: 2, skipComments: true }], | |
485 | errors: [ | |
486 | { | |
487 | messageId: "exceed", | |
488 | data: { max: 2, actual: 4 }, | |
489 | line: 3, | |
490 | column: 1, | |
491 | endLine: 7, | |
492 | endColumn: 13 | |
493 | } | |
494 | ] | |
495 | }, | |
496 | { | |
497 | code: [ | |
498 | "var a = 'a'; ", | |
499 | "", | |
500 | "", | |
501 | "// comment" | |
502 | ].join("\n"), | |
503 | options: [{ max: 2, skipComments: true }], | |
504 | errors: [ | |
505 | { | |
506 | messageId: "exceed", | |
507 | data: { max: 2, actual: 3 }, | |
508 | line: 3, | |
509 | column: 1, | |
510 | endLine: 4, | |
511 | endColumn: 11 | |
512 | } | |
513 | ] | |
514 | }, | |
515 | { | |
516 | code: [ | |
517 | "var a = 'a'; ", | |
518 | "var x", | |
519 | "\n", | |
520 | "var c;", | |
521 | "console.log", | |
522 | "\n" | |
523 | ].join("\n"), | |
524 | options: [{ max: 2, skipBlankLines: true }], | |
525 | errors: [ | |
526 | { | |
527 | messageId: "exceed", | |
528 | data: { max: 2, actual: 4 }, | |
529 | line: 5, | |
530 | column: 1, | |
531 | endLine: 8, | |
532 | endColumn: 1 | |
533 | } | |
534 | ] | |
535 | }, | |
536 | { | |
537 | code: [ | |
538 | "var a = 'a'; ", | |
539 | "\n", | |
540 | "var x", | |
541 | "var c;", | |
542 | "console.log", | |
543 | "\n" | |
544 | ].join("\n"), | |
545 | options: [{ max: 2, skipBlankLines: true }], | |
546 | errors: [ | |
547 | { | |
548 | messageId: "exceed", | |
549 | data: { max: 2, actual: 4 }, | |
550 | line: 5, | |
551 | column: 1, | |
552 | endLine: 8, | |
553 | endColumn: 1 | |
554 | } | |
555 | ] | |
556 | }, | |
557 | { | |
558 | code: [ | |
559 | "var a = 'a'; ", | |
560 | "//", | |
561 | "var x", | |
562 | "var c;", | |
563 | "console.log", | |
564 | "//" | |
565 | ].join("\n"), | |
566 | options: [{ max: 2, skipComments: true }], | |
567 | errors: [ | |
568 | { | |
569 | messageId: "exceed", | |
570 | data: { max: 2, actual: 4 }, | |
571 | line: 4, | |
572 | column: 1, | |
573 | endLine: 6, | |
574 | endColumn: 3 | |
575 | } | |
576 | ] | |
577 | }, | |
578 | { | |
579 | code: ["// hello world", "/*hello", " world 2 */", "var a,", "b", "// hh", "c,", "e,", "f;"].join("\n"), | |
580 | options: [{ max: 2, skipComments: true }], | |
581 | errors: [{ | |
582 | data: { max: 2, actual: 5 }, | |
583 | messageId: "exceed", | |
584 | line: 7, | |
585 | column: 1, | |
586 | endLine: 9, | |
587 | endColumn: 3 | |
588 | ||
589 | }] | |
590 | }, | |
591 | { | |
592 | code: ["", "var x = '';", "", "// comment", "", "var b = '',", "c,", "d,", "e", "", "// comment"].join("\n"), | |
593 | options: [{ max: 2, skipComments: true, skipBlankLines: true }], | |
594 | errors: [{ | |
595 | data: { max: 2, actual: 5 }, | |
596 | messageId: "exceed", | |
597 | line: 7, | |
598 | column: 1, | |
599 | endLine: 11, | |
600 | endColumn: 11 | |
601 | }] | |
eb39fafa | 602 | } |
6f036462 | 603 | |
eb39fafa DC |
604 | ] |
605 | }); |