2 * @fileoverview Tests for arrow-body-style
3 * @author Alberto RodrÃguez
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
11 const rule
= require("../../../lib/rules/arrow-body-style"),
12 { RuleTester
} = require("../../../lib/rule-tester");
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
18 const ruleTester
= new RuleTester({ parserOptions
: { ecmaVersion
: 6 } });
20 ruleTester
.run("arrow-body-style", rule
, {
22 "var foo = () => {};",
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 }] }
49 code
: "for (var foo = () => { return a in b ? bar : () => {} } ;;);",
50 output
: "for (var foo = () => (a in b ? bar : () => {}) ;;);",
51 options
: ["as-needed"],
56 messageId
: "unexpectedSingleBlock"
61 code
: "a in b; for (var f = () => { return c };;);",
62 output
: "a in b; for (var f = () => c;;);",
63 options
: ["as-needed"],
68 messageId
: "unexpectedSingleBlock"
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"],
80 messageId
: "unexpectedSingleBlock"
85 code
: "for (var f = () => { return a };;);",
86 output
: "for (var f = () => a;;);",
87 options
: ["as-needed"],
92 messageId
: "unexpectedSingleBlock"
97 code
: "for (var f;f = () => { return a };);",
98 output
: "for (var f;f = () => a;);",
99 options
: ["as-needed"],
104 messageId
: "unexpectedSingleBlock"
109 code
: "for (var f = () => { return a in c };;);",
110 output
: "for (var f = () => (a in c);;);",
111 options
: ["as-needed"],
116 messageId
: "unexpectedSingleBlock"
121 code
: "for (var f;f = () => { return a in c };);",
122 output
: "for (var f;f = () => a in c;);",
123 options
: ["as-needed"],
128 messageId
: "unexpectedSingleBlock"
133 code
: "for (;;){var f = () => { return a in c }}",
134 output
: "for (;;){var f = () => a in c}",
135 options
: ["as-needed"],
140 messageId
: "unexpectedSingleBlock"
145 code
: "for (a = b => { return c = d in e } ;;);",
146 output
: "for (a = b => (c = d in e) ;;);",
147 options
: ["as-needed"],
152 messageId
: "unexpectedSingleBlock"
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"],
164 messageId
: "unexpectedSingleBlock"
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); ;);",
175 messageId
: "unexpectedSingleBlock"
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); ;);",
186 messageId
: "unexpectedSingleBlock"
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); ;); }",
197 messageId
: "unexpectedSingleBlock"
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); ;);",
208 messageId
: "unexpectedSingleBlock"
213 code
: "for ( a = (b) => { return (c in d) }; ;);",
214 output
: "for ( a = (b) => (c in d); ;);",
219 messageId
: "unexpectedSingleBlock"
224 code
: "for (let a = (b, c, d) => { return vb in dd ; }; ;);",
225 output
: "for (let a = (b, c, d) => (vb in dd ); ;);",
230 messageId
: "unexpectedSingleBlock"
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 ); ;);",
241 messageId
: "unexpectedSingleBlock"
246 code
: "do{let a = () => {return f in ff}}while(true){}",
247 output
: "do{let a = () => f in ff}while(true){}",
251 messageId
: "unexpectedSingleBlock"
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){}",
260 messageId
: "unexpectedSingleBlock"
264 code
: "scores.map(score => { return x in +(score / maxScore).toFixed(2)});",
265 output
: "scores.map(score => x in +(score / maxScore).toFixed(2));",
269 messageId
: "unexpectedSingleBlock"
273 code
: "const fn = (a, b) => { return a + x in Number(b) };",
274 output
: "const fn = (a, b) => a + x in Number(b);",
278 messageId
: "unexpectedSingleBlock"
282 code
: "var foo = () => 0",
283 output
: "var foo = () => {return 0}",
291 type
: "ArrowFunctionExpression",
292 messageId
: "expectedBlock"
297 code
: "var foo = () => 0;",
298 output
: "var foo = () => {return 0};",
304 type
: "ArrowFunctionExpression",
305 messageId
: "expectedBlock"
310 code
: "var foo = () => ({});",
311 output
: "var foo = () => {return {}};",
317 type
: "ArrowFunctionExpression",
318 messageId
: "expectedBlock"
323 code
: "var foo = () => ( {});",
324 output
: "var foo = () => {return {}};",
330 type
: "ArrowFunctionExpression",
331 messageId
: "expectedBlock"
336 code
: "(() => ({}))",
337 output
: "(() => {return {}})",
343 type
: "ArrowFunctionExpression",
344 messageId
: "expectedBlock"
349 code
: "(() => ( {}))",
350 output
: "(() => {return {}})",
356 type
: "ArrowFunctionExpression",
357 messageId
: "expectedBlock"
362 code
: "var foo = () => { return 0; };",
363 output
: "var foo = () => 0;",
364 options
: ["as-needed"],
369 type
: "ArrowFunctionExpression",
370 messageId
: "unexpectedSingleBlock"
375 code
: "var foo = () => { return 0 };",
376 output
: "var foo = () => 0;",
377 options
: ["as-needed"],
382 type
: "ArrowFunctionExpression",
383 messageId
: "unexpectedSingleBlock"
388 code
: "var foo = () => { return bar(); };",
389 output
: "var foo = () => bar();",
390 options
: ["as-needed"],
395 type
: "ArrowFunctionExpression",
396 messageId
: "unexpectedSingleBlock"
401 code
: "var foo = () => {};",
408 type
: "ArrowFunctionExpression",
409 messageId
: "unexpectedEmptyBlock"
414 code
: "var foo = () => {\nreturn 0;\n};",
415 output
: "var foo = () => 0;",
421 type
: "ArrowFunctionExpression",
422 messageId
: "unexpectedSingleBlock"
427 code
: "var foo = () => { return { bar: 0 }; };",
428 output
: "var foo = () => ({ bar: 0 });",
429 options
: ["as-needed"],
434 type
: "ArrowFunctionExpression",
435 messageId
: "unexpectedObjectBlock"
440 code
: "var foo = () => { return ({ bar: 0 }); };",
441 output
: "var foo = () => ({ bar: 0 });",
442 options
: ["as-needed"],
447 type
: "ArrowFunctionExpression",
448 messageId
: "unexpectedSingleBlock"
453 code
: "var foo = () => { return a, b }",
454 output
: "var foo = () => (a, b)",
459 type
: "ArrowFunctionExpression",
460 messageId
: "unexpectedSingleBlock"
465 code
: "var foo = () => { return };",
466 output
: null, // not fixed
467 options
: ["as-needed", { requireReturnForObjectLiteral
: true }],
472 type
: "ArrowFunctionExpression",
473 messageId
: "unexpectedSingleBlock"
478 code
: "var foo = () => { return; };",
479 output
: null, // not fixed
480 options
: ["as-needed", { requireReturnForObjectLiteral
: true }],
485 type
: "ArrowFunctionExpression",
486 messageId
: "unexpectedSingleBlock"
491 code
: "var foo = () => { return ( /* a */ {ok: true} /* b */ ) };",
492 output
: "var foo = () => ( /* a */ {ok: true} /* b */ );",
493 options
: ["as-needed"],
498 type
: "ArrowFunctionExpression",
499 messageId
: "unexpectedSingleBlock"
504 code
: "var foo = () => { return '{' };",
505 output
: "var foo = () => '{';",
506 options
: ["as-needed"],
511 type
: "ArrowFunctionExpression",
512 messageId
: "unexpectedSingleBlock"
517 code
: "var foo = () => { return { bar: 0 }.bar; };",
518 output
: "var foo = () => ({ bar: 0 }.bar);",
519 options
: ["as-needed"],
524 type
: "ArrowFunctionExpression",
525 messageId
: "unexpectedObjectBlock"
530 code
: "var foo = (retv, name) => {\nretv[name] = true;\nreturn retv;\n};",
531 output
: null, // not fixed
534 { line
: 1, column
: 27, type
: "ArrowFunctionExpression", messageId
: "unexpectedOtherBlock" }
538 code
: "var foo = () => { return 0; };",
539 output
: "var foo = () => 0;",
540 options
: ["as-needed", { requireReturnForObjectLiteral
: true }],
545 type
: "ArrowFunctionExpression",
546 messageId
: "unexpectedSingleBlock"
551 code
: "var foo = () => { return bar(); };",
552 output
: "var foo = () => bar();",
553 options
: ["as-needed", { requireReturnForObjectLiteral
: true }],
558 type
: "ArrowFunctionExpression",
559 messageId
: "unexpectedSingleBlock"
564 code
: "var foo = () => ({});",
565 output
: "var foo = () => {return {}};",
566 options
: ["as-needed", { requireReturnForObjectLiteral
: true }],
571 type
: "ArrowFunctionExpression",
572 messageId
: "expectedBlock"
577 code
: "var foo = () => ({ bar: 0 });",
578 output
: "var foo = () => {return { bar: 0 }};",
579 options
: ["as-needed", { requireReturnForObjectLiteral
: true }],
584 type
: "ArrowFunctionExpression",
585 messageId
: "expectedBlock"
590 code
: "var foo = () => (((((((5)))))));",
591 output
: "var foo = () => {return (((((((5)))))))};",
597 type
: "ArrowFunctionExpression",
598 messageId
: "expectedBlock"
604 // Not fixed; fixing would cause ASI issues.
606 "var foo = () => { return bar }\n" +
607 "[1, 2, 3].map(foo)",
611 { line
: 1, column
: 17, type
: "ArrowFunctionExpression", messageId
: "unexpectedSingleBlock" }
617 // Not fixed; fixing would cause ASI issues.
619 "var foo = () => { return bar }\n" +
624 { line
: 1, column
: 17, type
: "ArrowFunctionExpression", messageId
: "unexpectedSingleBlock" }
629 // Fixing here is ok because the arrow function has a semicolon afterwards.
631 "var foo = () => { return bar };\n" +
632 "[1, 2, 3].map(foo)",
634 "var foo = () => bar;\n" +
635 "[1, 2, 3].map(foo)",
641 type
: "ArrowFunctionExpression",
642 messageId
: "unexpectedSingleBlock"
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"],
654 type
: "ArrowFunctionExpression",
655 messageId
: "unexpectedSingleBlock"
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 */ ;",
667 type
: "ArrowFunctionExpression",
668 messageId
: "expectedBlock"
673 code
: "var foo = () => {\nreturn bar;\n};",
674 output
: "var foo = () => bar;",
681 type
: "ArrowFunctionExpression",
682 messageId
: "unexpectedSingleBlock"
687 code
: "var foo = () => {\nreturn bar;};",
688 output
: "var foo = () => bar;",
695 type
: "ArrowFunctionExpression",
696 messageId
: "unexpectedSingleBlock"
701 code
: "var foo = () => {return bar;\n};",
702 output
: "var foo = () => bar;",
709 type
: "ArrowFunctionExpression",
710 messageId
: "unexpectedSingleBlock"
729 type
: "ArrowFunctionExpression",
730 messageId
: "unexpectedSingleBlock"
755 type
: "ArrowFunctionExpression",
756 messageId
: "unexpectedObjectBlock"
761 code
: "var foo = () => ({foo: 1}).foo();",
762 output
: "var foo = () => {return {foo: 1}.foo()};",
764 errors
: [{ messageId
: "expectedBlock" }]
767 code
: "var foo = () => ({foo: 1}.foo());",
768 output
: "var foo = () => {return {foo: 1}.foo()};",
770 errors
: [{ messageId
: "expectedBlock" }]
773 code
: "var foo = () => ( {foo: 1} ).foo();",
774 output
: "var foo = () => {return {foo: 1} .foo()};",
776 errors
: [{ messageId
: "expectedBlock" }]
786 var foo = () => {return {
792 errors
: [{ messageId
: "expectedBlock" }]
796 parsedYears = _map(years, (year) => (
799 title : splitYear(year)
804 parsedYears = _map(years, (year) => {
807 title : splitYear(year)
812 errors
: [{ messageId
: "expectedBlock" }]
815 // https://github.com/eslint/eslint/issues/14633
817 code
: "const createMarker = (color) => ({ latitude, longitude }, index) => {};",
818 output
: "const createMarker = (color) => {return ({ latitude, longitude }, index) => {}};",
820 errors
: [{ messageId
: "expectedBlock" }]