]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/arrow-body-style.js
7c1bea64ef8c942fd5ac44f7a123bd911aea9a03
[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: "var foo = () => 0",
50 output: "var foo = () => {return 0}",
51 options: ["always"],
52 errors: [
53 {
54 line: 1,
55 column: 17,
56 type: "ArrowFunctionExpression",
57 messageId: "expectedBlock"
58 }
59 ]
60 },
61 {
62 code: "var foo = () => 0;",
63 output: "var foo = () => {return 0};",
64 options: ["always"],
65 errors: [
66 {
67 line: 1,
68 column: 17,
69 type: "ArrowFunctionExpression",
70 messageId: "expectedBlock"
71 }
72 ]
73 },
74 {
75 code: "var foo = () => ({});",
76 output: "var foo = () => {return {}};",
77 options: ["always"],
78 errors: [
79 {
80 line: 1,
81 column: 18,
82 type: "ArrowFunctionExpression",
83 messageId: "expectedBlock"
84 }
85 ]
86 },
87 {
88 code: "var foo = () => ( {});",
89 output: "var foo = () => {return {}};",
90 options: ["always"],
91 errors: [
92 {
93 line: 1,
94 column: 20,
95 type: "ArrowFunctionExpression",
96 messageId: "expectedBlock"
97 }
98 ]
99 },
100 {
101 code: "(() => ({}))",
102 output: "(() => {return {}})",
103 options: ["always"],
104 errors: [
105 {
106 line: 1,
107 column: 9,
108 type: "ArrowFunctionExpression",
109 messageId: "expectedBlock"
110 }
111 ]
112 },
113 {
114 code: "(() => ( {}))",
115 output: "(() => {return {}})",
116 options: ["always"],
117 errors: [
118 {
119 line: 1,
120 column: 10,
121 type: "ArrowFunctionExpression",
122 messageId: "expectedBlock"
123 }
124 ]
125 },
126 {
127 code: "var foo = () => { return 0; };",
128 output: "var foo = () => 0;",
129 options: ["as-needed"],
130 errors: [
131 {
132 line: 1,
133 column: 17,
134 type: "ArrowFunctionExpression",
135 messageId: "unexpectedSingleBlock"
136 }
137 ]
138 },
139 {
140 code: "var foo = () => { return 0 };",
141 output: "var foo = () => 0;",
142 options: ["as-needed"],
143 errors: [
144 {
145 line: 1,
146 column: 17,
147 type: "ArrowFunctionExpression",
148 messageId: "unexpectedSingleBlock"
149 }
150 ]
151 },
152 {
153 code: "var foo = () => { return bar(); };",
154 output: "var foo = () => bar();",
155 options: ["as-needed"],
156 errors: [
157 {
158 line: 1,
159 column: 17,
160 type: "ArrowFunctionExpression",
161 messageId: "unexpectedSingleBlock"
162 }
163 ]
164 },
165 {
166 code: "var foo = () => {};",
167 output: null,
168 options: ["never"],
169 errors: [
170 {
171 line: 1,
172 column: 17,
173 type: "ArrowFunctionExpression",
174 messageId: "unexpectedEmptyBlock"
175 }
176 ]
177 },
178 {
179 code: "var foo = () => {\nreturn 0;\n};",
180 output: "var foo = () => 0;",
181 options: ["never"],
182 errors: [
183 {
184 line: 1,
185 column: 17,
186 type: "ArrowFunctionExpression",
187 messageId: "unexpectedSingleBlock"
188 }
189 ]
190 },
191 {
192 code: "var foo = () => { return { bar: 0 }; };",
193 output: "var foo = () => ({ bar: 0 });",
194 options: ["as-needed"],
195 errors: [
196 {
197 line: 1,
198 column: 17,
199 type: "ArrowFunctionExpression",
200 messageId: "unexpectedObjectBlock"
201 }
202 ]
203 },
204 {
205 code: "var foo = () => { return ({ bar: 0 }); };",
206 output: "var foo = () => ({ bar: 0 });",
207 options: ["as-needed"],
208 errors: [
209 {
210 line: 1,
211 column: 17,
212 type: "ArrowFunctionExpression",
213 messageId: "unexpectedSingleBlock"
214 }
215 ]
216 },
217 {
218 code: "var foo = () => { return a, b }",
219 output: "var foo = () => (a, b)",
220 errors: [
221 {
222 line: 1,
223 column: 17,
224 type: "ArrowFunctionExpression",
225 messageId: "unexpectedSingleBlock"
226 }
227 ]
228 },
229 {
230 code: "var foo = () => { return };",
231 output: null, // not fixed
232 options: ["as-needed", { requireReturnForObjectLiteral: true }],
233 errors: [
234 {
235 line: 1,
236 column: 17,
237 type: "ArrowFunctionExpression",
238 messageId: "unexpectedSingleBlock"
239 }
240 ]
241 },
242 {
243 code: "var foo = () => { return; };",
244 output: null, // not fixed
245 options: ["as-needed", { requireReturnForObjectLiteral: true }],
246 errors: [
247 {
248 line: 1,
249 column: 17,
250 type: "ArrowFunctionExpression",
251 messageId: "unexpectedSingleBlock"
252 }
253 ]
254 },
255 {
256 code: "var foo = () => { return ( /* a */ {ok: true} /* b */ ) };",
257 output: "var foo = () => ( /* a */ {ok: true} /* b */ );",
258 options: ["as-needed"],
259 errors: [
260 {
261 line: 1,
262 column: 17,
263 type: "ArrowFunctionExpression",
264 messageId: "unexpectedSingleBlock"
265 }
266 ]
267 },
268 {
269 code: "var foo = () => { return '{' };",
270 output: "var foo = () => '{';",
271 options: ["as-needed"],
272 errors: [
273 {
274 line: 1,
275 column: 17,
276 type: "ArrowFunctionExpression",
277 messageId: "unexpectedSingleBlock"
278 }
279 ]
280 },
281 {
282 code: "var foo = () => { return { bar: 0 }.bar; };",
283 output: "var foo = () => ({ bar: 0 }.bar);",
284 options: ["as-needed"],
285 errors: [
286 {
287 line: 1,
288 column: 17,
289 type: "ArrowFunctionExpression",
290 messageId: "unexpectedObjectBlock"
291 }
292 ]
293 },
294 {
295 code: "var foo = (retv, name) => {\nretv[name] = true;\nreturn retv;\n};",
296 output: null, // not fixed
297 options: ["never"],
298 errors: [
299 { line: 1, column: 27, type: "ArrowFunctionExpression", messageId: "unexpectedOtherBlock" }
300 ]
301 },
302 {
303 code: "var foo = () => { return 0; };",
304 output: "var foo = () => 0;",
305 options: ["as-needed", { requireReturnForObjectLiteral: true }],
306 errors: [
307 {
308 line: 1,
309 column: 17,
310 type: "ArrowFunctionExpression",
311 messageId: "unexpectedSingleBlock"
312 }
313 ]
314 },
315 {
316 code: "var foo = () => { return bar(); };",
317 output: "var foo = () => bar();",
318 options: ["as-needed", { requireReturnForObjectLiteral: true }],
319 errors: [
320 {
321 line: 1,
322 column: 17,
323 type: "ArrowFunctionExpression",
324 messageId: "unexpectedSingleBlock"
325 }
326 ]
327 },
328 {
329 code: "var foo = () => ({});",
330 output: "var foo = () => {return {}};",
331 options: ["as-needed", { requireReturnForObjectLiteral: true }],
332 errors: [
333 {
334 line: 1,
335 column: 18,
336 type: "ArrowFunctionExpression",
337 messageId: "expectedBlock"
338 }
339 ]
340 },
341 {
342 code: "var foo = () => ({ bar: 0 });",
343 output: "var foo = () => {return { bar: 0 }};",
344 options: ["as-needed", { requireReturnForObjectLiteral: true }],
345 errors: [
346 {
347 line: 1,
348 column: 18,
349 type: "ArrowFunctionExpression",
350 messageId: "expectedBlock"
351 }
352 ]
353 },
354 {
355 code: "var foo = () => (((((((5)))))));",
356 output: "var foo = () => {return (((((((5)))))))};",
357 options: ["always"],
358 errors: [
359 {
360 line: 1,
361 column: 24,
362 type: "ArrowFunctionExpression",
363 messageId: "expectedBlock"
364 }
365 ]
366 },
367 {
368
369 // Not fixed; fixing would cause ASI issues.
370 code:
371 "var foo = () => { return bar }\n" +
372 "[1, 2, 3].map(foo)",
373 output: null,
374 options: ["never"],
375 errors: [
376 { line: 1, column: 17, type: "ArrowFunctionExpression", messageId: "unexpectedSingleBlock" }
377 ]
378 },
379 {
380
381 // Not fixed; fixing would cause ASI issues.
382 code:
383 "var foo = () => { return bar }\n" +
384 "(1).toString();",
385 output: null,
386 options: ["never"],
387 errors: [
388 { line: 1, column: 17, type: "ArrowFunctionExpression", messageId: "unexpectedSingleBlock" }
389 ]
390 },
391 {
392
393 // Fixing here is ok because the arrow function has a semicolon afterwards.
394 code:
395 "var foo = () => { return bar };\n" +
396 "[1, 2, 3].map(foo)",
397 output:
398 "var foo = () => bar;\n" +
399 "[1, 2, 3].map(foo)",
400 options: ["never"],
401 errors: [
402 {
403 line: 1,
404 column: 17,
405 type: "ArrowFunctionExpression",
406 messageId: "unexpectedSingleBlock"
407 }
408 ]
409 },
410 {
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"],
414 errors: [
415 {
416 line: 1,
417 column: 50,
418 type: "ArrowFunctionExpression",
419 messageId: "unexpectedSingleBlock"
420 }
421 ]
422 },
423 {
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 */ ;",
426 options: ["always"],
427 errors: [
428 {
429 line: 1,
430 column: 60,
431 type: "ArrowFunctionExpression",
432 messageId: "expectedBlock"
433 }
434 ]
435 },
436 {
437 code: "var foo = () => {\nreturn bar;\n};",
438 output: "var foo = () => bar;",
439 errors: [
440 {
441 line: 1,
442 column: 17,
443 type: "ArrowFunctionExpression",
444 messageId: "unexpectedSingleBlock"
445 }
446 ]
447 },
448 {
449 code: "var foo = () => {\nreturn bar;};",
450 output: "var foo = () => bar;",
451 errors: [
452 {
453 line: 1,
454 column: 17,
455 type: "ArrowFunctionExpression",
456 messageId: "unexpectedSingleBlock"
457 }
458 ]
459 },
460 {
461 code: "var foo = () => {return bar;\n};",
462 output: "var foo = () => bar;",
463 errors: [
464 {
465 line: 1,
466 column: 17,
467 type: "ArrowFunctionExpression",
468 messageId: "unexpectedSingleBlock"
469 }
470 ]
471 },
472 {
473 code: `
474 var foo = () => {
475 return foo
476 .bar;
477 };
478 `,
479 output: `
480 var foo = () => foo
481 .bar;
482 `,
483 errors: [
484 {
485 line: 2,
486 column: 31,
487 type: "ArrowFunctionExpression",
488 messageId: "unexpectedSingleBlock"
489 }
490 ]
491 },
492 {
493 code: `
494 var foo = () => {
495 return {
496 bar: 1,
497 baz: 2
498 };
499 };
500 `,
501 output: `
502 var foo = () => ({
503 bar: 1,
504 baz: 2
505 });
506 `,
507 errors: [
508 {
509 line: 2,
510 column: 31,
511 type: "ArrowFunctionExpression",
512 messageId: "unexpectedObjectBlock"
513 }
514 ]
515 },
516 {
517 code: "var foo = () => ({foo: 1}).foo();",
518 output: "var foo = () => {return {foo: 1}.foo()};",
519 options: ["always"],
520 errors: [{ messageId: "expectedBlock" }]
521 },
522 {
523 code: "var foo = () => ({foo: 1}.foo());",
524 output: "var foo = () => {return {foo: 1}.foo()};",
525 options: ["always"],
526 errors: [{ messageId: "expectedBlock" }]
527 },
528 {
529 code: "var foo = () => ( {foo: 1} ).foo();",
530 output: "var foo = () => {return {foo: 1} .foo()};",
531 options: ["always"],
532 errors: [{ messageId: "expectedBlock" }]
533 },
534 {
535 code: `
536 var foo = () => ({
537 bar: 1,
538 baz: 2
539 });
540 `,
541 output: `
542 var foo = () => {return {
543 bar: 1,
544 baz: 2
545 }};
546 `,
547 options: ["always"],
548 errors: [{ messageId: "expectedBlock" }]
549 },
550 {
551 code: `
552 parsedYears = _map(years, (year) => (
553 {
554 index : year,
555 title : splitYear(year)
556 }
557 ));
558 `,
559 output: `
560 parsedYears = _map(years, (year) => {
561 return {
562 index : year,
563 title : splitYear(year)
564 }
565 });
566 `,
567 options: ["always"],
568 errors: [{ messageId: "expectedBlock" }]
569 }
570 ]
571 });