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
: "var foo = () => 0",
50 output
: "var foo = () => {return 0}",
56 type
: "ArrowFunctionExpression",
57 messageId
: "expectedBlock"
62 code
: "var foo = () => 0;",
63 output
: "var foo = () => {return 0};",
69 type
: "ArrowFunctionExpression",
70 messageId
: "expectedBlock"
75 code
: "var foo = () => ({});",
76 output
: "var foo = () => {return {}};",
82 type
: "ArrowFunctionExpression",
83 messageId
: "expectedBlock"
88 code
: "var foo = () => ( {});",
89 output
: "var foo = () => {return {}};",
95 type
: "ArrowFunctionExpression",
96 messageId
: "expectedBlock"
101 code
: "(() => ({}))",
102 output
: "(() => {return {}})",
108 type
: "ArrowFunctionExpression",
109 messageId
: "expectedBlock"
114 code
: "(() => ( {}))",
115 output
: "(() => {return {}})",
121 type
: "ArrowFunctionExpression",
122 messageId
: "expectedBlock"
127 code
: "var foo = () => { return 0; };",
128 output
: "var foo = () => 0;",
129 options
: ["as-needed"],
134 type
: "ArrowFunctionExpression",
135 messageId
: "unexpectedSingleBlock"
140 code
: "var foo = () => { return 0 };",
141 output
: "var foo = () => 0;",
142 options
: ["as-needed"],
147 type
: "ArrowFunctionExpression",
148 messageId
: "unexpectedSingleBlock"
153 code
: "var foo = () => { return bar(); };",
154 output
: "var foo = () => bar();",
155 options
: ["as-needed"],
160 type
: "ArrowFunctionExpression",
161 messageId
: "unexpectedSingleBlock"
166 code
: "var foo = () => {};",
173 type
: "ArrowFunctionExpression",
174 messageId
: "unexpectedEmptyBlock"
179 code
: "var foo = () => {\nreturn 0;\n};",
180 output
: "var foo = () => 0;",
186 type
: "ArrowFunctionExpression",
187 messageId
: "unexpectedSingleBlock"
192 code
: "var foo = () => { return { bar: 0 }; };",
193 output
: "var foo = () => ({ bar: 0 });",
194 options
: ["as-needed"],
199 type
: "ArrowFunctionExpression",
200 messageId
: "unexpectedObjectBlock"
205 code
: "var foo = () => { return ({ bar: 0 }); };",
206 output
: "var foo = () => ({ bar: 0 });",
207 options
: ["as-needed"],
212 type
: "ArrowFunctionExpression",
213 messageId
: "unexpectedSingleBlock"
218 code
: "var foo = () => { return a, b }",
219 output
: "var foo = () => (a, b)",
224 type
: "ArrowFunctionExpression",
225 messageId
: "unexpectedSingleBlock"
230 code
: "var foo = () => { return };",
231 output
: null, // not fixed
232 options
: ["as-needed", { requireReturnForObjectLiteral
: true }],
237 type
: "ArrowFunctionExpression",
238 messageId
: "unexpectedSingleBlock"
243 code
: "var foo = () => { return; };",
244 output
: null, // not fixed
245 options
: ["as-needed", { requireReturnForObjectLiteral
: true }],
250 type
: "ArrowFunctionExpression",
251 messageId
: "unexpectedSingleBlock"
256 code
: "var foo = () => { return ( /* a */ {ok: true} /* b */ ) };",
257 output
: "var foo = () => ( /* a */ {ok: true} /* b */ );",
258 options
: ["as-needed"],
263 type
: "ArrowFunctionExpression",
264 messageId
: "unexpectedSingleBlock"
269 code
: "var foo = () => { return '{' };",
270 output
: "var foo = () => '{';",
271 options
: ["as-needed"],
276 type
: "ArrowFunctionExpression",
277 messageId
: "unexpectedSingleBlock"
282 code
: "var foo = () => { return { bar: 0 }.bar; };",
283 output
: "var foo = () => ({ bar: 0 }.bar);",
284 options
: ["as-needed"],
289 type
: "ArrowFunctionExpression",
290 messageId
: "unexpectedObjectBlock"
295 code
: "var foo = (retv, name) => {\nretv[name] = true;\nreturn retv;\n};",
296 output
: null, // not fixed
299 { line
: 1, column
: 27, type
: "ArrowFunctionExpression", messageId
: "unexpectedOtherBlock" }
303 code
: "var foo = () => { return 0; };",
304 output
: "var foo = () => 0;",
305 options
: ["as-needed", { requireReturnForObjectLiteral
: true }],
310 type
: "ArrowFunctionExpression",
311 messageId
: "unexpectedSingleBlock"
316 code
: "var foo = () => { return bar(); };",
317 output
: "var foo = () => bar();",
318 options
: ["as-needed", { requireReturnForObjectLiteral
: true }],
323 type
: "ArrowFunctionExpression",
324 messageId
: "unexpectedSingleBlock"
329 code
: "var foo = () => ({});",
330 output
: "var foo = () => {return {}};",
331 options
: ["as-needed", { requireReturnForObjectLiteral
: true }],
336 type
: "ArrowFunctionExpression",
337 messageId
: "expectedBlock"
342 code
: "var foo = () => ({ bar: 0 });",
343 output
: "var foo = () => {return { bar: 0 }};",
344 options
: ["as-needed", { requireReturnForObjectLiteral
: true }],
349 type
: "ArrowFunctionExpression",
350 messageId
: "expectedBlock"
355 code
: "var foo = () => (((((((5)))))));",
356 output
: "var foo = () => {return (((((((5)))))))};",
362 type
: "ArrowFunctionExpression",
363 messageId
: "expectedBlock"
369 // Not fixed; fixing would cause ASI issues.
371 "var foo = () => { return bar }\n" +
372 "[1, 2, 3].map(foo)",
376 { line
: 1, column
: 17, type
: "ArrowFunctionExpression", messageId
: "unexpectedSingleBlock" }
381 // Not fixed; fixing would cause ASI issues.
383 "var foo = () => { return bar }\n" +
388 { line
: 1, column
: 17, type
: "ArrowFunctionExpression", messageId
: "unexpectedSingleBlock" }
393 // Fixing here is ok because the arrow function has a semicolon afterwards.
395 "var foo = () => { return bar };\n" +
396 "[1, 2, 3].map(foo)",
398 "var foo = () => bar;\n" +
399 "[1, 2, 3].map(foo)",
405 type
: "ArrowFunctionExpression",
406 messageId
: "unexpectedSingleBlock"
411 code
: "var foo = /* a */ ( /* b */ ) /* c */ => /* d */ { /* e */ return /* f */ 5 /* g */ ; /* h */ } /* i */ ;",
412 output
: "var foo = /* a */ ( /* b */ ) /* c */ => /* d */ /* e */ /* f */ 5 /* g */ /* h */ /* i */ ;",
413 options
: ["as-needed"],
418 type
: "ArrowFunctionExpression",
419 messageId
: "unexpectedSingleBlock"
424 code
: "var foo = /* a */ ( /* b */ ) /* c */ => /* d */ ( /* e */ 5 /* f */ ) /* g */ ;",
425 output
: "var foo = /* a */ ( /* b */ ) /* c */ => /* d */ {return ( /* e */ 5 /* f */ )} /* g */ ;",
431 type
: "ArrowFunctionExpression",
432 messageId
: "expectedBlock"
437 code
: "var foo = () => {\nreturn bar;\n};",
438 output
: "var foo = () => bar;",
443 type
: "ArrowFunctionExpression",
444 messageId
: "unexpectedSingleBlock"
449 code
: "var foo = () => {\nreturn bar;};",
450 output
: "var foo = () => bar;",
455 type
: "ArrowFunctionExpression",
456 messageId
: "unexpectedSingleBlock"
461 code
: "var foo = () => {return bar;\n};",
462 output
: "var foo = () => bar;",
467 type
: "ArrowFunctionExpression",
468 messageId
: "unexpectedSingleBlock"
487 type
: "ArrowFunctionExpression",
488 messageId
: "unexpectedSingleBlock"
511 type
: "ArrowFunctionExpression",
512 messageId
: "unexpectedObjectBlock"
517 code
: "var foo = () => ({foo: 1}).foo();",
518 output
: "var foo = () => {return {foo: 1}.foo()};",
520 errors
: [{ messageId
: "expectedBlock" }]
523 code
: "var foo = () => ({foo: 1}.foo());",
524 output
: "var foo = () => {return {foo: 1}.foo()};",
526 errors
: [{ messageId
: "expectedBlock" }]
529 code
: "var foo = () => ( {foo: 1} ).foo();",
530 output
: "var foo = () => {return {foo: 1} .foo()};",
532 errors
: [{ messageId
: "expectedBlock" }]
542 var foo = () => {return {
548 errors
: [{ messageId
: "expectedBlock" }]
552 parsedYears = _map(years, (year) => (
555 title : splitYear(year)
560 parsedYears = _map(years, (year) => {
563 title : splitYear(year)
568 errors
: [{ messageId
: "expectedBlock" }]