]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | /** |
2 | * @fileoverview Tests for semi-style rule. | |
3 | * @author Toru Nagashima | |
4 | */ | |
5 | ||
6 | "use strict"; | |
7 | ||
8 | //------------------------------------------------------------------------------ | |
9 | // Requirements | |
10 | //------------------------------------------------------------------------------ | |
11 | ||
12 | const rule = require("../../../lib/rules/semi-style"), | |
13 | { RuleTester } = require("../../../lib/rule-tester"); | |
14 | ||
15 | //------------------------------------------------------------------------------ | |
16 | // Tests | |
17 | //------------------------------------------------------------------------------ | |
18 | ||
19 | const ruleTester = new RuleTester(); | |
20 | ||
21 | ruleTester.run("semi-style", rule, { | |
22 | valid: [ | |
23 | ";", | |
24 | ";foo;bar;baz;", | |
25 | "foo;\nbar;", | |
26 | "for(a;b;c);", | |
27 | "for(a;\nb;\nc);", | |
28 | "for((a\n);\n(b\n);\n(c));", | |
29 | "if(a)foo;\nbar", | |
30 | { code: ";", options: ["last"] }, | |
31 | { code: ";foo;bar;baz;", options: ["last"] }, | |
32 | { code: "foo;\nbar;", options: ["last"] }, | |
33 | { code: "for(a;b;c);", options: ["last"] }, | |
34 | { code: "for(a;\nb;\nc);", options: ["last"] }, | |
35 | { code: "for((a\n);\n(b\n);\n(c));", options: ["last"] }, | |
609c276f TL |
36 | { code: "class C { a; b; }", options: ["last"], parserOptions: { ecmaVersion: 2022 } }, |
37 | { code: "class C {\na;\nb;\n}", options: ["last"], parserOptions: { ecmaVersion: 2022 } }, | |
eb39fafa DC |
38 | { code: "if(a)foo;\nbar", options: ["last"] }, |
39 | { code: ";", options: ["first"] }, | |
40 | { code: ";foo;bar;baz;", options: ["first"] }, | |
41 | { code: "foo\n;bar;", options: ["first"] }, | |
42 | { code: "for(a;b;c);", options: ["first"] }, | |
43 | { code: "for(a;\nb;\nc);", options: ["first"] }, | |
44 | { code: "for((a\n);\n(b\n);\n(c));", options: ["first"] }, | |
609c276f TL |
45 | { code: "class C { a ;b }", options: ["first"], parserOptions: { ecmaVersion: 2022 } }, |
46 | { code: "class C {\na\n;b\n}", options: ["first"], parserOptions: { ecmaVersion: 2022 } }, | |
eb39fafa DC |
47 | |
48 | // edge cases | |
49 | { | |
50 | code: ` | |
51 | { | |
52 | ; | |
53 | } | |
54 | `, | |
55 | options: ["first"] | |
56 | }, | |
57 | { | |
58 | code: ` | |
59 | while (a) | |
60 | ; | |
61 | foo | |
62 | `, | |
63 | options: ["first"] | |
64 | }, | |
65 | { | |
66 | code: ` | |
67 | do | |
68 | ; | |
69 | while (a) | |
70 | `, | |
71 | options: ["first"] | |
72 | }, | |
73 | { | |
74 | code: ` | |
75 | do | |
76 | foo; | |
77 | while (a) | |
78 | `, | |
79 | options: ["first"] | |
80 | }, | |
81 | { | |
82 | code: ` | |
83 | if (a) | |
84 | foo; | |
85 | else | |
86 | bar | |
87 | `, | |
88 | options: ["first"] | |
89 | }, | |
90 | { | |
91 | code: ` | |
92 | if (a) | |
93 | foo | |
94 | ;bar | |
95 | `, | |
96 | options: ["first"] | |
97 | }, | |
98 | { | |
99 | code: ` | |
100 | { | |
101 | ; | |
102 | } | |
103 | `, | |
104 | options: ["last"] | |
105 | }, | |
106 | { | |
107 | code: ` | |
108 | switch (a) { | |
109 | case 1: | |
110 | ;foo | |
111 | } | |
112 | `, | |
113 | options: ["last"] | |
114 | }, | |
115 | { | |
116 | code: ` | |
117 | while (a) | |
118 | ; | |
119 | foo | |
120 | `, | |
121 | options: ["last"] | |
122 | }, | |
123 | { | |
124 | code: ` | |
125 | do | |
126 | ; | |
127 | while (a) | |
128 | `, | |
129 | options: ["last"] | |
609c276f TL |
130 | }, |
131 | ||
132 | // Class static blocks | |
133 | { | |
134 | code: ` | |
135 | class C { | |
136 | static {} | |
137 | } | |
138 | `, | |
139 | options: ["last"], | |
140 | parserOptions: { ecmaVersion: 2022 } | |
141 | }, | |
142 | { | |
143 | code: ` | |
144 | class C { | |
145 | static { | |
146 | foo | |
147 | } | |
148 | } | |
149 | `, | |
150 | options: ["last"], | |
151 | parserOptions: { ecmaVersion: 2022 } | |
152 | }, | |
153 | { | |
154 | code: ` | |
155 | class C { | |
156 | static { | |
157 | foo | |
158 | bar | |
159 | } | |
160 | } | |
161 | `, | |
162 | options: ["last"], | |
163 | parserOptions: { ecmaVersion: 2022 } | |
164 | }, | |
165 | { | |
166 | code: ` | |
167 | class C { | |
168 | static { | |
169 | ; | |
170 | } | |
171 | } | |
172 | `, | |
173 | options: ["last"], | |
174 | parserOptions: { ecmaVersion: 2022 } | |
175 | }, | |
176 | { | |
177 | code: ` | |
178 | class C { | |
179 | static { | |
180 | foo; | |
181 | } | |
182 | } | |
183 | `, | |
184 | options: ["last"], | |
185 | parserOptions: { ecmaVersion: 2022 } | |
186 | }, | |
187 | { | |
188 | code: ` | |
189 | class C { | |
190 | static { | |
191 | foo; | |
192 | bar; | |
193 | } | |
194 | } | |
195 | `, | |
196 | options: ["last"], | |
197 | parserOptions: { ecmaVersion: 2022 } | |
198 | }, | |
199 | { | |
200 | code: ` | |
201 | class C { | |
202 | static { | |
203 | foo; | |
204 | bar; | |
205 | baz; | |
206 | } | |
207 | } | |
208 | `, | |
209 | options: ["last"], | |
210 | parserOptions: { ecmaVersion: 2022 } | |
211 | }, | |
212 | { | |
213 | code: ` | |
214 | class C { | |
215 | static {} | |
216 | } | |
217 | `, | |
218 | options: ["first"], | |
219 | parserOptions: { ecmaVersion: 2022 } | |
220 | }, | |
221 | { | |
222 | code: ` | |
223 | class C { | |
224 | static { | |
225 | foo | |
226 | } | |
227 | } | |
228 | `, | |
229 | options: ["first"], | |
230 | parserOptions: { ecmaVersion: 2022 } | |
231 | }, | |
232 | { | |
233 | code: ` | |
234 | class C { | |
235 | static { | |
236 | foo | |
237 | bar | |
238 | } | |
239 | } | |
240 | `, | |
241 | options: ["first"], | |
242 | parserOptions: { ecmaVersion: 2022 } | |
243 | }, | |
244 | { | |
245 | code: ` | |
246 | class C { | |
247 | static { | |
248 | ; | |
249 | } | |
250 | } | |
251 | `, | |
252 | options: ["first"], | |
253 | parserOptions: { ecmaVersion: 2022 } | |
254 | }, | |
255 | { | |
256 | code: ` | |
257 | class C { | |
258 | static { | |
259 | ;foo | |
260 | } | |
261 | } | |
262 | `, | |
263 | options: ["first"], | |
264 | parserOptions: { ecmaVersion: 2022 } | |
265 | }, | |
266 | { | |
267 | code: ` | |
268 | class C { | |
269 | static { | |
270 | foo; | |
271 | } | |
272 | } | |
273 | `, | |
274 | options: ["first"], | |
275 | parserOptions: { ecmaVersion: 2022 } | |
276 | }, | |
277 | { | |
278 | code: ` | |
279 | class C { | |
280 | static { | |
281 | foo | |
282 | ;bar | |
283 | } | |
284 | } | |
285 | `, | |
286 | options: ["first"], | |
287 | parserOptions: { ecmaVersion: 2022 } | |
288 | }, | |
289 | { | |
290 | code: ` | |
291 | class C { | |
292 | static { | |
293 | foo | |
294 | ;bar; | |
295 | } | |
296 | } | |
297 | `, | |
298 | options: ["first"], | |
299 | parserOptions: { ecmaVersion: 2022 } | |
300 | }, | |
301 | { | |
302 | code: ` | |
303 | class C { | |
304 | static { | |
305 | foo | |
306 | ;bar | |
307 | ;baz | |
308 | } | |
309 | } | |
310 | `, | |
311 | options: ["first"], | |
312 | parserOptions: { ecmaVersion: 2022 } | |
313 | }, | |
314 | { | |
315 | code: ` | |
316 | class C { | |
317 | static { | |
318 | foo | |
319 | ;bar | |
320 | ;baz; | |
321 | } | |
322 | } | |
323 | `, | |
324 | options: ["first"], | |
325 | parserOptions: { ecmaVersion: 2022 } | |
eb39fafa DC |
326 | } |
327 | ], | |
328 | invalid: [ | |
329 | { | |
330 | code: "foo\n;bar", | |
331 | output: "foo;\nbar", | |
332 | errors: [{ | |
333 | messageId: "expectedSemiColon", | |
334 | data: { | |
335 | pos: "the end of the previous line" | |
336 | } | |
337 | }] | |
338 | }, | |
339 | { | |
340 | code: "if(a)foo\n;bar", | |
341 | output: "if(a)foo;\nbar", | |
342 | errors: [{ | |
343 | messageId: "expectedSemiColon", | |
344 | data: { | |
345 | pos: "the end of the previous line" | |
346 | } | |
347 | }] | |
348 | }, | |
349 | { | |
350 | code: "var foo\n;bar", | |
351 | output: "var foo;\nbar", | |
352 | errors: [{ | |
353 | messageId: "expectedSemiColon", | |
354 | data: { | |
355 | pos: "the end of the previous line" | |
356 | } | |
357 | }] | |
358 | }, | |
359 | { | |
360 | code: "foo\n;\nbar", | |
361 | output: "foo;\nbar", | |
362 | errors: [{ | |
363 | messageId: "expectedSemiColon", | |
364 | data: { | |
365 | pos: "the end of the previous line" | |
366 | } | |
367 | }] | |
368 | }, | |
369 | { | |
370 | code: "for(a\n;b;c)d", | |
371 | output: "for(a;\nb;c)d", | |
372 | errors: [{ | |
373 | messageId: "expectedSemiColon", | |
374 | data: { | |
375 | pos: "the end of the previous line" | |
376 | } | |
377 | }] | |
378 | }, | |
379 | { | |
380 | code: "for(a;b\n;c)d", | |
381 | output: "for(a;b;\nc)d", | |
382 | errors: [{ | |
383 | messageId: "expectedSemiColon", | |
384 | data: { | |
385 | pos: "the end of the previous line" | |
386 | } | |
387 | }] | |
388 | }, | |
389 | { | |
390 | code: "do;while(a)\n;b", | |
391 | output: "do;while(a);\nb", | |
392 | errors: [{ | |
393 | messageId: "expectedSemiColon", | |
394 | data: { | |
395 | pos: "the end of the previous line" | |
396 | } | |
397 | }] | |
398 | }, | |
399 | ||
400 | { | |
401 | code: "foo\n;bar", | |
402 | output: "foo;\nbar", | |
403 | options: ["last"], | |
404 | errors: [{ | |
405 | messageId: "expectedSemiColon", | |
406 | data: { | |
407 | pos: "the end of the previous line" | |
408 | } | |
409 | }] | |
410 | }, | |
411 | { | |
412 | code: "if(a)foo\n;bar", | |
413 | output: "if(a)foo;\nbar", | |
414 | options: ["last"], | |
415 | errors: [{ | |
416 | messageId: "expectedSemiColon", | |
417 | data: { | |
418 | pos: "the end of the previous line" | |
419 | } | |
420 | }] | |
421 | }, | |
422 | { | |
423 | code: "var foo\n;bar", | |
424 | output: "var foo;\nbar", | |
425 | options: ["last"], | |
426 | errors: [{ | |
427 | messageId: "expectedSemiColon", | |
428 | data: { | |
429 | pos: "the end of the previous line" | |
430 | } | |
431 | }] | |
432 | }, | |
433 | { | |
434 | code: "foo\n;\nbar", | |
435 | output: "foo;\nbar", | |
436 | options: ["last"], | |
437 | errors: [{ | |
438 | messageId: "expectedSemiColon", | |
439 | data: { | |
440 | pos: "the end of the previous line" | |
441 | } | |
442 | }] | |
443 | }, | |
444 | { | |
445 | code: "for(a\n;b;c)d", | |
446 | output: "for(a;\nb;c)d", | |
447 | options: ["last"], | |
448 | errors: [{ | |
449 | messageId: "expectedSemiColon", | |
450 | data: { | |
451 | pos: "the end of the previous line" | |
452 | } | |
453 | }] | |
454 | }, | |
455 | { | |
456 | code: "for(a;b\n;c)d", | |
457 | output: "for(a;b;\nc)d", | |
458 | options: ["last"], | |
459 | errors: [{ | |
460 | messageId: "expectedSemiColon", | |
461 | data: { | |
462 | pos: "the end of the previous line" | |
463 | } | |
464 | }] | |
465 | }, | |
466 | { | |
467 | code: "foo()\n;", | |
468 | output: "foo();\n", | |
469 | options: ["last"], | |
470 | errors: [{ | |
471 | messageId: "expectedSemiColon", | |
472 | data: { | |
473 | pos: "the end of the previous line" | |
474 | } | |
475 | }] | |
476 | }, | |
477 | ||
478 | { | |
479 | code: "foo;\nbar", | |
480 | output: "foo\n;bar", | |
481 | options: ["first"], | |
482 | errors: [{ | |
483 | messageId: "expectedSemiColon", | |
484 | data: { | |
485 | pos: "the beginning of the next line" | |
486 | } | |
487 | }] | |
488 | }, | |
489 | { | |
490 | code: "if(a)foo;\nbar", | |
491 | output: "if(a)foo\n;bar", | |
492 | options: ["first"], | |
493 | errors: [{ | |
494 | messageId: "expectedSemiColon", | |
495 | data: { | |
496 | pos: "the beginning of the next line" | |
497 | } | |
498 | }] | |
499 | }, | |
500 | { | |
501 | code: "var foo;\nbar", | |
502 | output: "var foo\n;bar", | |
503 | options: ["first"], | |
504 | errors: [{ | |
505 | messageId: "expectedSemiColon", | |
506 | data: { | |
507 | pos: "the beginning of the next line" | |
508 | } | |
509 | }] | |
510 | }, | |
511 | { | |
512 | code: "foo\n;\nbar", | |
513 | output: "foo\n;bar", | |
514 | options: ["first"], | |
515 | errors: [{ | |
516 | messageId: "expectedSemiColon", | |
517 | data: { | |
518 | pos: "the beginning of the next line" | |
519 | } | |
520 | }] | |
521 | }, | |
522 | { | |
523 | code: "for(a\n;b;c)d", | |
524 | output: "for(a;\nb;c)d", | |
525 | options: ["first"], | |
526 | errors: [{ | |
527 | messageId: "expectedSemiColon", | |
528 | data: { | |
529 | pos: "the end of the previous line" | |
530 | } | |
531 | }] | |
532 | }, | |
533 | { | |
534 | code: "for(a;b\n;c)d", | |
535 | output: "for(a;b;\nc)d", | |
536 | options: ["first"], | |
537 | errors: [{ | |
538 | messageId: "expectedSemiColon", | |
539 | data: { | |
540 | pos: "the end of the previous line" | |
541 | } | |
542 | }] | |
543 | }, | |
544 | ||
545 | { | |
546 | code: "foo\n;/**/bar", | |
547 | output: null, | |
548 | errors: [{ | |
549 | messageId: "expectedSemiColon", | |
550 | data: { | |
551 | pos: "the end of the previous line" | |
552 | } | |
553 | }] | |
554 | }, | |
555 | { | |
556 | code: "foo\n/**/;bar", | |
557 | output: null, | |
558 | errors: [{ | |
559 | messageId: "expectedSemiColon", | |
560 | data: { | |
561 | pos: "the end of the previous line" | |
562 | } | |
563 | }] | |
564 | }, | |
565 | ||
566 | { | |
567 | code: "foo;\n/**/bar", | |
568 | output: null, | |
569 | options: ["first"], | |
570 | errors: [{ | |
571 | messageId: "expectedSemiColon", | |
572 | data: { | |
573 | pos: "the beginning of the next line" | |
574 | } | |
575 | }] | |
576 | }, | |
577 | { | |
578 | code: "foo/**/;\nbar", | |
579 | output: null, | |
580 | options: ["first"], | |
581 | errors: [{ | |
582 | messageId: "expectedSemiColon", | |
583 | data: { | |
584 | pos: "the beginning of the next line" | |
585 | } | |
586 | }] | |
609c276f TL |
587 | }, |
588 | ||
589 | // Class fields | |
590 | { | |
591 | code: "class C { foo\n;bar }", | |
592 | output: "class C { foo;\nbar }", | |
593 | options: ["last"], | |
594 | parserOptions: { ecmaVersion: 2022 }, | |
595 | errors: [{ | |
596 | messageId: "expectedSemiColon", | |
597 | data: { | |
598 | pos: "the end of the previous line" | |
599 | } | |
600 | }] | |
601 | }, | |
602 | { | |
603 | code: "class C { foo;\nbar }", | |
604 | output: "class C { foo\n;bar }", | |
605 | options: ["first"], | |
606 | parserOptions: { ecmaVersion: 2022 }, | |
607 | errors: [{ | |
608 | messageId: "expectedSemiColon", | |
609 | data: { | |
610 | pos: "the beginning of the next line" | |
611 | } | |
612 | }] | |
613 | }, | |
614 | ||
615 | // Class static blocks | |
616 | { | |
617 | code: "class C { static { foo\n; } }", | |
618 | output: "class C { static { foo;\n} }", | |
619 | options: ["last"], | |
620 | parserOptions: { ecmaVersion: 2022 }, | |
621 | errors: [{ | |
622 | messageId: "expectedSemiColon", | |
623 | data: { | |
624 | pos: "the end of the previous line" | |
625 | } | |
626 | }] | |
627 | }, | |
628 | { | |
629 | code: "class C { static { foo\n ;bar } }", | |
630 | output: "class C { static { foo;\nbar } }", | |
631 | options: ["last"], | |
632 | parserOptions: { ecmaVersion: 2022 }, | |
633 | errors: [{ | |
634 | messageId: "expectedSemiColon", | |
635 | data: { | |
636 | pos: "the end of the previous line" | |
637 | } | |
638 | }] | |
639 | }, | |
640 | { | |
641 | code: "class C { static { foo;\nbar\n ; } }", | |
642 | output: "class C { static { foo;\nbar;\n} }", | |
643 | options: ["last"], | |
644 | parserOptions: { ecmaVersion: 2022 }, | |
645 | errors: [{ | |
646 | messageId: "expectedSemiColon", | |
647 | data: { | |
648 | pos: "the end of the previous line" | |
649 | } | |
650 | }] | |
651 | }, | |
652 | { | |
653 | code: "class C { static { foo;\nbar } }", | |
654 | output: "class C { static { foo\n;bar } }", | |
655 | options: ["first"], | |
656 | parserOptions: { ecmaVersion: 2022 }, | |
657 | errors: [{ | |
658 | messageId: "expectedSemiColon", | |
659 | data: { | |
660 | pos: "the beginning of the next line" | |
661 | } | |
662 | }] | |
eb39fafa DC |
663 | } |
664 | ] | |
665 | }); |