]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/arrow-body-style.js
import eslint 7.28.0
[pve-eslint.git] / eslint / tests / lib / rules / arrow-body-style.js
1 /**
2 * @fileoverview Tests for arrow-body-style
3 * @author Alberto Rodríguez
4 */
5 "use strict";
6
7 //------------------------------------------------------------------------------
8 // Requirements
9 //------------------------------------------------------------------------------
10
11 const rule = require("../../../lib/rules/arrow-body-style"),
12 { RuleTester } = require("../../../lib/rule-tester");
13
14 //------------------------------------------------------------------------------
15 // Tests
16 //------------------------------------------------------------------------------
17
18 const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } });
19
20 ruleTester.run("arrow-body-style", rule, {
21 valid: [
22 "var foo = () => {};",
23 "var foo = () => 0;",
24 "var addToB = (a) => { b = b + a };",
25 "var foo = () => { /* do nothing */ };",
26 "var foo = () => {\n /* do nothing */ \n};",
27 "var foo = (retv, name) => {\nretv[name] = true;\nreturn retv;\n};",
28 "var foo = () => ({});",
29 "var foo = () => bar();",
30 "var foo = () => { bar(); };",
31 "var foo = () => { b = a };",
32 "var foo = () => { bar: 1 };",
33 { code: "var foo = () => { return 0; };", options: ["always"] },
34 { code: "var foo = () => { return bar(); };", options: ["always"] },
35 { code: "var foo = () => 0;", options: ["never"] },
36 { code: "var foo = () => ({ foo: 0 });", options: ["never"] },
37 { code: "var foo = () => {};", options: ["as-needed", { requireReturnForObjectLiteral: true }] },
38 { code: "var foo = () => 0;", options: ["as-needed", { requireReturnForObjectLiteral: true }] },
39 { code: "var addToB = (a) => { b = b + a };", options: ["as-needed", { requireReturnForObjectLiteral: true }] },
40 { code: "var foo = () => { /* do nothing */ };", options: ["as-needed", { requireReturnForObjectLiteral: true }] },
41 { code: "var foo = () => {\n /* do nothing */ \n};", options: ["as-needed", { requireReturnForObjectLiteral: true }] },
42 { code: "var foo = (retv, name) => {\nretv[name] = true;\nreturn retv;\n};", options: ["as-needed", { requireReturnForObjectLiteral: true }] },
43 { code: "var foo = () => bar();", options: ["as-needed", { requireReturnForObjectLiteral: true }] },
44 { code: "var foo = () => { bar(); };", options: ["as-needed", { requireReturnForObjectLiteral: true }] },
45 { code: "var foo = () => { return { bar: 0 }; };", options: ["as-needed", { requireReturnForObjectLiteral: true }] }
46 ],
47 invalid: [
48 {
49 code: "for (var foo = () => { return a in b ? bar : () => {} } ;;);",
50 output: "for (var foo = () => (a in b ? bar : () => {}) ;;);",
51 options: ["as-needed"],
52 errors: [
53 {
54 line: 1,
55 column: 22,
56 messageId: "unexpectedSingleBlock"
57 }
58 ]
59 },
60 {
61 code: "a in b; for (var f = () => { return c };;);",
62 output: "a in b; for (var f = () => c;;);",
63 options: ["as-needed"],
64 errors: [
65 {
66 line: 1,
67 column: 28,
68 messageId: "unexpectedSingleBlock"
69 }
70 ]
71 },
72 {
73 code: "for (a = b => { return c in d ? e : f } ;;);",
74 output: "for (a = b => (c in d ? e : f) ;;);",
75 options: ["as-needed"],
76 errors: [
77 {
78 line: 1,
79 column: 15,
80 messageId: "unexpectedSingleBlock"
81 }
82 ]
83 },
84 {
85 code: "for (var f = () => { return a };;);",
86 output: "for (var f = () => a;;);",
87 options: ["as-needed"],
88 errors: [
89 {
90 line: 1,
91 column: 20,
92 messageId: "unexpectedSingleBlock"
93 }
94 ]
95 },
96 {
97 code: "for (var f;f = () => { return a };);",
98 output: "for (var f;f = () => a;);",
99 options: ["as-needed"],
100 errors: [
101 {
102 line: 1,
103 column: 22,
104 messageId: "unexpectedSingleBlock"
105 }
106 ]
107 },
108 {
109 code: "for (var f = () => { return a in c };;);",
110 output: "for (var f = () => (a in c);;);",
111 options: ["as-needed"],
112 errors: [
113 {
114 line: 1,
115 column: 20,
116 messageId: "unexpectedSingleBlock"
117 }
118 ]
119 },
120 {
121 code: "for (var f;f = () => { return a in c };);",
122 output: "for (var f;f = () => a in c;);",
123 options: ["as-needed"],
124 errors: [
125 {
126 line: 1,
127 column: 22,
128 messageId: "unexpectedSingleBlock"
129 }
130 ]
131 },
132 {
133 code: "for (;;){var f = () => { return a in c }}",
134 output: "for (;;){var f = () => a in c}",
135 options: ["as-needed"],
136 errors: [
137 {
138 line: 1,
139 column: 24,
140 messageId: "unexpectedSingleBlock"
141 }
142 ]
143 },
144 {
145 code: "for (a = b => { return c = d in e } ;;);",
146 output: "for (a = b => (c = d in e) ;;);",
147 options: ["as-needed"],
148 errors: [
149 {
150 line: 1,
151 column: 15,
152 messageId: "unexpectedSingleBlock"
153 }
154 ]
155 },
156 {
157 code: "for (var a;;a = b => { return c = d in e } );",
158 output: "for (var a;;a = b => c = d in e );",
159 options: ["as-needed"],
160 errors: [
161 {
162 line: 1,
163 column: 22,
164 messageId: "unexpectedSingleBlock"
165 }
166 ]
167 },
168 {
169 code: "for (let a = (b, c, d) => { return vb && c in d; }; ;);",
170 output: "for (let a = (b, c, d) => (vb && c in d); ;);",
171 errors: [
172 {
173 line: 1,
174 column: 27,
175 messageId: "unexpectedSingleBlock"
176 }
177 ]
178 },
179 {
180 code: "for (let a = (b, c, d) => { return v in b && c in d; }; ;);",
181 output: "for (let a = (b, c, d) => (v in b && c in d); ;);",
182 errors: [
183 {
184 line: 1,
185 column: 27,
186 messageId: "unexpectedSingleBlock"
187 }
188 ]
189 },
190 {
191 code: "function foo(){ for (let a = (b, c, d) => { return v in b && c in d; }; ;); }",
192 output: "function foo(){ for (let a = (b, c, d) => (v in b && c in d); ;); }",
193 errors: [
194 {
195 line: 1,
196 column: 43,
197 messageId: "unexpectedSingleBlock"
198 }
199 ]
200 },
201 {
202 code: "for ( a = (b, c, d) => { return v in b && c in d; }; ;);",
203 output: "for ( a = (b, c, d) => (v in b && c in d); ;);",
204 errors: [
205 {
206 line: 1,
207 column: 24,
208 messageId: "unexpectedSingleBlock"
209 }
210 ]
211 },
212 {
213 code: "for ( a = (b) => { return (c in d) }; ;);",
214 output: "for ( a = (b) => (c in d); ;);",
215 errors: [
216 {
217 line: 1,
218 column: 18,
219 messageId: "unexpectedSingleBlock"
220 }
221 ]
222 },
223 {
224 code: "for (let a = (b, c, d) => { return vb in dd ; }; ;);",
225 output: "for (let a = (b, c, d) => (vb in dd ); ;);",
226 errors: [
227 {
228 line: 1,
229 column: 27,
230 messageId: "unexpectedSingleBlock"
231 }
232 ]
233 },
234 {
235 code: "for (let a = (b, c, d) => { return vb in c in dd ; }; ;);",
236 output: "for (let a = (b, c, d) => (vb in c in dd ); ;);",
237 errors: [
238 {
239 line: 1,
240 column: 27,
241 messageId: "unexpectedSingleBlock"
242 }
243 ]
244 },
245 {
246 code: "do{let a = () => {return f in ff}}while(true){}",
247 output: "do{let a = () => f in ff}while(true){}",
248 errors: [{
249 line: 1,
250 column: 18,
251 messageId: "unexpectedSingleBlock"
252 }]
253 },
254 {
255 code: "do{for (let a = (b, c, d) => { return vb in c in dd ; }; ;);}while(true){}",
256 output: "do{for (let a = (b, c, d) => (vb in c in dd ); ;);}while(true){}",
257 errors: [{
258 line: 1,
259 column: 30,
260 messageId: "unexpectedSingleBlock"
261 }]
262 },
263 {
264 code: "scores.map(score => { return x in +(score / maxScore).toFixed(2)});",
265 output: "scores.map(score => x in +(score / maxScore).toFixed(2));",
266 errors: [{
267 line: 1,
268 column: 21,
269 messageId: "unexpectedSingleBlock"
270 }]
271 },
272 {
273 code: "const fn = (a, b) => { return a + x in Number(b) };",
274 output: "const fn = (a, b) => a + x in Number(b);",
275 errors: [{
276 line: 1,
277 column: 22,
278 messageId: "unexpectedSingleBlock"
279 }]
280 },
281 {
282 code: "var foo = () => 0",
283 output: "var foo = () => {return 0}",
284 options: ["always"],
285 errors: [
286 {
287 line: 1,
288 column: 17,
289 endLine: 1,
290 endColumn: 18,
291 type: "ArrowFunctionExpression",
292 messageId: "expectedBlock"
293 }
294 ]
295 },
296 {
297 code: "var foo = () => 0;",
298 output: "var foo = () => {return 0};",
299 options: ["always"],
300 errors: [
301 {
302 line: 1,
303 column: 17,
304 type: "ArrowFunctionExpression",
305 messageId: "expectedBlock"
306 }
307 ]
308 },
309 {
310 code: "var foo = () => ({});",
311 output: "var foo = () => {return {}};",
312 options: ["always"],
313 errors: [
314 {
315 line: 1,
316 column: 18,
317 type: "ArrowFunctionExpression",
318 messageId: "expectedBlock"
319 }
320 ]
321 },
322 {
323 code: "var foo = () => ( {});",
324 output: "var foo = () => {return {}};",
325 options: ["always"],
326 errors: [
327 {
328 line: 1,
329 column: 20,
330 type: "ArrowFunctionExpression",
331 messageId: "expectedBlock"
332 }
333 ]
334 },
335 {
336 code: "(() => ({}))",
337 output: "(() => {return {}})",
338 options: ["always"],
339 errors: [
340 {
341 line: 1,
342 column: 9,
343 type: "ArrowFunctionExpression",
344 messageId: "expectedBlock"
345 }
346 ]
347 },
348 {
349 code: "(() => ( {}))",
350 output: "(() => {return {}})",
351 options: ["always"],
352 errors: [
353 {
354 line: 1,
355 column: 10,
356 type: "ArrowFunctionExpression",
357 messageId: "expectedBlock"
358 }
359 ]
360 },
361 {
362 code: "var foo = () => { return 0; };",
363 output: "var foo = () => 0;",
364 options: ["as-needed"],
365 errors: [
366 {
367 line: 1,
368 column: 17,
369 type: "ArrowFunctionExpression",
370 messageId: "unexpectedSingleBlock"
371 }
372 ]
373 },
374 {
375 code: "var foo = () => { return 0 };",
376 output: "var foo = () => 0;",
377 options: ["as-needed"],
378 errors: [
379 {
380 line: 1,
381 column: 17,
382 type: "ArrowFunctionExpression",
383 messageId: "unexpectedSingleBlock"
384 }
385 ]
386 },
387 {
388 code: "var foo = () => { return bar(); };",
389 output: "var foo = () => bar();",
390 options: ["as-needed"],
391 errors: [
392 {
393 line: 1,
394 column: 17,
395 type: "ArrowFunctionExpression",
396 messageId: "unexpectedSingleBlock"
397 }
398 ]
399 },
400 {
401 code: "var foo = () => {};",
402 output: null,
403 options: ["never"],
404 errors: [
405 {
406 line: 1,
407 column: 17,
408 type: "ArrowFunctionExpression",
409 messageId: "unexpectedEmptyBlock"
410 }
411 ]
412 },
413 {
414 code: "var foo = () => {\nreturn 0;\n};",
415 output: "var foo = () => 0;",
416 options: ["never"],
417 errors: [
418 {
419 line: 1,
420 column: 17,
421 type: "ArrowFunctionExpression",
422 messageId: "unexpectedSingleBlock"
423 }
424 ]
425 },
426 {
427 code: "var foo = () => { return { bar: 0 }; };",
428 output: "var foo = () => ({ bar: 0 });",
429 options: ["as-needed"],
430 errors: [
431 {
432 line: 1,
433 column: 17,
434 type: "ArrowFunctionExpression",
435 messageId: "unexpectedObjectBlock"
436 }
437 ]
438 },
439 {
440 code: "var foo = () => { return ({ bar: 0 }); };",
441 output: "var foo = () => ({ bar: 0 });",
442 options: ["as-needed"],
443 errors: [
444 {
445 line: 1,
446 column: 17,
447 type: "ArrowFunctionExpression",
448 messageId: "unexpectedSingleBlock"
449 }
450 ]
451 },
452 {
453 code: "var foo = () => { return a, b }",
454 output: "var foo = () => (a, b)",
455 errors: [
456 {
457 line: 1,
458 column: 17,
459 type: "ArrowFunctionExpression",
460 messageId: "unexpectedSingleBlock"
461 }
462 ]
463 },
464 {
465 code: "var foo = () => { return };",
466 output: null, // not fixed
467 options: ["as-needed", { requireReturnForObjectLiteral: true }],
468 errors: [
469 {
470 line: 1,
471 column: 17,
472 type: "ArrowFunctionExpression",
473 messageId: "unexpectedSingleBlock"
474 }
475 ]
476 },
477 {
478 code: "var foo = () => { return; };",
479 output: null, // not fixed
480 options: ["as-needed", { requireReturnForObjectLiteral: true }],
481 errors: [
482 {
483 line: 1,
484 column: 17,
485 type: "ArrowFunctionExpression",
486 messageId: "unexpectedSingleBlock"
487 }
488 ]
489 },
490 {
491 code: "var foo = () => { return ( /* a */ {ok: true} /* b */ ) };",
492 output: "var foo = () => ( /* a */ {ok: true} /* b */ );",
493 options: ["as-needed"],
494 errors: [
495 {
496 line: 1,
497 column: 17,
498 type: "ArrowFunctionExpression",
499 messageId: "unexpectedSingleBlock"
500 }
501 ]
502 },
503 {
504 code: "var foo = () => { return '{' };",
505 output: "var foo = () => '{';",
506 options: ["as-needed"],
507 errors: [
508 {
509 line: 1,
510 column: 17,
511 type: "ArrowFunctionExpression",
512 messageId: "unexpectedSingleBlock"
513 }
514 ]
515 },
516 {
517 code: "var foo = () => { return { bar: 0 }.bar; };",
518 output: "var foo = () => ({ bar: 0 }.bar);",
519 options: ["as-needed"],
520 errors: [
521 {
522 line: 1,
523 column: 17,
524 type: "ArrowFunctionExpression",
525 messageId: "unexpectedObjectBlock"
526 }
527 ]
528 },
529 {
530 code: "var foo = (retv, name) => {\nretv[name] = true;\nreturn retv;\n};",
531 output: null, // not fixed
532 options: ["never"],
533 errors: [
534 { line: 1, column: 27, type: "ArrowFunctionExpression", messageId: "unexpectedOtherBlock" }
535 ]
536 },
537 {
538 code: "var foo = () => { return 0; };",
539 output: "var foo = () => 0;",
540 options: ["as-needed", { requireReturnForObjectLiteral: true }],
541 errors: [
542 {
543 line: 1,
544 column: 17,
545 type: "ArrowFunctionExpression",
546 messageId: "unexpectedSingleBlock"
547 }
548 ]
549 },
550 {
551 code: "var foo = () => { return bar(); };",
552 output: "var foo = () => bar();",
553 options: ["as-needed", { requireReturnForObjectLiteral: true }],
554 errors: [
555 {
556 line: 1,
557 column: 17,
558 type: "ArrowFunctionExpression",
559 messageId: "unexpectedSingleBlock"
560 }
561 ]
562 },
563 {
564 code: "var foo = () => ({});",
565 output: "var foo = () => {return {}};",
566 options: ["as-needed", { requireReturnForObjectLiteral: true }],
567 errors: [
568 {
569 line: 1,
570 column: 18,
571 type: "ArrowFunctionExpression",
572 messageId: "expectedBlock"
573 }
574 ]
575 },
576 {
577 code: "var foo = () => ({ bar: 0 });",
578 output: "var foo = () => {return { bar: 0 }};",
579 options: ["as-needed", { requireReturnForObjectLiteral: true }],
580 errors: [
581 {
582 line: 1,
583 column: 18,
584 type: "ArrowFunctionExpression",
585 messageId: "expectedBlock"
586 }
587 ]
588 },
589 {
590 code: "var foo = () => (((((((5)))))));",
591 output: "var foo = () => {return (((((((5)))))))};",
592 options: ["always"],
593 errors: [
594 {
595 line: 1,
596 column: 24,
597 type: "ArrowFunctionExpression",
598 messageId: "expectedBlock"
599 }
600 ]
601 },
602 {
603
604 // Not fixed; fixing would cause ASI issues.
605 code:
606 "var foo = () => { return bar }\n" +
607 "[1, 2, 3].map(foo)",
608 output: null,
609 options: ["never"],
610 errors: [
611 { line: 1, column: 17, type: "ArrowFunctionExpression", messageId: "unexpectedSingleBlock" }
612 ]
613 },
614 {
615
616
617 // Not fixed; fixing would cause ASI issues.
618 code:
619 "var foo = () => { return bar }\n" +
620 "(1).toString();",
621 output: null,
622 options: ["never"],
623 errors: [
624 { line: 1, column: 17, type: "ArrowFunctionExpression", messageId: "unexpectedSingleBlock" }
625 ]
626 },
627 {
628
629 // Fixing here is ok because the arrow function has a semicolon afterwards.
630 code:
631 "var foo = () => { return bar };\n" +
632 "[1, 2, 3].map(foo)",
633 output:
634 "var foo = () => bar;\n" +
635 "[1, 2, 3].map(foo)",
636 options: ["never"],
637 errors: [
638 {
639 line: 1,
640 column: 17,
641 type: "ArrowFunctionExpression",
642 messageId: "unexpectedSingleBlock"
643 }
644 ]
645 },
646 {
647 code: "var foo = /* a */ ( /* b */ ) /* c */ => /* d */ { /* e */ return /* f */ 5 /* g */ ; /* h */ } /* i */ ;",
648 output: "var foo = /* a */ ( /* b */ ) /* c */ => /* d */ /* e */ /* f */ 5 /* g */ /* h */ /* i */ ;",
649 options: ["as-needed"],
650 errors: [
651 {
652 line: 1,
653 column: 50,
654 type: "ArrowFunctionExpression",
655 messageId: "unexpectedSingleBlock"
656 }
657 ]
658 },
659 {
660 code: "var foo = /* a */ ( /* b */ ) /* c */ => /* d */ ( /* e */ 5 /* f */ ) /* g */ ;",
661 output: "var foo = /* a */ ( /* b */ ) /* c */ => /* d */ {return ( /* e */ 5 /* f */ )} /* g */ ;",
662 options: ["always"],
663 errors: [
664 {
665 line: 1,
666 column: 60,
667 type: "ArrowFunctionExpression",
668 messageId: "expectedBlock"
669 }
670 ]
671 },
672 {
673 code: "var foo = () => {\nreturn bar;\n};",
674 output: "var foo = () => bar;",
675 errors: [
676 {
677 line: 1,
678 column: 17,
679 endLine: 3,
680 endColumn: 2,
681 type: "ArrowFunctionExpression",
682 messageId: "unexpectedSingleBlock"
683 }
684 ]
685 },
686 {
687 code: "var foo = () => {\nreturn bar;};",
688 output: "var foo = () => bar;",
689 errors: [
690 {
691 line: 1,
692 column: 17,
693 endLine: 2,
694 endColumn: 13,
695 type: "ArrowFunctionExpression",
696 messageId: "unexpectedSingleBlock"
697 }
698 ]
699 },
700 {
701 code: "var foo = () => {return bar;\n};",
702 output: "var foo = () => bar;",
703 errors: [
704 {
705 line: 1,
706 column: 17,
707 endLine: 2,
708 endColumn: 2,
709 type: "ArrowFunctionExpression",
710 messageId: "unexpectedSingleBlock"
711 }
712 ]
713 },
714 {
715 code: `
716 var foo = () => {
717 return foo
718 .bar;
719 };
720 `,
721 output: `
722 var foo = () => foo
723 .bar;
724 `,
725 errors: [
726 {
727 line: 2,
728 column: 31,
729 type: "ArrowFunctionExpression",
730 messageId: "unexpectedSingleBlock"
731 }
732 ]
733 },
734 {
735 code: `
736 var foo = () => {
737 return {
738 bar: 1,
739 baz: 2
740 };
741 };
742 `,
743 output: `
744 var foo = () => ({
745 bar: 1,
746 baz: 2
747 });
748 `,
749 errors: [
750 {
751 line: 2,
752 column: 31,
753 endLine: 7,
754 endColumn: 16,
755 type: "ArrowFunctionExpression",
756 messageId: "unexpectedObjectBlock"
757 }
758 ]
759 },
760 {
761 code: "var foo = () => ({foo: 1}).foo();",
762 output: "var foo = () => {return {foo: 1}.foo()};",
763 options: ["always"],
764 errors: [{ messageId: "expectedBlock" }]
765 },
766 {
767 code: "var foo = () => ({foo: 1}.foo());",
768 output: "var foo = () => {return {foo: 1}.foo()};",
769 options: ["always"],
770 errors: [{ messageId: "expectedBlock" }]
771 },
772 {
773 code: "var foo = () => ( {foo: 1} ).foo();",
774 output: "var foo = () => {return {foo: 1} .foo()};",
775 options: ["always"],
776 errors: [{ messageId: "expectedBlock" }]
777 },
778 {
779 code: `
780 var foo = () => ({
781 bar: 1,
782 baz: 2
783 });
784 `,
785 output: `
786 var foo = () => {return {
787 bar: 1,
788 baz: 2
789 }};
790 `,
791 options: ["always"],
792 errors: [{ messageId: "expectedBlock" }]
793 },
794 {
795 code: `
796 parsedYears = _map(years, (year) => (
797 {
798 index : year,
799 title : splitYear(year)
800 }
801 ));
802 `,
803 output: `
804 parsedYears = _map(years, (year) => {
805 return {
806 index : year,
807 title : splitYear(year)
808 }
809 });
810 `,
811 options: ["always"],
812 errors: [{ messageId: "expectedBlock" }]
813 },
814
815 // https://github.com/eslint/eslint/issues/14633
816 {
817 code: "const createMarker = (color) => ({ latitude, longitude }, index) => {};",
818 output: "const createMarker = (color) => {return ({ latitude, longitude }, index) => {}};",
819 options: ["always"],
820 errors: [{ messageId: "expectedBlock" }]
821 }
822 ]
823 });