]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/comma-dangle.js
import 8.41.0 source
[pve-eslint.git] / eslint / tests / lib / rules / comma-dangle.js
1 /**
2 * @fileoverview Tests for comma-dangle rule.
3 * @author Ian Christian Myers
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const path = require("path"),
13 { unIndent } = require("../../_utils"),
14 rule = require("../../../lib/rules/comma-dangle"),
15 { RuleTester } = require("../../../lib/rule-tester"),
16 FlatRuleTester = require("../../../lib/rule-tester/flat-rule-tester");
17
18 //------------------------------------------------------------------------------
19 // Helpers
20 //------------------------------------------------------------------------------
21
22 /**
23 * Gets the path to the parser of the given name.
24 * @param {string} name The name of a parser to get.
25 * @returns {string} The path to the specified parser.
26 */
27 function parser(name) {
28 return path.resolve(
29 __dirname,
30 `../../fixtures/parsers/comma-dangle/${name}.js`
31 );
32 }
33
34 //------------------------------------------------------------------------------
35 // Tests
36 //------------------------------------------------------------------------------
37
38 const ruleTester = new RuleTester();
39
40 ruleTester.defineRule("add-named-import", {
41 meta: {
42 fixable: "code"
43 },
44 create(context) {
45 return {
46 ImportDeclaration(node) {
47 const sourceCode = context.sourceCode;
48 const closingBrace = sourceCode.getLastToken(node, token => token.value === "}");
49 const addComma = sourceCode.getTokenBefore(closingBrace).value !== ",";
50
51 context.report({
52 message: "Add I18nManager.",
53 node,
54 fix(fixer) {
55 return fixer.insertTextBefore(closingBrace, `${addComma ? "," : ""}I18nManager`);
56 }
57 });
58 }
59 };
60 }
61 });
62
63 ruleTester.run("comma-dangle", rule, {
64 valid: [
65 "var foo = { bar: 'baz' }",
66 "var foo = {\nbar: 'baz'\n}",
67 "var foo = [ 'baz' ]",
68 "var foo = [\n'baz'\n]",
69 "[,,]",
70 "[\n,\n,\n]",
71 "[,]",
72 "[\n,\n]",
73 "[]",
74 "[\n]",
75 { code: "var foo = [\n (bar ? baz : qux),\n ];", options: ["always-multiline"] },
76 { code: "var foo = { bar: 'baz' }", options: ["never"] },
77 { code: "var foo = {\nbar: 'baz'\n}", options: ["never"] },
78 { code: "var foo = [ 'baz' ]", options: ["never"] },
79 { code: "var { a, b } = foo;", options: ["never"], parserOptions: { ecmaVersion: 6 } },
80 { code: "var [ a, b ] = foo;", options: ["never"], parserOptions: { ecmaVersion: 6 } },
81 { code: "var { a,\n b, \n} = foo;", options: ["only-multiline"], parserOptions: { ecmaVersion: 6 } },
82 { code: "var [ a,\n b, \n] = foo;", options: ["only-multiline"], parserOptions: { ecmaVersion: 6 } },
83
84 { code: "[(1),]", options: ["always"] },
85 { code: "var x = { foo: (1),};", options: ["always"] },
86 { code: "var foo = { bar: 'baz', }", options: ["always"] },
87 { code: "var foo = {\nbar: 'baz',\n}", options: ["always"] },
88 { code: "var foo = {\nbar: 'baz'\n,}", options: ["always"] },
89 { code: "var foo = [ 'baz', ]", options: ["always"] },
90 { code: "var foo = [\n'baz',\n]", options: ["always"] },
91 { code: "var foo = [\n'baz'\n,]", options: ["always"] },
92 { code: "[,,]", options: ["always"] },
93 { code: "[\n,\n,\n]", options: ["always"] },
94 { code: "[,]", options: ["always"] },
95 { code: "[\n,\n]", options: ["always"] },
96 { code: "[]", options: ["always"] },
97 { code: "[\n]", options: ["always"] },
98
99 { code: "var foo = { bar: 'baz' }", options: ["always-multiline"] },
100 { code: "var foo = { bar: 'baz' }", options: ["only-multiline"] },
101 { code: "var foo = {\nbar: 'baz',\n}", options: ["always-multiline"] },
102 { code: "var foo = {\nbar: 'baz',\n}", options: ["only-multiline"] },
103 { code: "var foo = [ 'baz' ]", options: ["always-multiline"] },
104 { code: "var foo = [ 'baz' ]", options: ["only-multiline"] },
105 { code: "var foo = [\n'baz',\n]", options: ["always-multiline"] },
106 { code: "var foo = [\n'baz',\n]", options: ["only-multiline"] },
107 { code: "var foo = { bar:\n\n'bar' }", options: ["always-multiline"] },
108 { code: "var foo = { bar:\n\n'bar' }", options: ["only-multiline"] },
109 { code: "var foo = {a: 1, b: 2, c: 3, d: 4}", options: ["always-multiline"] },
110 { code: "var foo = {a: 1, b: 2, c: 3, d: 4}", options: ["only-multiline"] },
111 { code: "var foo = {a: 1, b: 2,\n c: 3, d: 4}", options: ["always-multiline"] },
112 { code: "var foo = {a: 1, b: 2,\n c: 3, d: 4}", options: ["only-multiline"] },
113 { code: "var foo = {x: {\nfoo: 'bar',\n}}", options: ["always-multiline"] },
114 { code: "var foo = {x: {\nfoo: 'bar',\n}}", options: ["only-multiline"] },
115 { code: "var foo = new Map([\n[key, {\na: 1,\nb: 2,\nc: 3,\n}],\n])", options: ["always-multiline"] },
116 { code: "var foo = new Map([\n[key, {\na: 1,\nb: 2,\nc: 3,\n}],\n])", options: ["only-multiline"] },
117
118 // https://github.com/eslint/eslint/issues/3627
119 {
120 code: "var [a, ...rest] = [];",
121 options: ["always"],
122 parserOptions: { ecmaVersion: 6 }
123 },
124 {
125 code: "var [\n a,\n ...rest\n] = [];",
126 options: ["always"],
127 parserOptions: { ecmaVersion: 6 }
128 },
129 {
130 code: "var [\n a,\n ...rest\n] = [];",
131 options: ["always-multiline"],
132 parserOptions: { ecmaVersion: 6 }
133 },
134 {
135 code: "var [\n a,\n ...rest\n] = [];",
136 options: ["only-multiline"],
137 parserOptions: { ecmaVersion: 6 }
138 },
139 {
140 code: "[a, ...rest] = [];",
141 options: ["always"],
142 parserOptions: { ecmaVersion: 6 }
143 },
144 {
145 code: "for ([a, ...rest] of []);",
146 options: ["always"],
147 parserOptions: { ecmaVersion: 6 }
148 },
149 {
150 code: "var a = [b, ...spread,];",
151 options: ["always"],
152 parserOptions: { ecmaVersion: 6 }
153 },
154
155 // https://github.com/eslint/eslint/issues/7297
156 {
157 code: "var {foo, ...bar} = baz",
158 options: ["always"],
159 parserOptions: { ecmaVersion: 2018 }
160 },
161
162 // https://github.com/eslint/eslint/issues/3794
163 {
164 code: "import {foo,} from 'foo';",
165 options: ["always"],
166 parserOptions: { ecmaVersion: 6, sourceType: "module" }
167 },
168 {
169 code: "import foo from 'foo';",
170 options: ["always"],
171 parserOptions: { ecmaVersion: 6, sourceType: "module" }
172 },
173 {
174 code: "import foo, {abc,} from 'foo';",
175 options: ["always"],
176 parserOptions: { ecmaVersion: 6, sourceType: "module" }
177 },
178 {
179 code: "import * as foo from 'foo';",
180 options: ["always"],
181 parserOptions: { ecmaVersion: 6, sourceType: "module" }
182 },
183 {
184 code: "export {foo,} from 'foo';",
185 options: ["always"],
186 parserOptions: { ecmaVersion: 6, sourceType: "module" }
187 },
188 {
189 code: "import {foo} from 'foo';",
190 options: ["never"],
191 parserOptions: { ecmaVersion: 6, sourceType: "module" }
192 },
193 {
194 code: "import foo from 'foo';",
195 options: ["never"],
196 parserOptions: { ecmaVersion: 6, sourceType: "module" }
197 },
198 {
199 code: "import foo, {abc} from 'foo';",
200 options: ["never"],
201 parserOptions: { ecmaVersion: 6, sourceType: "module" }
202 },
203 {
204 code: "import * as foo from 'foo';",
205 options: ["never"],
206 parserOptions: { ecmaVersion: 6, sourceType: "module" }
207 },
208 {
209 code: "export {foo} from 'foo';",
210 options: ["never"],
211 parserOptions: { ecmaVersion: 6, sourceType: "module" }
212 },
213 {
214 code: "import {foo} from 'foo';",
215 options: ["always-multiline"],
216 parserOptions: { ecmaVersion: 6, sourceType: "module" }
217 },
218 {
219 code: "import {foo} from 'foo';",
220 options: ["only-multiline"],
221 parserOptions: { ecmaVersion: 6, sourceType: "module" }
222 },
223 {
224 code: "export {foo} from 'foo';",
225 options: ["always-multiline"],
226 parserOptions: { ecmaVersion: 6, sourceType: "module" }
227 },
228 {
229 code: "export {foo} from 'foo';",
230 options: ["only-multiline"],
231 parserOptions: { ecmaVersion: 6, sourceType: "module" }
232 },
233 {
234 code: "import {\n foo,\n} from 'foo';",
235 options: ["always-multiline"],
236 parserOptions: { ecmaVersion: 6, sourceType: "module" }
237 },
238 {
239 code: "import {\n foo,\n} from 'foo';",
240 options: ["only-multiline"],
241 parserOptions: { ecmaVersion: 6, sourceType: "module" }
242 },
243 {
244 code: "export {\n foo,\n} from 'foo';",
245 options: ["always-multiline"],
246 parserOptions: { ecmaVersion: 6, sourceType: "module" }
247 },
248 {
249 code: "export {\n foo,\n} from 'foo';",
250 options: ["only-multiline"],
251 parserOptions: { ecmaVersion: 6, sourceType: "module" }
252 },
253 {
254 code: "import {foo} from \n'foo';",
255 options: ["always-multiline"],
256 parserOptions: { ecmaVersion: 6, sourceType: "module" }
257 },
258 {
259 code: "import {foo} from \n'foo';",
260 options: ["only-multiline"],
261 parserOptions: { ecmaVersion: 6, sourceType: "module" }
262 },
263 {
264 code: "function foo(a) {}",
265 options: ["always"]
266 },
267 {
268 code: "foo(a)",
269 options: ["always"]
270 },
271 {
272 code: "function foo(a) {}",
273 options: ["never"]
274 },
275 {
276 code: "foo(a)",
277 options: ["never"]
278 },
279 {
280 code: "function foo(a,\nb) {}",
281 options: ["always-multiline"]
282 },
283 {
284 code: "foo(a,\nb\n)",
285 options: ["always-multiline"]
286 },
287 {
288 code: "function foo(a,\nb\n) {}",
289 options: ["always-multiline"]
290 },
291 {
292 code: "foo(a,\nb)",
293 options: ["always-multiline"]
294 },
295 {
296 code: "function foo(a,\nb) {}",
297 options: ["only-multiline"]
298 },
299 {
300 code: "foo(a,\nb)",
301 options: ["only-multiline"]
302 },
303 {
304 code: "function foo(a) {}",
305 options: ["always"],
306 parserOptions: { ecmaVersion: 7 }
307 },
308 {
309 code: "foo(a)",
310 options: ["always"],
311 parserOptions: { ecmaVersion: 7 }
312 },
313 {
314 code: "function foo(a) {}",
315 options: ["never"],
316 parserOptions: { ecmaVersion: 7 }
317 },
318 {
319 code: "foo(a)",
320 options: ["never"],
321 parserOptions: { ecmaVersion: 7 }
322 },
323 {
324 code: "function foo(a,\nb) {}",
325 options: ["always-multiline"],
326 parserOptions: { ecmaVersion: 7 }
327 },
328 {
329 code: "foo(a,\nb)",
330 options: ["always-multiline"],
331 parserOptions: { ecmaVersion: 7 }
332 },
333 {
334 code: "function foo(a,\nb\n) {}",
335 options: ["always-multiline"],
336 parserOptions: { ecmaVersion: 7 }
337 },
338 {
339 code: "foo(a,\nb\n)",
340 options: ["always-multiline"],
341 parserOptions: { ecmaVersion: 7 }
342 },
343 {
344 code: "function foo(a,\nb) {}",
345 options: ["only-multiline"],
346 parserOptions: { ecmaVersion: 7 }
347 },
348 {
349 code: "foo(a,\nb)",
350 options: ["only-multiline"],
351 parserOptions: { ecmaVersion: 7 }
352 },
353 {
354 code: "function foo(a) {}",
355 options: ["never"],
356 parserOptions: { ecmaVersion: 8 }
357 },
358 {
359 code: "foo(a)",
360 options: ["never"],
361 parserOptions: { ecmaVersion: 8 }
362 },
363 {
364 code: "function foo(a,) {}",
365 options: ["always"],
366 parserOptions: { ecmaVersion: 8 }
367 },
368 {
369 code: "foo(a,)",
370 options: ["always"],
371 parserOptions: { ecmaVersion: 8 }
372 },
373 {
374 code: "function foo(\na,\nb,\n) {}",
375 options: ["always-multiline"],
376 parserOptions: { ecmaVersion: 8 }
377 },
378 {
379 code: "foo(\na,b)",
380 options: ["always-multiline"],
381 parserOptions: { ecmaVersion: 8 }
382 },
383 {
384 code: "function foo(a,b) {}",
385 options: ["always-multiline"],
386 parserOptions: { ecmaVersion: 8 }
387 },
388 {
389 code: "foo(a,b)",
390 options: ["always-multiline"],
391 parserOptions: { ecmaVersion: 8 }
392 },
393 {
394 code: "function foo(a,b) {}",
395 options: ["only-multiline"],
396 parserOptions: { ecmaVersion: 8 }
397 },
398 {
399 code: "foo(a,b)",
400 options: ["only-multiline"],
401 parserOptions: { ecmaVersion: 8 }
402 },
403
404 // trailing comma in functions
405 {
406 code: "function foo(a) {} ",
407 options: [{}],
408 parserOptions: { ecmaVersion: 8 }
409 },
410 {
411 code: "foo(a)",
412 options: [{}],
413 parserOptions: { ecmaVersion: 8 }
414 },
415 {
416 code: "function foo(a) {} ",
417 options: [{ functions: "never" }],
418 parserOptions: { ecmaVersion: 8 }
419 },
420 {
421 code: "foo(a)",
422 options: [{ functions: "never" }],
423 parserOptions: { ecmaVersion: 8 }
424 },
425 {
426 code: "function foo(a,) {}",
427 options: [{ functions: "always" }],
428 parserOptions: { ecmaVersion: 8 }
429 },
430 {
431 code: "function bar(a, ...b) {}",
432 options: [{ functions: "always" }],
433 parserOptions: { ecmaVersion: 8 }
434 },
435 {
436 code: "foo(a,)",
437 options: [{ functions: "always" }],
438 parserOptions: { ecmaVersion: 8 }
439 },
440 {
441 code: "foo(a,)",
442 options: [{ functions: "always" }],
443 parserOptions: { ecmaVersion: 9 }
444 },
445 {
446 code: "bar(...a,)",
447 options: [{ functions: "always" }],
448 parserOptions: { ecmaVersion: 8 }
449 },
450 {
451 code: "function foo(a) {} ",
452 options: [{ functions: "always-multiline" }],
453 parserOptions: { ecmaVersion: 8 }
454 },
455 {
456 code: "foo(a)",
457 options: [{ functions: "always-multiline" }],
458 parserOptions: { ecmaVersion: 8 }
459 },
460 {
461 code: "function foo(\na,\nb,\n) {} ",
462 options: [{ functions: "always-multiline" }],
463 parserOptions: { ecmaVersion: 8 }
464 },
465 {
466 code: "function foo(\na,\n...b\n) {} ",
467 options: [{ functions: "always-multiline" }],
468 parserOptions: { ecmaVersion: 8 }
469 },
470 {
471 code: "foo(\na,\nb,\n)",
472 options: [{ functions: "always-multiline" }],
473 parserOptions: { ecmaVersion: 8 }
474 },
475 {
476 code: "foo(\na,\n...b,\n)",
477 options: [{ functions: "always-multiline" }],
478 parserOptions: { ecmaVersion: 8 }
479 },
480 {
481 code: "function foo(a) {} ",
482 options: [{ functions: "only-multiline" }],
483 parserOptions: { ecmaVersion: 8 }
484 },
485 {
486 code: "foo(a)",
487 options: [{ functions: "only-multiline" }],
488 parserOptions: { ecmaVersion: 8 }
489 },
490 {
491 code: "function foo(\na,\nb,\n) {} ",
492 options: [{ functions: "only-multiline" }],
493 parserOptions: { ecmaVersion: 8 }
494 },
495 {
496 code: "foo(\na,\nb,\n)",
497 options: [{ functions: "only-multiline" }],
498 parserOptions: { ecmaVersion: 8 }
499 },
500 {
501 code: "function foo(\na,\nb\n) {} ",
502 options: [{ functions: "only-multiline" }],
503 parserOptions: { ecmaVersion: 8 }
504 },
505 {
506 code: "foo(\na,\nb\n)",
507 options: [{ functions: "only-multiline" }],
508 parserOptions: { ecmaVersion: 8 }
509 },
510
511 // https://github.com/eslint/eslint/issues/7370
512 {
513 code: "function foo({a}: {a: string,}) {}",
514 options: ["never"],
515 parser: parser("object-pattern-1")
516 },
517 {
518 code: "function foo({a,}: {a: string}) {}",
519 options: ["always"],
520 parser: parser("object-pattern-2")
521 },
522 {
523 code: "function foo(a): {b: boolean,} {}",
524 options: [{ functions: "never" }],
525 parser: parser("return-type-1")
526 },
527 {
528 code: "function foo(a,): {b: boolean} {}",
529 options: [{ functions: "always" }],
530 parser: parser("return-type-2")
531 }
532 ],
533 invalid: [
534 {
535 code: "var foo = { bar: 'baz', }",
536 output: "var foo = { bar: 'baz' }",
537 errors: [
538 {
539 messageId: "unexpected",
540 type: "Property",
541 line: 1,
542 column: 23,
543 endColumn: 24
544 }
545 ]
546 },
547 {
548 code: "var foo = {\nbar: 'baz',\n}",
549 output: "var foo = {\nbar: 'baz'\n}",
550 errors: [
551 {
552 messageId: "unexpected",
553 type: "Property",
554 line: 2,
555 column: 11,
556 endColumn: 12
557 }
558 ]
559 },
560 {
561 code: "foo({ bar: 'baz', qux: 'quux', });",
562 output: "foo({ bar: 'baz', qux: 'quux' });",
563 errors: [
564 {
565 messageId: "unexpected",
566 type: "Property",
567 line: 1,
568 column: 30
569 }
570 ]
571 },
572 {
573 code: "foo({\nbar: 'baz',\nqux: 'quux',\n});",
574 output: "foo({\nbar: 'baz',\nqux: 'quux'\n});",
575 errors: [
576 {
577 messageId: "unexpected",
578 type: "Property",
579 line: 3,
580 column: 12
581 }
582 ]
583 },
584 {
585 code: "var foo = [ 'baz', ]",
586 output: "var foo = [ 'baz' ]",
587 errors: [
588 {
589 messageId: "unexpected",
590 type: "Literal",
591 line: 1,
592 column: 18
593 }
594 ]
595 },
596 {
597 code: "var foo = [ 'baz',\n]",
598 output: "var foo = [ 'baz'\n]",
599 errors: [
600 {
601 messageId: "unexpected",
602 type: "Literal",
603 line: 1,
604 column: 18
605 }
606 ]
607 },
608 {
609 code: "var foo = { bar: 'bar'\n\n, }",
610 output: "var foo = { bar: 'bar'\n\n }",
611 errors: [
612 {
613 messageId: "unexpected",
614 type: "Property",
615 line: 3,
616 column: 1
617 }
618 ]
619 },
620
621
622 {
623 code: "var foo = { bar: 'baz', }",
624 output: "var foo = { bar: 'baz' }",
625 options: ["never"],
626 errors: [
627 {
628 messageId: "unexpected",
629 type: "Property",
630 line: 1,
631 column: 23
632 }
633 ]
634 },
635 {
636 code: "var foo = { bar: 'baz', }",
637 output: "var foo = { bar: 'baz' }",
638 options: ["only-multiline"],
639 errors: [
640 {
641 messageId: "unexpected",
642 type: "Property",
643 line: 1,
644 column: 23
645 }
646 ]
647 },
648 {
649 code: "var foo = {\nbar: 'baz',\n}",
650 output: "var foo = {\nbar: 'baz'\n}",
651 options: ["never"],
652 errors: [
653 {
654 messageId: "unexpected",
655 type: "Property",
656 line: 2,
657 column: 11
658 }
659 ]
660 },
661 {
662 code: "foo({ bar: 'baz', qux: 'quux', });",
663 output: "foo({ bar: 'baz', qux: 'quux' });",
664 options: ["never"],
665 errors: [
666 {
667 messageId: "unexpected",
668 type: "Property",
669 line: 1,
670 column: 30
671 }
672 ]
673 },
674 {
675 code: "foo({ bar: 'baz', qux: 'quux', });",
676 output: "foo({ bar: 'baz', qux: 'quux' });",
677 options: ["only-multiline"],
678 errors: [
679 {
680 messageId: "unexpected",
681 type: "Property",
682 line: 1,
683 column: 30
684 }
685 ]
686 },
687
688 {
689 code: "var foo = { bar: 'baz' }",
690 output: "var foo = { bar: 'baz', }",
691 options: ["always"],
692 errors: [
693 {
694 messageId: "missing",
695 type: "Property",
696 line: 1,
697 column: 23,
698 endLine: 1,
699 endColumn: 24
700 }
701 ]
702 },
703 {
704 code: "var foo = {\nbar: 'baz'\n}",
705 output: "var foo = {\nbar: 'baz',\n}",
706 options: ["always"],
707 errors: [
708 {
709 messageId: "missing",
710 type: "Property",
711 line: 2,
712 column: 11,
713 endLine: 3,
714 endColumn: 1
715 }
716 ]
717 },
718 {
719 code: "var foo = {\nbar: 'baz'\r\n}",
720 output: "var foo = {\nbar: 'baz',\r\n}",
721 options: ["always"],
722 errors: [
723 {
724 messageId: "missing",
725 type: "Property",
726 line: 2,
727 column: 11,
728 endLine: 3,
729 endColumn: 1
730 }
731 ]
732 },
733 {
734 code: "foo({ bar: 'baz', qux: 'quux' });",
735 output: "foo({ bar: 'baz', qux: 'quux', });",
736 options: ["always"],
737 errors: [
738 {
739 messageId: "missing",
740 type: "Property",
741 line: 1,
742 column: 30,
743 endLine: 1,
744 endColumn: 31
745
746 }
747 ]
748 },
749 {
750 code: "foo({\nbar: 'baz',\nqux: 'quux'\n});",
751 output: "foo({\nbar: 'baz',\nqux: 'quux',\n});",
752 options: ["always"],
753 errors: [
754 {
755 messageId: "missing",
756 type: "Property",
757 line: 3,
758 column: 12,
759 endLine: 4,
760 endColumn: 1
761 }
762 ]
763 },
764 {
765 code: "var foo = [ 'baz' ]",
766 output: "var foo = [ 'baz', ]",
767 options: ["always"],
768 errors: [
769 {
770 messageId: "missing",
771 type: "Literal",
772 line: 1,
773 column: 18
774 }
775 ]
776 },
777 {
778 code: "var foo = ['baz']",
779 output: "var foo = ['baz',]",
780 options: ["always"],
781 errors: [
782 {
783 messageId: "missing",
784 type: "Literal",
785 line: 1,
786 column: 17,
787 endColumn: 18
788 }
789 ]
790 },
791 {
792 code: "var foo = [ 'baz'\n]",
793 output: "var foo = [ 'baz',\n]",
794 options: ["always"],
795 errors: [
796 {
797 messageId: "missing",
798 type: "Literal",
799 line: 1,
800 column: 18
801 }
802 ]
803 },
804 {
805 code: "var foo = { bar:\n\n'bar' }",
806 output: "var foo = { bar:\n\n'bar', }",
807 options: ["always"],
808 errors: [
809 {
810 messageId: "missing",
811 type: "Property",
812 line: 3,
813 column: 6
814 }
815 ]
816 },
817
818 {
819 code: "var foo = {\nbar: 'baz'\n}",
820 output: "var foo = {\nbar: 'baz',\n}",
821 options: ["always-multiline"],
822 errors: [
823 {
824 messageId: "missing",
825 type: "Property",
826 line: 2,
827 column: 11
828 }
829 ]
830 },
831 {
832 code:
833 "var foo = [\n" +
834 " bar,\n" +
835 " (\n" +
836 " baz\n" +
837 " )\n" +
838 "];",
839 output:
840 "var foo = [\n" +
841 " bar,\n" +
842 " (\n" +
843 " baz\n" +
844 " ),\n" +
845 "];",
846 options: ["always"],
847 errors: [
848 {
849 messageId: "missing",
850 type: "Identifier",
851 line: 5,
852 column: 4
853 }
854 ]
855 },
856 {
857 code:
858 "var foo = {\n" +
859 " foo: 'bar',\n" +
860 " baz: (\n" +
861 " qux\n" +
862 " )\n" +
863 "};",
864 output:
865 "var foo = {\n" +
866 " foo: 'bar',\n" +
867 " baz: (\n" +
868 " qux\n" +
869 " ),\n" +
870 "};",
871 options: ["always"],
872 errors: [
873 {
874 messageId: "missing",
875 type: "Property",
876 line: 5,
877 column: 4
878 }
879 ]
880 },
881 {
882
883 // https://github.com/eslint/eslint/issues/7291
884 code:
885 "var foo = [\n" +
886 " (bar\n" +
887 " ? baz\n" +
888 " : qux\n" +
889 " )\n" +
890 "];",
891 output:
892 "var foo = [\n" +
893 " (bar\n" +
894 " ? baz\n" +
895 " : qux\n" +
896 " ),\n" +
897 "];",
898 options: ["always"],
899 errors: [
900 {
901 messageId: "missing",
902 type: "ConditionalExpression",
903 line: 5,
904 column: 4
905 }
906 ]
907 },
908 {
909 code: "var foo = { bar: 'baz', }",
910 output: "var foo = { bar: 'baz' }",
911 options: ["always-multiline"],
912 errors: [
913 {
914 messageId: "unexpected",
915 type: "Property",
916 line: 1,
917 column: 23
918 }
919 ]
920 },
921 {
922 code: "foo({\nbar: 'baz',\nqux: 'quux'\n});",
923 output: "foo({\nbar: 'baz',\nqux: 'quux',\n});",
924 options: ["always-multiline"],
925 errors: [
926 {
927 messageId: "missing",
928 type: "Property",
929 line: 3,
930 column: 12
931 }
932 ]
933 },
934 {
935 code: "foo({ bar: 'baz', qux: 'quux', });",
936 output: "foo({ bar: 'baz', qux: 'quux' });",
937 options: ["always-multiline"],
938 errors: [
939 {
940 messageId: "unexpected",
941 type: "Property",
942 line: 1,
943 column: 30
944 }
945 ]
946 },
947 {
948 code: "var foo = [\n'baz'\n]",
949 output: "var foo = [\n'baz',\n]",
950 options: ["always-multiline"],
951 errors: [
952 {
953 messageId: "missing",
954 type: "Literal",
955 line: 2,
956 column: 6
957 }
958 ]
959 },
960 {
961 code: "var foo = ['baz',]",
962 output: "var foo = ['baz']",
963 options: ["always-multiline"],
964 errors: [
965 {
966 messageId: "unexpected",
967 type: "Literal",
968 line: 1,
969 column: 17
970 }
971 ]
972 },
973 {
974 code: "var foo = ['baz',]",
975 output: "var foo = ['baz']",
976 options: ["only-multiline"],
977 errors: [
978 {
979 messageId: "unexpected",
980 type: "Literal",
981 line: 1,
982 column: 17
983 }
984 ]
985 },
986 {
987 code: "var foo = {x: {\nfoo: 'bar',\n},}",
988 output: "var foo = {x: {\nfoo: 'bar',\n}}",
989 options: ["always-multiline"],
990 errors: [
991 {
992 messageId: "unexpected",
993 type: "Property",
994 line: 3,
995 column: 2
996 }
997 ]
998 },
999 {
1000 code: "var foo = {a: 1, b: 2,\nc: 3, d: 4,}",
1001 output: "var foo = {a: 1, b: 2,\nc: 3, d: 4}",
1002 options: ["always-multiline"],
1003 errors: [
1004 {
1005 messageId: "unexpected",
1006 type: "Property",
1007 line: 2,
1008 column: 11
1009 }
1010 ]
1011 },
1012 {
1013 code: "var foo = {a: 1, b: 2,\nc: 3, d: 4,}",
1014 output: "var foo = {a: 1, b: 2,\nc: 3, d: 4}",
1015 options: ["only-multiline"],
1016 errors: [
1017 {
1018 messageId: "unexpected",
1019 type: "Property",
1020 line: 2,
1021 column: 11
1022 }
1023 ]
1024 },
1025 {
1026 code: "var foo = [{\na: 1,\nb: 2,\nc: 3,\nd: 4,\n},]",
1027 output: "var foo = [{\na: 1,\nb: 2,\nc: 3,\nd: 4,\n}]",
1028 options: ["always-multiline"],
1029 errors: [
1030 {
1031 messageId: "unexpected",
1032 type: "ObjectExpression",
1033 line: 6,
1034 column: 2
1035 }
1036 ]
1037 },
1038 {
1039 code: "var { a, b, } = foo;",
1040 output: "var { a, b } = foo;",
1041 options: ["never"],
1042 parserOptions: { ecmaVersion: 6 },
1043 errors: [
1044 {
1045 messageId: "unexpected",
1046 type: "Property",
1047 line: 1,
1048 column: 11
1049 }
1050 ]
1051 },
1052 {
1053 code: "var { a, b, } = foo;",
1054 output: "var { a, b } = foo;",
1055 options: ["only-multiline"],
1056 parserOptions: { ecmaVersion: 6 },
1057 errors: [
1058 {
1059 messageId: "unexpected",
1060 type: "Property",
1061 line: 1,
1062 column: 11
1063 }
1064 ]
1065 },
1066 {
1067 code: "var [ a, b, ] = foo;",
1068 output: "var [ a, b ] = foo;",
1069 options: ["never"],
1070 parserOptions: { ecmaVersion: 6 },
1071 errors: [
1072 {
1073 messageId: "unexpected",
1074 type: "Identifier",
1075 line: 1,
1076 column: 11
1077 }
1078 ]
1079 },
1080 {
1081 code: "var [ a, b, ] = foo;",
1082 output: "var [ a, b ] = foo;",
1083 options: ["only-multiline"],
1084 parserOptions: { ecmaVersion: 6 },
1085 errors: [
1086 {
1087 messageId: "unexpected",
1088 type: "Identifier",
1089 line: 1,
1090 column: 11
1091 }
1092 ]
1093 },
1094 {
1095 code: "[(1),]",
1096 output: "[(1)]",
1097 options: ["never"],
1098 errors: [
1099 {
1100 messageId: "unexpected",
1101 type: "Literal",
1102 line: 1,
1103 column: 5
1104 }
1105 ]
1106 },
1107 {
1108 code: "[(1),]",
1109 output: "[(1)]",
1110 options: ["only-multiline"],
1111 errors: [
1112 {
1113 messageId: "unexpected",
1114 type: "Literal",
1115 line: 1,
1116 column: 5
1117 }
1118 ]
1119 },
1120 {
1121 code: "var x = { foo: (1),};",
1122 output: "var x = { foo: (1)};",
1123 options: ["never"],
1124 errors: [
1125 {
1126 messageId: "unexpected",
1127 type: "Property",
1128 line: 1,
1129 column: 19
1130 }
1131 ]
1132 },
1133 {
1134 code: "var x = { foo: (1),};",
1135 output: "var x = { foo: (1)};",
1136 options: ["only-multiline"],
1137 errors: [
1138 {
1139 messageId: "unexpected",
1140 type: "Property",
1141 line: 1,
1142 column: 19
1143 }
1144 ]
1145 },
1146
1147 // https://github.com/eslint/eslint/issues/3794
1148 {
1149 code: "import {foo} from 'foo';",
1150 output: "import {foo,} from 'foo';",
1151 options: ["always"],
1152 parserOptions: { ecmaVersion: 6, sourceType: "module" },
1153 errors: [{ messageId: "missing", type: "ImportSpecifier" }]
1154 },
1155 {
1156 code: "import foo, {abc} from 'foo';",
1157 output: "import foo, {abc,} from 'foo';",
1158 options: ["always"],
1159 parserOptions: { ecmaVersion: 6, sourceType: "module" },
1160 errors: [{ messageId: "missing", type: "ImportSpecifier" }]
1161 },
1162 {
1163 code: "export {foo} from 'foo';",
1164 output: "export {foo,} from 'foo';",
1165 options: ["always"],
1166 parserOptions: { ecmaVersion: 6, sourceType: "module" },
1167 errors: [{ messageId: "missing", type: "ExportSpecifier" }]
1168 },
1169 {
1170 code: "import {foo,} from 'foo';",
1171 output: "import {foo} from 'foo';",
1172 options: ["never"],
1173 parserOptions: { ecmaVersion: 6, sourceType: "module" },
1174 errors: [{ messageId: "unexpected", type: "ImportSpecifier" }]
1175 },
1176 {
1177 code: "import {foo,} from 'foo';",
1178 output: "import {foo} from 'foo';",
1179 options: ["only-multiline"],
1180 parserOptions: { ecmaVersion: 6, sourceType: "module" },
1181 errors: [{ messageId: "unexpected", type: "ImportSpecifier" }]
1182 },
1183 {
1184 code: "import foo, {abc,} from 'foo';",
1185 output: "import foo, {abc} from 'foo';",
1186 options: ["never"],
1187 parserOptions: { ecmaVersion: 6, sourceType: "module" },
1188 errors: [{ messageId: "unexpected", type: "ImportSpecifier" }]
1189 },
1190 {
1191 code: "import foo, {abc,} from 'foo';",
1192 output: "import foo, {abc} from 'foo';",
1193 options: ["only-multiline"],
1194 parserOptions: { ecmaVersion: 6, sourceType: "module" },
1195 errors: [{ messageId: "unexpected", type: "ImportSpecifier" }]
1196 },
1197 {
1198 code: "export {foo,} from 'foo';",
1199 output: "export {foo} from 'foo';",
1200 options: ["never"],
1201 parserOptions: { ecmaVersion: 6, sourceType: "module" },
1202 errors: [{ messageId: "unexpected", type: "ExportSpecifier" }]
1203 },
1204 {
1205 code: "export {foo,} from 'foo';",
1206 output: "export {foo} from 'foo';",
1207 options: ["only-multiline"],
1208 parserOptions: { ecmaVersion: 6, sourceType: "module" },
1209 errors: [{ messageId: "unexpected", type: "ExportSpecifier" }]
1210 },
1211 {
1212 code: "import {foo,} from 'foo';",
1213 output: "import {foo} from 'foo';",
1214 options: ["always-multiline"],
1215 parserOptions: { ecmaVersion: 6, sourceType: "module" },
1216 errors: [{ messageId: "unexpected", type: "ImportSpecifier" }]
1217 },
1218 {
1219 code: "export {foo,} from 'foo';",
1220 output: "export {foo} from 'foo';",
1221 options: ["always-multiline"],
1222 parserOptions: { ecmaVersion: 6, sourceType: "module" },
1223 errors: [{ messageId: "unexpected", type: "ExportSpecifier" }]
1224 },
1225 {
1226 code: "import {\n foo\n} from 'foo';",
1227 output: "import {\n foo,\n} from 'foo';",
1228 options: ["always-multiline"],
1229 parserOptions: { ecmaVersion: 6, sourceType: "module" },
1230 errors: [{ messageId: "missing", type: "ImportSpecifier" }]
1231 },
1232 {
1233 code: "export {\n foo\n} from 'foo';",
1234 output: "export {\n foo,\n} from 'foo';",
1235 options: ["always-multiline"],
1236 parserOptions: { ecmaVersion: 6, sourceType: "module" },
1237 errors: [{ messageId: "missing", type: "ExportSpecifier" }]
1238 },
1239
1240 // https://github.com/eslint/eslint/issues/6233
1241 {
1242 code: "var foo = {a: (1)}",
1243 output: "var foo = {a: (1),}",
1244 options: ["always"],
1245 errors: [{ messageId: "missing", type: "Property" }]
1246 },
1247 {
1248 code: "var foo = [(1)]",
1249 output: "var foo = [(1),]",
1250 options: ["always"],
1251 errors: [{ messageId: "missing", type: "Literal" }]
1252 },
1253 {
1254 code: "var foo = [\n1,\n(2)\n]",
1255 output: "var foo = [\n1,\n(2),\n]",
1256 options: ["always-multiline"],
1257 errors: [{ messageId: "missing", type: "Literal" }]
1258 },
1259
1260 // trailing commas in functions
1261 {
1262 code: "function foo(a,) {}",
1263 output: "function foo(a) {}",
1264 options: [{ functions: "never" }],
1265 parserOptions: { ecmaVersion: 8 },
1266 errors: [{ messageId: "unexpected", type: "Identifier" }]
1267 },
1268 {
1269 code: "(function foo(a,) {})",
1270 output: "(function foo(a) {})",
1271 options: [{ functions: "never" }],
1272 parserOptions: { ecmaVersion: 8 },
1273 errors: [{ messageId: "unexpected", type: "Identifier" }]
1274 },
1275 {
1276 code: "(a,) => a",
1277 output: "(a) => a",
1278 options: [{ functions: "never" }],
1279 parserOptions: { ecmaVersion: 8 },
1280 errors: [{ messageId: "unexpected", type: "Identifier" }]
1281 },
1282 {
1283 code: "(a,) => (a)",
1284 output: "(a) => (a)",
1285 options: [{ functions: "never" }],
1286 parserOptions: { ecmaVersion: 8 },
1287 errors: [{ messageId: "unexpected", type: "Identifier" }]
1288 },
1289 {
1290 code: "({foo(a,) {}})",
1291 output: "({foo(a) {}})",
1292 options: [{ functions: "never" }],
1293 parserOptions: { ecmaVersion: 8 },
1294 errors: [{ messageId: "unexpected", type: "Identifier" }]
1295 },
1296 {
1297 code: "class A {foo(a,) {}}",
1298 output: "class A {foo(a) {}}",
1299 options: [{ functions: "never" }],
1300 parserOptions: { ecmaVersion: 8 },
1301 errors: [{ messageId: "unexpected", type: "Identifier" }]
1302 },
1303 {
1304 code: "foo(a,)",
1305 output: "foo(a)",
1306 options: [{ functions: "never" }],
1307 parserOptions: { ecmaVersion: 8 },
1308 errors: [{ messageId: "unexpected", type: "Identifier" }]
1309 },
1310 {
1311 code: "foo(...a,)",
1312 output: "foo(...a)",
1313 options: [{ functions: "never" }],
1314 parserOptions: { ecmaVersion: 8 },
1315 errors: [{ messageId: "unexpected", type: "SpreadElement" }]
1316 },
1317
1318 {
1319 code: "function foo(a) {}",
1320 output: "function foo(a,) {}",
1321 options: [{ functions: "always" }],
1322 parserOptions: { ecmaVersion: 8 },
1323 errors: [{ messageId: "missing", type: "Identifier" }]
1324 },
1325 {
1326 code: "(function foo(a) {})",
1327 output: "(function foo(a,) {})",
1328 options: [{ functions: "always" }],
1329 parserOptions: { ecmaVersion: 8 },
1330 errors: [{ messageId: "missing", type: "Identifier" }]
1331 },
1332 {
1333 code: "(a) => a",
1334 output: "(a,) => a",
1335 options: [{ functions: "always" }],
1336 parserOptions: { ecmaVersion: 8 },
1337 errors: [{ messageId: "missing", type: "Identifier" }]
1338 },
1339 {
1340 code: "(a) => (a)",
1341 output: "(a,) => (a)",
1342 options: [{ functions: "always" }],
1343 parserOptions: { ecmaVersion: 8 },
1344 errors: [{ messageId: "missing", type: "Identifier" }]
1345 },
1346 {
1347 code: "({foo(a) {}})",
1348 output: "({foo(a,) {}})",
1349 options: [{ functions: "always" }],
1350 parserOptions: { ecmaVersion: 8 },
1351 errors: [{ messageId: "missing", type: "Identifier" }]
1352 },
1353 {
1354 code: "class A {foo(a) {}}",
1355 output: "class A {foo(a,) {}}",
1356 options: [{ functions: "always" }],
1357 parserOptions: { ecmaVersion: 8 },
1358 errors: [{ messageId: "missing", type: "Identifier" }]
1359 },
1360 {
1361 code: "foo(a)",
1362 output: "foo(a,)",
1363 options: [{ functions: "always" }],
1364 parserOptions: { ecmaVersion: 8 },
1365 errors: [{ messageId: "missing", type: "Identifier" }]
1366 },
1367 {
1368 code: "foo(...a)",
1369 output: "foo(...a,)",
1370 options: [{ functions: "always" }],
1371 parserOptions: { ecmaVersion: 8 },
1372 errors: [{ messageId: "missing", type: "SpreadElement" }]
1373 },
1374
1375 {
1376 code: "function foo(a,) {}",
1377 output: "function foo(a) {}",
1378 options: [{ functions: "always-multiline" }],
1379 parserOptions: { ecmaVersion: 8 },
1380 errors: [{ messageId: "unexpected", type: "Identifier" }]
1381 },
1382 {
1383 code: "(function foo(a,) {})",
1384 output: "(function foo(a) {})",
1385 options: [{ functions: "always-multiline" }],
1386 parserOptions: { ecmaVersion: 8 },
1387 errors: [{ messageId: "unexpected", type: "Identifier" }]
1388 },
1389 {
1390 code: "foo(a,)",
1391 output: "foo(a)",
1392 options: [{ functions: "always-multiline" }],
1393 parserOptions: { ecmaVersion: 8 },
1394 errors: [{ messageId: "unexpected", type: "Identifier" }]
1395 },
1396 {
1397 code: "foo(...a,)",
1398 output: "foo(...a)",
1399 options: [{ functions: "always-multiline" }],
1400 parserOptions: { ecmaVersion: 8 },
1401 errors: [{ messageId: "unexpected", type: "SpreadElement" }]
1402 },
1403 {
1404 code: "function foo(\na,\nb\n) {}",
1405 output: "function foo(\na,\nb,\n) {}",
1406 options: [{ functions: "always-multiline" }],
1407 parserOptions: { ecmaVersion: 8 },
1408 errors: [{ messageId: "missing", type: "Identifier" }]
1409 },
1410 {
1411 code: "foo(\na,\nb\n)",
1412 output: "foo(\na,\nb,\n)",
1413 options: [{ functions: "always-multiline" }],
1414 parserOptions: { ecmaVersion: 8 },
1415 errors: [{ messageId: "missing", type: "Identifier" }]
1416 },
1417 {
1418 code: "foo(\n...a,\n...b\n)",
1419 output: "foo(\n...a,\n...b,\n)",
1420 options: [{ functions: "always-multiline" }],
1421 parserOptions: { ecmaVersion: 8 },
1422 errors: [{ messageId: "missing", type: "SpreadElement" }]
1423 },
1424
1425 {
1426 code: "function foo(a,) {}",
1427 output: "function foo(a) {}",
1428 options: [{ functions: "only-multiline" }],
1429 parserOptions: { ecmaVersion: 8 },
1430 errors: [{ messageId: "unexpected", type: "Identifier" }]
1431 },
1432 {
1433 code: "(function foo(a,) {})",
1434 output: "(function foo(a) {})",
1435 options: [{ functions: "only-multiline" }],
1436 parserOptions: { ecmaVersion: 8 },
1437 errors: [{ messageId: "unexpected", type: "Identifier" }]
1438 },
1439 {
1440 code: "foo(a,)",
1441 output: "foo(a)",
1442 options: [{ functions: "only-multiline" }],
1443 parserOptions: { ecmaVersion: 8 },
1444 errors: [{ messageId: "unexpected", type: "Identifier" }]
1445 },
1446 {
1447 code: "foo(...a,)",
1448 output: "foo(...a)",
1449 options: [{ functions: "only-multiline" }],
1450 parserOptions: { ecmaVersion: 8 },
1451 errors: [{ messageId: "unexpected", type: "SpreadElement" }]
1452 },
1453 {
1454 code: "function foo(a,) {}",
1455 output: "function foo(a) {}",
1456 options: ["never"],
1457 parserOptions: { ecmaVersion: 8 },
1458 errors: [{ messageId: "unexpected", type: "Identifier" }]
1459 },
1460 {
1461 code: "(function foo(a,) {})",
1462 output: "(function foo(a) {})",
1463 options: ["never"],
1464 parserOptions: { ecmaVersion: 8 },
1465 errors: [{ messageId: "unexpected", type: "Identifier" }]
1466 },
1467 {
1468 code: "(a,) => a",
1469 output: "(a) => a",
1470 options: ["never"],
1471 parserOptions: { ecmaVersion: 8 },
1472 errors: [{ messageId: "unexpected", type: "Identifier" }]
1473 },
1474 {
1475 code: "(a,) => (a)",
1476 output: "(a) => (a)",
1477 options: ["never"],
1478 parserOptions: { ecmaVersion: 8 },
1479 errors: [{ messageId: "unexpected", type: "Identifier" }]
1480 },
1481 {
1482 code: "({foo(a,) {}})",
1483 output: "({foo(a) {}})",
1484 options: ["never"],
1485 parserOptions: { ecmaVersion: 8 },
1486 errors: [{ messageId: "unexpected", type: "Identifier" }]
1487 },
1488 {
1489 code: "class A {foo(a,) {}}",
1490 output: "class A {foo(a) {}}",
1491 options: ["never"],
1492 parserOptions: { ecmaVersion: 8 },
1493 errors: [{ messageId: "unexpected", type: "Identifier" }]
1494 },
1495 {
1496 code: "foo(a,)",
1497 output: "foo(a)",
1498 options: ["never"],
1499 parserOptions: { ecmaVersion: 8 },
1500 errors: [{ messageId: "unexpected", type: "Identifier" }]
1501 },
1502 {
1503 code: "foo(...a,)",
1504 output: "foo(...a)",
1505 options: ["never"],
1506 parserOptions: { ecmaVersion: 8 },
1507 errors: [{ messageId: "unexpected", type: "SpreadElement" }]
1508 },
1509
1510 {
1511 code: "function foo(a) {}",
1512 output: "function foo(a,) {}",
1513 options: ["always"],
1514 parserOptions: { ecmaVersion: 8 },
1515 errors: [{ messageId: "missing", type: "Identifier" }]
1516 },
1517 {
1518 code: "(function foo(a) {})",
1519 output: "(function foo(a,) {})",
1520 options: ["always"],
1521 parserOptions: { ecmaVersion: 8 },
1522 errors: [{ messageId: "missing", type: "Identifier" }]
1523 },
1524 {
1525 code: "(a) => a",
1526 output: "(a,) => a",
1527 options: ["always"],
1528 parserOptions: { ecmaVersion: 8 },
1529 errors: [{ messageId: "missing", type: "Identifier" }]
1530 },
1531 {
1532 code: "(a) => (a)",
1533 output: "(a,) => (a)",
1534 options: ["always"],
1535 parserOptions: { ecmaVersion: 8 },
1536 errors: [{ messageId: "missing", type: "Identifier" }]
1537 },
1538 {
1539 code: "({foo(a) {}})",
1540 output: "({foo(a,) {},})",
1541 options: ["always"],
1542 parserOptions: { ecmaVersion: 8 },
1543 errors: [
1544 { messageId: "missing", type: "Identifier" },
1545 { messageId: "missing", type: "Property" }
1546 ]
1547 },
1548 {
1549 code: "class A {foo(a) {}}",
1550 output: "class A {foo(a,) {}}",
1551 options: ["always"],
1552 parserOptions: { ecmaVersion: 8 },
1553 errors: [{ messageId: "missing", type: "Identifier" }]
1554 },
1555 {
1556 code: "foo(a)",
1557 output: "foo(a,)",
1558 options: ["always"],
1559 parserOptions: { ecmaVersion: 8 },
1560 errors: [{ messageId: "missing", type: "Identifier" }]
1561 },
1562 {
1563 code: "foo(...a)",
1564 output: "foo(...a,)",
1565 options: ["always"],
1566 parserOptions: { ecmaVersion: 8 },
1567 errors: [{ messageId: "missing", type: "SpreadElement" }]
1568 },
1569
1570 {
1571 code: "function foo(a,) {}",
1572 output: "function foo(a) {}",
1573 options: ["always-multiline"],
1574 parserOptions: { ecmaVersion: 8 },
1575 errors: [{ messageId: "unexpected", type: "Identifier" }]
1576 },
1577 {
1578 code: "(function foo(a,) {})",
1579 output: "(function foo(a) {})",
1580 options: ["always-multiline"],
1581 parserOptions: { ecmaVersion: 8 },
1582 errors: [{ messageId: "unexpected", type: "Identifier" }]
1583 },
1584 {
1585 code: "foo(a,)",
1586 output: "foo(a)",
1587 options: ["always-multiline"],
1588 parserOptions: { ecmaVersion: 8 },
1589 errors: [{ messageId: "unexpected", type: "Identifier" }]
1590 },
1591 {
1592 code: "foo(...a,)",
1593 output: "foo(...a)",
1594 options: ["always-multiline"],
1595 parserOptions: { ecmaVersion: 8 },
1596 errors: [{ messageId: "unexpected", type: "SpreadElement" }]
1597 },
1598 {
1599 code: "function foo(\na,\nb\n) {}",
1600 output: "function foo(\na,\nb,\n) {}",
1601 options: ["always-multiline"],
1602 parserOptions: { ecmaVersion: 8 },
1603 errors: [{ messageId: "missing", type: "Identifier" }]
1604 },
1605 {
1606 code: "foo(\na,\nb\n)",
1607 output: "foo(\na,\nb,\n)",
1608 options: ["always-multiline"],
1609 parserOptions: { ecmaVersion: 8 },
1610 errors: [{ messageId: "missing", type: "Identifier" }]
1611 },
1612 {
1613 code: "foo(\n...a,\n...b\n)",
1614 output: "foo(\n...a,\n...b,\n)",
1615 options: ["always-multiline"],
1616 parserOptions: { ecmaVersion: 8 },
1617 errors: [{ messageId: "missing", type: "SpreadElement" }]
1618 },
1619
1620 {
1621 code: "function foo(a,) {}",
1622 output: "function foo(a) {}",
1623 options: ["only-multiline"],
1624 parserOptions: { ecmaVersion: 8 },
1625 errors: [{ messageId: "unexpected", type: "Identifier" }]
1626 },
1627 {
1628 code: "(function foo(a,) {})",
1629 output: "(function foo(a) {})",
1630 options: ["only-multiline"],
1631 parserOptions: { ecmaVersion: 8 },
1632 errors: [{ messageId: "unexpected", type: "Identifier" }]
1633 },
1634 {
1635 code: "foo(a,)",
1636 output: "foo(a)",
1637 options: ["only-multiline"],
1638 parserOptions: { ecmaVersion: 8 },
1639 errors: [{ messageId: "unexpected", type: "Identifier" }]
1640 },
1641 {
1642 code: "foo(...a,)",
1643 output: "foo(...a)",
1644 options: ["only-multiline"],
1645 parserOptions: { ecmaVersion: 8 },
1646 errors: [{ messageId: "unexpected", type: "SpreadElement" }]
1647 },
1648 {
1649 code: "function foo(a) {}",
1650 output: "function foo(a,) {}",
1651 options: ["always"],
1652 parserOptions: { ecmaVersion: 9 },
1653 errors: [{ messageId: "missing", type: "Identifier" }]
1654 },
1655
1656 // separated options
1657 {
1658 code: `let {a,} = {a: 1,};
1659 let [b,] = [1,];
1660 import {c,} from "foo";
1661 let d = 0;export {d,};
1662 (function foo(e,) {})(f,);`,
1663 output: `let {a} = {a: 1};
1664 let [b,] = [1,];
1665 import {c,} from "foo";
1666 let d = 0;export {d,};
1667 (function foo(e,) {})(f,);`,
1668 options: [{
1669 objects: "never",
1670 arrays: "ignore",
1671 imports: "ignore",
1672 exports: "ignore",
1673 functions: "ignore"
1674 }],
1675 parserOptions: { ecmaVersion: 8, sourceType: "module" },
1676 errors: [
1677 { messageId: "unexpected", line: 1 },
1678 { messageId: "unexpected", line: 1 }
1679 ]
1680 },
1681 {
1682 code: `let {a,} = {a: 1,};
1683 let [b,] = [1,];
1684 import {c,} from "foo";
1685 let d = 0;export {d,};
1686 (function foo(e,) {})(f,);`,
1687 output: `let {a,} = {a: 1,};
1688 let [b] = [1];
1689 import {c,} from "foo";
1690 let d = 0;export {d,};
1691 (function foo(e,) {})(f,);`,
1692 options: [{
1693 objects: "ignore",
1694 arrays: "never",
1695 imports: "ignore",
1696 exports: "ignore",
1697 functions: "ignore"
1698 }],
1699 parserOptions: { ecmaVersion: 8, sourceType: "module" },
1700 errors: [
1701 { messageId: "unexpected", line: 2 },
1702 { messageId: "unexpected", line: 2 }
1703 ]
1704 },
1705 {
1706 code: `let {a,} = {a: 1,};
1707 let [b,] = [1,];
1708 import {c,} from "foo";
1709 let d = 0;export {d,};
1710 (function foo(e,) {})(f,);`,
1711 output: `let {a,} = {a: 1,};
1712 let [b,] = [1,];
1713 import {c} from "foo";
1714 let d = 0;export {d,};
1715 (function foo(e,) {})(f,);`,
1716 options: [{
1717 objects: "ignore",
1718 arrays: "ignore",
1719 imports: "never",
1720 exports: "ignore",
1721 functions: "ignore"
1722 }],
1723 parserOptions: { ecmaVersion: 8, sourceType: "module" },
1724 errors: [
1725 { messageId: "unexpected", line: 3 }
1726 ]
1727 },
1728 {
1729 code: `let {a,} = {a: 1,};
1730 let [b,] = [1,];
1731 import {c,} from "foo";
1732 let d = 0;export {d,};
1733 (function foo(e,) {})(f,);`,
1734 output: `let {a,} = {a: 1,};
1735 let [b,] = [1,];
1736 import {c,} from "foo";
1737 let d = 0;export {d};
1738 (function foo(e,) {})(f,);`,
1739 options: [{
1740 objects: "ignore",
1741 arrays: "ignore",
1742 imports: "ignore",
1743 exports: "never",
1744 functions: "ignore"
1745 }],
1746 parserOptions: { ecmaVersion: 8, sourceType: "module" },
1747 errors: [
1748 { messageId: "unexpected", line: 4 }
1749 ]
1750 },
1751 {
1752 code: `let {a,} = {a: 1,};
1753 let [b,] = [1,];
1754 import {c,} from "foo";
1755 let d = 0;export {d,};
1756 (function foo(e,) {})(f,);`,
1757 output: `let {a,} = {a: 1,};
1758 let [b,] = [1,];
1759 import {c,} from "foo";
1760 let d = 0;export {d,};
1761 (function foo(e) {})(f);`,
1762 options: [{
1763 objects: "ignore",
1764 arrays: "ignore",
1765 imports: "ignore",
1766 exports: "ignore",
1767 functions: "never"
1768 }],
1769 parserOptions: { ecmaVersion: 8, sourceType: "module" },
1770 errors: [
1771 { messageId: "unexpected", line: 5 },
1772 { messageId: "unexpected", line: 5 }
1773 ]
1774 },
1775
1776 // https://github.com/eslint/eslint/issues/7370
1777 {
1778 code: "function foo({a}: {a: string,}) {}",
1779 output: "function foo({a,}: {a: string,}) {}",
1780 options: ["always"],
1781 parser: parser("object-pattern-1"),
1782 errors: [{ messageId: "missing" }]
1783 },
1784 {
1785 code: "function foo({a,}: {a: string}) {}",
1786 output: "function foo({a}: {a: string}) {}",
1787 options: ["never"],
1788 parser: parser("object-pattern-2"),
1789 errors: [{ messageId: "unexpected" }]
1790 },
1791 {
1792 code: "function foo(a): {b: boolean,} {}",
1793 output: "function foo(a,): {b: boolean,} {}",
1794 options: [{ functions: "always" }],
1795 parser: parser("return-type-1"),
1796 errors: [{ messageId: "missing" }]
1797 },
1798 {
1799 code: "function foo(a,): {b: boolean} {}",
1800 output: "function foo(a): {b: boolean} {}",
1801 options: [{ functions: "never" }],
1802 parser: parser("return-type-2"),
1803 errors: [{ messageId: "unexpected" }]
1804 },
1805
1806 // https://github.com/eslint/eslint/issues/11502
1807 {
1808 code: "foo(a,)",
1809 output: "foo(a)",
1810 parserOptions: { ecmaVersion: 8 },
1811 errors: [{ messageId: "unexpected" }]
1812 },
1813
1814 // https://github.com/eslint/eslint/issues/15660
1815 {
1816 code: unIndent`
1817 /*eslint add-named-import:1*/
1818 import {
1819 StyleSheet,
1820 View,
1821 TextInput,
1822 ImageBackground,
1823 Image,
1824 TouchableOpacity,
1825 SafeAreaView
1826 } from 'react-native';
1827 `,
1828 output: unIndent`
1829 /*eslint add-named-import:1*/
1830 import {
1831 StyleSheet,
1832 View,
1833 TextInput,
1834 ImageBackground,
1835 Image,
1836 TouchableOpacity,
1837 SafeAreaView,
1838 } from 'react-native';
1839 `,
1840 options: [{ imports: "always-multiline" }],
1841 parserOptions: { ecmaVersion: 6, sourceType: "module" },
1842 errors: 2
1843 },
1844 {
1845 code: unIndent`
1846 /*eslint add-named-import:1*/
1847 import {
1848 StyleSheet,
1849 View,
1850 TextInput,
1851 ImageBackground,
1852 Image,
1853 TouchableOpacity,
1854 SafeAreaView,
1855 } from 'react-native';
1856 `,
1857 output: unIndent`
1858 /*eslint add-named-import:1*/
1859 import {
1860 StyleSheet,
1861 View,
1862 TextInput,
1863 ImageBackground,
1864 Image,
1865 TouchableOpacity,
1866 SafeAreaView
1867 } from 'react-native';
1868 `,
1869 options: [{ imports: "never" }],
1870 parserOptions: { ecmaVersion: 6, sourceType: "module" },
1871 errors: 2
1872 }
1873 ]
1874 });
1875
1876 const flatRuleTester = new FlatRuleTester();
1877
1878 // https://github.com/eslint/eslint/issues/16442
1879 flatRuleTester.run("comma-dangle", rule, {
1880 valid: [
1881 {
1882 code: "function f(\n a,\n b\n) {}",
1883 options: ["always-multiline"],
1884 languageOptions: {
1885 ecmaVersion: 5,
1886 sourceType: "script"
1887 }
1888 },
1889 {
1890 code: "f(\n a,\n b\n);",
1891 options: ["always-multiline"],
1892 languageOptions: {
1893 ecmaVersion: 5,
1894 sourceType: "script"
1895 }
1896 },
1897 {
1898 code: "function f(\n a,\n b\n) {}",
1899 options: ["always-multiline"],
1900 languageOptions: {
1901 ecmaVersion: 2016
1902 }
1903 },
1904 {
1905 code: "f(\n a,\n b\n);",
1906 options: ["always-multiline"],
1907 languageOptions: {
1908 ecmaVersion: 2016
1909 }
1910 }
1911 ],
1912
1913 invalid: [
1914 {
1915 code: "function f(\n a,\n b\n) {}",
1916 output: "function f(\n a,\n b,\n) {}",
1917 options: ["always-multiline"],
1918 languageOptions: {
1919 ecmaVersion: 2017
1920 },
1921 errors: [{
1922 messageId: "missing",
1923 type: "Identifier",
1924 line: 3,
1925 column: 3
1926 }]
1927 },
1928 {
1929 code: "f(\n a,\n b\n);",
1930 output: "f(\n a,\n b,\n);",
1931 options: ["always-multiline"],
1932 languageOptions: {
1933 ecmaVersion: 2017
1934 },
1935 errors: [{
1936 messageId: "missing",
1937 type: "Identifier",
1938 line: 3,
1939 column: 3
1940 }]
1941 },
1942 {
1943 code: "function f(\n a,\n b\n) {}",
1944 output: "function f(\n a,\n b,\n) {}",
1945 options: ["always-multiline"],
1946 languageOptions: {
1947 ecmaVersion: "latest"
1948 },
1949 errors: [{
1950 messageId: "missing",
1951 type: "Identifier",
1952 line: 3,
1953 column: 3
1954 }]
1955 },
1956 {
1957 code: "f(\n a,\n b\n);",
1958 output: "f(\n a,\n b,\n);",
1959 options: ["always-multiline"],
1960 languageOptions: {
1961 ecmaVersion: "latest"
1962 },
1963 errors: [{
1964 messageId: "missing",
1965 type: "Identifier",
1966 line: 3,
1967 column: 3
1968 }]
1969 }
1970 ]
1971 });