]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/arrow-parens.js
d2f32a784a3a7a9b4a7aa722dd9b62bfd1892879
[pve-eslint.git] / eslint / tests / lib / rules / arrow-parens.js
1 /**
2 * @fileoverview Tests for arrow-parens
3 * @author Jxck
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const baseParser = require("../../fixtures/fixture-parser"),
13 rule = require("../../../lib/rules/arrow-parens"),
14 { RuleTester } = require("../../../lib/rule-tester");
15
16 const parser = baseParser.bind(null, "arrow-parens");
17
18 //------------------------------------------------------------------------------
19 // Tests
20 //------------------------------------------------------------------------------
21
22 const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } });
23
24 const valid = [
25
26 // "always" (by default)
27 "() => {}",
28 "(a) => {}",
29 "(a) => a",
30 "(a) => {\n}",
31 "a.then((foo) => {});",
32 "a.then((foo) => { if (true) {}; });",
33 "const f = (/* */a) => a + a;",
34 "const f = (a/** */) => a + a;",
35 "const f = (a//\n) => a + a;",
36 "const f = (//\na) => a + a;",
37 "const f = (/*\n */a//\n) => a + a;",
38 "const f = (/** @type {number} */a/**hello*/) => a + a;",
39 { code: "a.then(async (foo) => { if (true) {}; });", parserOptions: { ecmaVersion: 8 } },
40
41 // "always" (explicit)
42 { code: "() => {}", options: ["always"] },
43 { code: "(a) => {}", options: ["always"] },
44 { code: "(a) => a", options: ["always"] },
45 { code: "(a) => {\n}", options: ["always"] },
46 { code: "a.then((foo) => {});", options: ["always"] },
47 { code: "a.then((foo) => { if (true) {}; });", options: ["always"] },
48 { code: "a.then(async (foo) => { if (true) {}; });", options: ["always"], parserOptions: { ecmaVersion: 8 } },
49 { code: "(a: T) => a", options: ["always"], parser: parser("identifer-type") },
50 { code: "(a): T => a", options: ["always"], parser: parser("return-type") },
51
52 // "as-needed"
53 { code: "() => {}", options: ["as-needed"] },
54 { code: "a => {}", options: ["as-needed"] },
55 { code: "a => a", options: ["as-needed"] },
56 { code: "a => (a)", options: ["as-needed"] },
57 { code: "(a => a)", options: ["as-needed"] },
58 { code: "((a => a))", options: ["as-needed"] },
59 { code: "([a, b]) => {}", options: ["as-needed"] },
60 { code: "({ a, b }) => {}", options: ["as-needed"] },
61 { code: "(a = 10) => {}", options: ["as-needed"] },
62 { code: "(...a) => a[0]", options: ["as-needed"] },
63 { code: "(a, b) => {}", options: ["as-needed"] },
64 { code: "async a => a", options: ["as-needed"], parserOptions: { ecmaVersion: 8 } },
65 { code: "async ([a, b]) => {}", options: ["as-needed"], parserOptions: { ecmaVersion: 8 } },
66 { code: "async (a, b) => {}", options: ["as-needed"], parserOptions: { ecmaVersion: 8 } },
67 { code: "(a: T) => a", options: ["as-needed"], parser: parser("identifer-type") },
68 { code: "(a): T => a", options: ["as-needed"], parser: parser("return-type") },
69
70 // "as-needed", { "requireForBlockBody": true }
71 { code: "() => {}", options: ["as-needed", { requireForBlockBody: true }] },
72 { code: "a => a", options: ["as-needed", { requireForBlockBody: true }] },
73 { code: "a => (a)", options: ["as-needed", { requireForBlockBody: true }] },
74 { code: "(a => a)", options: ["as-needed", { requireForBlockBody: true }] },
75 { code: "((a => a))", options: ["as-needed", { requireForBlockBody: true }] },
76 { code: "([a, b]) => {}", options: ["as-needed", { requireForBlockBody: true }] },
77 { code: "([a, b]) => a", options: ["as-needed", { requireForBlockBody: true }] },
78 { code: "({ a, b }) => {}", options: ["as-needed", { requireForBlockBody: true }] },
79 { code: "({ a, b }) => a + b", options: ["as-needed", { requireForBlockBody: true }] },
80 { code: "(a = 10) => {}", options: ["as-needed", { requireForBlockBody: true }] },
81 { code: "(...a) => a[0]", options: ["as-needed", { requireForBlockBody: true }] },
82 { code: "(a, b) => {}", options: ["as-needed", { requireForBlockBody: true }] },
83 { code: "a => ({})", options: ["as-needed", { requireForBlockBody: true }] },
84 { code: "async a => ({})", options: ["as-needed", { requireForBlockBody: true }], parserOptions: { ecmaVersion: 8 } },
85 { code: "async a => a", options: ["as-needed", { requireForBlockBody: true }], parserOptions: { ecmaVersion: 8 } },
86 { code: "(a: T) => a", options: ["as-needed", { requireForBlockBody: true }], parser: parser("identifer-type") },
87 { code: "(a): T => a", options: ["as-needed", { requireForBlockBody: true }], parser: parser("return-type") },
88 {
89 code: "const f = (/** @type {number} */a/**hello*/) => a + a;",
90 options: ["as-needed"]
91 },
92 {
93 code: "const f = (/* */a) => a + a;",
94 options: ["as-needed"]
95 },
96 {
97 code: "const f = (a/** */) => a + a;",
98 options: ["as-needed"]
99 },
100 {
101 code: "const f = (a//\n) => a + a;",
102 options: ["as-needed"]
103 },
104 {
105 code: "const f = (//\na) => a + a;",
106 options: ["as-needed"]
107 },
108 {
109 code: "const f = (/*\n */a//\n) => a + a;",
110 options: ["as-needed"]
111 },
112 {
113 code: "var foo = (a,/**/) => b;",
114 parserOptions: { ecmaVersion: 2017 },
115 options: ["as-needed"]
116 },
117 {
118 code: "var foo = (a , /**/) => b;",
119 parserOptions: { ecmaVersion: 2017 },
120 options: ["as-needed"]
121 },
122 {
123 code: "var foo = (a\n,\n/**/) => b;",
124 parserOptions: { ecmaVersion: 2017 },
125 options: ["as-needed"]
126 },
127 {
128 code: "var foo = (a,//\n) => b;",
129 parserOptions: { ecmaVersion: 2017 },
130 options: ["as-needed"]
131 },
132 {
133 code: "const i = (a/**/,) => a + a;",
134 parserOptions: { ecmaVersion: 2017 },
135 options: ["as-needed"]
136 },
137 {
138 code: "const i = (a \n /**/,) => a + a;",
139 parserOptions: { ecmaVersion: 2017 },
140 options: ["as-needed"]
141 },
142 {
143 code: "var bar = ({/*comment here*/a}) => a",
144 options: ["as-needed"]
145 },
146 {
147 code: "var bar = (/*comment here*/{a}) => a",
148 options: ["as-needed"]
149 },
150
151 // generics
152 {
153 code: "<T>(a) => b",
154 options: ["always"],
155 parser: parser("generics-simple")
156 },
157 {
158 code: "<T>(a) => b",
159 options: ["as-needed"],
160 parser: parser("generics-simple")
161 },
162 {
163 code: "<T>(a) => b",
164 options: ["as-needed", { requireForBlockBody: true }],
165 parser: parser("generics-simple")
166 },
167 {
168 code: "async <T>(a) => b",
169 options: ["always"],
170 parser: parser("generics-simple-async")
171 },
172 {
173 code: "async <T>(a) => b",
174 options: ["as-needed"],
175 parser: parser("generics-simple-async")
176 },
177 {
178 code: "async <T>(a) => b",
179 options: ["as-needed", { requireForBlockBody: true }],
180 parser: parser("generics-simple-async")
181 },
182 {
183 code: "<T>() => b",
184 options: ["always"],
185 parser: parser("generics-simple-no-params")
186 },
187 {
188 code: "<T>() => b",
189 options: ["as-needed"],
190 parser: parser("generics-simple-no-params")
191 },
192 {
193 code: "<T>() => b",
194 options: ["as-needed", { requireForBlockBody: true }],
195 parser: parser("generics-simple-no-params")
196 },
197 {
198 code: "<T extends A>(a) => b",
199 options: ["always"],
200 parser: parser("generics-extends")
201 },
202 {
203 code: "<T extends A>(a) => b",
204 options: ["as-needed"],
205 parser: parser("generics-extends")
206 },
207 {
208 code: "<T extends A>(a) => b",
209 options: ["as-needed", { requireForBlockBody: true }],
210 parser: parser("generics-extends")
211 },
212 {
213 code: "<T extends (A | B) & C>(a) => b",
214 options: ["always"],
215 parser: parser("generics-extends-complex")
216 },
217 {
218 code: "<T extends (A | B) & C>(a) => b",
219 options: ["as-needed"],
220 parser: parser("generics-extends-complex")
221 },
222 {
223 code: "<T extends (A | B) & C>(a) => b",
224 options: ["as-needed", { requireForBlockBody: true }],
225 parser: parser("generics-extends-complex")
226 }
227 ];
228
229 const type = "ArrowFunctionExpression";
230
231 const invalid = [
232
233 // "always" (by default)
234 {
235 code: "a => {}",
236 output: "(a) => {}",
237 errors: [{
238 line: 1,
239 column: 1,
240 endColumn: 2,
241 messageId: "expectedParens",
242 type
243 }]
244 },
245 {
246 code: "a => a",
247 output: "(a) => a",
248 errors: [{
249 line: 1,
250 column: 1,
251 endColumn: 2,
252 messageId: "expectedParens",
253 type
254 }]
255 },
256 {
257 code: "a => {\n}",
258 output: "(a) => {\n}",
259 errors: [{
260 line: 1,
261 column: 1,
262 endColumn: 2,
263 messageId: "expectedParens",
264 type
265 }]
266 },
267 {
268 code: "a.then(foo => {});",
269 output: "a.then((foo) => {});",
270 errors: [{
271 line: 1,
272 column: 8,
273 endColumn: 11,
274 messageId: "expectedParens",
275 type
276 }]
277 },
278 {
279 code: "a.then(foo => a);",
280 output: "a.then((foo) => a);",
281 errors: [{
282 line: 1,
283 column: 8,
284 endColumn: 11,
285 messageId: "expectedParens",
286 type
287 }]
288 },
289 {
290 code: "a(foo => { if (true) {}; });",
291 output: "a((foo) => { if (true) {}; });",
292 errors: [{
293 line: 1,
294 column: 3,
295 endColumn: 6,
296 messageId: "expectedParens",
297 type
298 }]
299 },
300 {
301 code: "a(async foo => { if (true) {}; });",
302 output: "a(async (foo) => { if (true) {}; });",
303 parserOptions: { ecmaVersion: 8 },
304 errors: [{
305 line: 1,
306 column: 9,
307 endColumn: 12,
308 messageId: "expectedParens",
309 type
310 }]
311 },
312
313 // "as-needed"
314 {
315 code: "(a) => a",
316 output: "a => a",
317 options: ["as-needed"],
318 errors: [{
319 line: 1,
320 column: 2,
321 endColumn: 3,
322 messageId: "unexpectedParens",
323 type
324 }]
325 },
326 {
327 code: "( a ) => b",
328 output: "a => b",
329 options: ["as-needed"],
330 errors: [{
331 line: 1,
332 column: 4,
333 endColumn: 5,
334 messageId: "unexpectedParens",
335 type
336 }]
337 },
338 {
339 code: "(\na\n) => b",
340 output: "a => b",
341 options: ["as-needed"],
342 errors: [{
343 line: 2,
344 column: 1,
345 endColumn: 2,
346 messageId: "unexpectedParens",
347 type
348 }]
349 },
350 {
351 code: "(a,) => a",
352 output: "a => a",
353 options: ["as-needed"],
354 parserOptions: { ecmaVersion: 8 },
355 errors: [{
356 line: 1,
357 column: 2,
358 endColumn: 3,
359 messageId: "unexpectedParens",
360 type
361 }]
362 },
363 {
364 code: "async (a) => a",
365 output: "async a => a",
366 options: ["as-needed"],
367 parserOptions: { ecmaVersion: 8 },
368 errors: [{
369 line: 1,
370 column: 8,
371 endColumn: 9,
372 messageId: "unexpectedParens",
373 type
374 }]
375 },
376 {
377 code: "async(a) => a",
378 output: "async a => a",
379 options: ["as-needed"],
380 parserOptions: { ecmaVersion: 8 },
381 errors: [{
382 line: 1,
383 column: 7,
384 endColumn: 8,
385 messageId: "unexpectedParens",
386 type
387 }]
388 },
389 {
390 code: "typeof((a) => {})",
391 output: "typeof(a => {})",
392 options: ["as-needed"],
393 errors: [{
394 line: 1,
395 column: 9,
396 endColumn: 10,
397 messageId: "unexpectedParens",
398 type
399 }]
400 },
401 {
402 code: "function *f() { yield(a) => a; }",
403 output: "function *f() { yield a => a; }",
404 options: ["as-needed"],
405 errors: [{
406 line: 1,
407 column: 23,
408 endColumn: 24,
409 messageId: "unexpectedParens",
410 type
411 }]
412 },
413
414 // "as-needed", { "requireForBlockBody": true }
415 {
416 code: "a => {}",
417 output: "(a) => {}",
418 options: ["as-needed", { requireForBlockBody: true }],
419 errors: [{
420 line: 1,
421 column: 1,
422 endColumn: 2,
423 messageId: "expectedParensBlock",
424 type
425 }]
426 },
427 {
428 code: "(a) => a",
429 output: "a => a",
430 options: ["as-needed", { requireForBlockBody: true }],
431 errors: [{
432 line: 1,
433 column: 2,
434 endColumn: 3,
435 messageId: "unexpectedParensInline",
436 type
437 }]
438 },
439 {
440 code: "async a => {}",
441 output: "async (a) => {}",
442 options: ["as-needed", { requireForBlockBody: true }],
443 parserOptions: { ecmaVersion: 8 },
444 errors: [{
445 line: 1,
446 column: 7,
447 endColumn: 8,
448 messageId: "expectedParensBlock",
449 type
450 }]
451 },
452 {
453 code: "async (a) => a",
454 output: "async a => a",
455 options: ["as-needed", { requireForBlockBody: true }],
456 parserOptions: { ecmaVersion: 8 },
457 errors: [{
458 line: 1,
459 column: 8,
460 endColumn: 9,
461 messageId: "unexpectedParensInline",
462 type
463 }]
464 },
465 {
466 code: "async(a) => a",
467 output: "async a => a",
468 options: ["as-needed", { requireForBlockBody: true }],
469 parserOptions: { ecmaVersion: 8 },
470 errors: [{
471 line: 1,
472 column: 7,
473 endColumn: 8,
474 messageId: "unexpectedParensInline",
475 type
476 }]
477 },
478 {
479 code: "const f = /** @type {number} */(a)/**hello*/ => a + a;",
480 options: ["as-needed"],
481 output: "const f = /** @type {number} */a/**hello*/ => a + a;",
482 errors: [{
483 line: 1,
484 column: 33,
485 type,
486 messageId: "unexpectedParens",
487 endLine: 1,
488 endColumn: 34
489 }]
490 },
491 {
492 code: "const f = //\n(a) => a + a;",
493 output: "const f = //\na => a + a;",
494 options: ["as-needed"],
495 errors: [{
496 line: 2,
497 column: 2,
498 type,
499 messageId: "unexpectedParens",
500 endLine: 2,
501 endColumn: 3
502 }]
503 },
504 {
505 code: "var foo = /**/ a => b;",
506 output: "var foo = /**/ (a) => b;",
507 errors: [{
508 line: 1,
509 column: 16,
510 type: "ArrowFunctionExpression",
511 messageId: "expectedParens",
512 endLine: 1,
513 endColumn: 17
514 }]
515 },
516 {
517 code: "var bar = a /**/ => b;",
518 output: "var bar = (a) /**/ => b;",
519 errors: [{
520 line: 1,
521 column: 11,
522 type: "ArrowFunctionExpression",
523 messageId: "expectedParens",
524 endLine: 1,
525 endColumn: 12
526 }]
527 },
528 {
529 code: `const foo = a => {};
530
531 // comment between 'a' and an unrelated closing paren
532
533 bar();`,
534 output: `const foo = (a) => {};
535
536 // comment between 'a' and an unrelated closing paren
537
538 bar();`,
539 errors: [{
540 line: 1,
541 column: 13,
542 type: "ArrowFunctionExpression",
543 messageId: "expectedParens",
544 endLine: 1,
545 endColumn: 14
546 }]
547 }
548
549 ];
550
551 ruleTester.run("arrow-parens", rule, {
552 valid,
553 invalid
554 });