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