]> git.proxmox.com Git - pve-eslint.git/blame - eslint/tests/lib/rules/func-names.js
import 8.3.0 source
[pve-eslint.git] / eslint / tests / lib / rules / func-names.js
CommitLineData
eb39fafa
DC
1/**
2 * @fileoverview Tests for func-names rule.
3 * @author Kyle T. Nunery
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
12const rule = require("../../../lib/rules/func-names"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15//------------------------------------------------------------------------------
16// Tests
17//------------------------------------------------------------------------------
18
19const ruleTester = new RuleTester();
20
21ruleTester.run("func-names", rule, {
22 valid: [
23 "Foo.prototype.bar = function bar(){};",
24 { code: "Foo.prototype.bar = () => {}", parserOptions: { ecmaVersion: 6 } },
25 "function foo(){}",
26 "function test(d, e, f) {}",
27 "new function bar(){}",
28 "exports = { get foo() { return 1; }, set bar(val) { return val; } };",
29 {
30 code: "({ foo() { return 1; } });",
31 parserOptions: { ecmaVersion: 6 }
32 },
33 {
34 code: "class A { constructor(){} foo(){} get bar(){} set baz(value){} static qux(){}}",
35 parserOptions: { ecmaVersion: 6 }
36 },
37 {
38 code: "function foo() {}",
39 options: ["always"]
40 },
41 {
42 code: "var a = function foo() {};",
43 options: ["always"]
44 },
45 {
46 code: "class A { constructor(){} foo(){} get bar(){} set baz(value){} static qux(){}}",
47 options: ["as-needed"],
48 parserOptions: { ecmaVersion: 6 }
49 },
50 {
51 code: "({ foo() {} });",
52 options: ["as-needed"],
53 parserOptions: { ecmaVersion: 6 }
54 },
55 {
56 code: "var foo = function(){};",
57 options: ["as-needed"]
58 },
59 {
60 code: "({foo: function(){}});",
61 options: ["as-needed"]
62 },
63 {
64 code: "(foo = function(){});",
65 options: ["as-needed"]
66 },
67 {
68 code: "({foo = function(){}} = {});",
69 options: ["as-needed"],
70 parserOptions: { ecmaVersion: 6 }
71 },
72 {
73 code: "({key: foo = function(){}} = {});",
74 options: ["as-needed"],
75 parserOptions: { ecmaVersion: 6 }
76 },
77 {
78 code: "[foo = function(){}] = [];",
79 options: ["as-needed"],
80 parserOptions: { ecmaVersion: 6 }
81 },
82 {
83 code: "function fn(foo = function(){}) {}",
84 options: ["as-needed"],
85 parserOptions: { ecmaVersion: 6 }
86 },
87 {
88 code: "function foo() {}",
89 options: ["never"]
90 },
91 {
92 code: "var a = function() {};",
93 options: ["never"]
94 },
95 {
96 code: "var a = function foo() { foo(); };",
97 options: ["never"]
98 },
99 {
100 code: "var foo = {bar: function() {}};",
101 options: ["never"]
102 },
103 {
104 code: "$('#foo').click(function() {});",
105 options: ["never"]
106 },
107 {
108 code: "Foo.prototype.bar = function() {};",
109 options: ["never"]
110 },
111 {
112 code: "class A { constructor(){} foo(){} get bar(){} set baz(value){} static qux(){}}",
113 options: ["never"],
114 parserOptions: { ecmaVersion: 6 }
115 },
116 {
117 code: "({ foo() {} });",
118 options: ["never"],
119 parserOptions: { ecmaVersion: 6 }
120 },
121
122 // export default
123 {
124 code: "export default function foo() {}",
125 options: ["always"],
126 parserOptions: { sourceType: "module", ecmaVersion: 6 }
127 },
128 {
129 code: "export default function foo() {}",
130 options: ["as-needed"],
131 parserOptions: { sourceType: "module", ecmaVersion: 6 }
132 },
133 {
134 code: "export default function foo() {}",
135 options: ["never"],
136 parserOptions: { sourceType: "module", ecmaVersion: 6 }
137 },
138 {
139 code: "export default function() {}",
140 options: ["never"],
141 parserOptions: { sourceType: "module", ecmaVersion: 6 }
142 },
143
144 // generators
145 {
146 code: "var foo = bar(function *baz() {});",
147 options: ["always"],
148 parserOptions: { ecmaVersion: 6 }
149 },
150 {
151 code: "var foo = bar(function *baz() {});",
152 options: ["always", { generators: "always" }],
153 parserOptions: { ecmaVersion: 6 }
154 },
155 {
156 code: "var foo = bar(function *baz() {});",
157 options: ["always", { generators: "as-needed" }],
158 parserOptions: { ecmaVersion: 6 }
159 },
160 {
161 code: "var foo = function*() {};",
162 options: ["always", { generators: "as-needed" }],
163 parserOptions: { ecmaVersion: 6 }
164 },
165 {
166 code: "var foo = bar(function *baz() {});",
167 options: ["as-needed"],
168 parserOptions: { ecmaVersion: 6 }
169 },
170 {
171 code: "var foo = function*() {};",
172 options: ["as-needed"],
173 parserOptions: { ecmaVersion: 6 }
174 },
175 {
176 code: "var foo = bar(function *baz() {});",
177 options: ["as-needed", { generators: "always" }],
178 parserOptions: { ecmaVersion: 6 }
179 },
180 {
181 code: "var foo = bar(function *baz() {});",
182 options: ["as-needed", { generators: "as-needed" }],
183 parserOptions: { ecmaVersion: 6 }
184 },
185 {
186 code: "var foo = function*() {};",
187 options: ["as-needed", { generators: "as-needed" }],
188 parserOptions: { ecmaVersion: 6 }
189 },
190 {
191 code: "var foo = bar(function *baz() {});",
192 options: ["never", { generators: "always" }],
193 parserOptions: { ecmaVersion: 6 }
194 },
195 {
196 code: "var foo = bar(function *baz() {});",
197 options: ["never", { generators: "as-needed" }],
198 parserOptions: { ecmaVersion: 6 }
199 },
200 {
201 code: "var foo = function*() {};",
202 options: ["never", { generators: "as-needed" }],
203 parserOptions: { ecmaVersion: 6 }
204 },
205
206 {
207 code: "var foo = bar(function *() {});",
208 options: ["never"],
209 parserOptions: { ecmaVersion: 6 }
210 },
211 {
212 code: "var foo = function*() {};",
213 options: ["never"],
214 parserOptions: { ecmaVersion: 6 }
215 },
216 {
217 code: "(function*() {}())",
218 options: ["never"],
219 parserOptions: { ecmaVersion: 6 }
220 },
221 {
222 code: "var foo = bar(function *() {});",
223 options: ["never", { generators: "never" }],
224 parserOptions: { ecmaVersion: 6 }
225 },
226 {
227 code: "var foo = function*() {};",
228 options: ["never", { generators: "never" }],
229 parserOptions: { ecmaVersion: 6 }
230 },
231 {
232 code: "(function*() {}())",
233 options: ["never", { generators: "never" }],
234 parserOptions: { ecmaVersion: 6 }
235 },
236 {
237 code: "var foo = bar(function *() {});",
238 options: ["always", { generators: "never" }],
239 parserOptions: { ecmaVersion: 6 }
240 },
241 {
242 code: "var foo = function*() {};",
243 options: ["always", { generators: "never" }],
244 parserOptions: { ecmaVersion: 6 }
245 },
246 {
247 code: "(function*() {}())",
248 options: ["always", { generators: "never" }],
249 parserOptions: { ecmaVersion: 6 }
250 },
251 {
252 code: "var foo = bar(function *() {});",
253 options: ["as-needed", { generators: "never" }],
254 parserOptions: { ecmaVersion: 6 }
255 },
256 {
257 code: "var foo = function*() {};",
258 options: ["as-needed", { generators: "never" }],
259 parserOptions: { ecmaVersion: 6 }
260 },
261 {
262 code: "(function*() {}())",
263 options: ["as-needed", { generators: "never" }],
264 parserOptions: { ecmaVersion: 6 }
609c276f
TL
265 },
266
267 // class fields
268 {
269 code: "class C { foo = function() {}; }",
270 options: ["as-needed"],
271 parserOptions: { ecmaVersion: 2022 }
272 },
273 {
274 code: "class C { [foo] = function() {}; }",
275 options: ["as-needed"],
276 parserOptions: { ecmaVersion: 2022 }
277 },
278 {
279 code: "class C { #foo = function() {}; }",
280 options: ["as-needed"],
281 parserOptions: { ecmaVersion: 2022 }
eb39fafa
DC
282 }
283 ],
284 invalid: [
285 {
286 code: "Foo.prototype.bar = function() {};",
287 errors: [{
288 messageId: "unnamed",
289 type: "FunctionExpression",
290 line: 1,
291 column: 21,
292 endColumn: 29
293 }]
294 },
295 {
296 code: "(function(){}())",
297 errors: [{
298 messageId: "unnamed",
299 type: "FunctionExpression",
300 line: 1,
301 column: 2,
302 endColumn: 10
303 }]
304 },
305 {
306 code: "f(function(){})",
307 errors: [{
308 messageId: "unnamed",
309 type: "FunctionExpression",
310 line: 1,
311 column: 3,
312 endColumn: 11
313 }]
314 },
315 {
316 code: "var a = new Date(function() {});",
317 errors: [{
318 messageId: "unnamed",
319 type: "FunctionExpression",
320 line: 1,
321 column: 18,
322 endColumn: 26
323 }]
324 },
325 {
326 code: "var test = function(d, e, f) {};",
327 errors: [{
328 messageId: "unnamed",
329 type: "FunctionExpression",
330 line: 1,
331 column: 12,
332 endColumn: 20
333 }]
334 },
335 {
336 code: "new function() {}",
337 errors: [{
338 messageId: "unnamed",
339 type: "FunctionExpression",
340 line: 1,
341 column: 5,
342 endColumn: 13
343 }]
344 },
345 {
346 code: "Foo.prototype.bar = function() {};",
347 options: ["as-needed"],
348 errors: [{
349 messageId: "unnamed",
350 type: "FunctionExpression",
351 line: 1,
352 column: 21,
353 endColumn: 29
354 }]
355 },
356 {
357 code: "(function(){}())",
358 options: ["as-needed"],
359 errors: [{
360 messageId: "unnamed",
361 type: "FunctionExpression",
362 line: 1,
363 column: 2,
364 endColumn: 10
365 }]
366 },
367 {
368 code: "f(function(){})",
369 options: ["as-needed"],
370 errors: [{
371 messageId: "unnamed",
372 type: "FunctionExpression",
373 line: 1,
374 column: 3,
375 endColumn: 11
376 }]
377 },
378 {
379 code: "var a = new Date(function() {});",
380 options: ["as-needed"],
381 errors: [{
382 messageId: "unnamed",
383 type: "FunctionExpression",
384 line: 1,
385 column: 18,
386 endColumn: 26
387 }]
388 },
389 {
390 code: "new function() {}",
391 options: ["as-needed"],
392 errors: [{
393 messageId: "unnamed",
394 type: "FunctionExpression",
395 line: 1,
396 column: 5,
397 endColumn: 13
398 }]
399 },
400 {
401 code: "var {foo} = function(){};",
402 options: ["as-needed"],
403 parserOptions: { ecmaVersion: 6 },
404 errors: [{
405 messageId: "unnamed",
406 type: "FunctionExpression",
407 line: 1,
408 column: 13,
409 endColumn: 21
410 }]
411 },
412 {
413 code: "({ a: obj.prop = function(){} } = foo);",
414 options: ["as-needed"],
415 parserOptions: { ecmaVersion: 6 },
416 errors: [{
417 messageId: "unnamed",
418 type: "FunctionExpression",
419 line: 1,
420 column: 18,
421 endColumn: 26
422 }]
423 },
424 {
425 code: "[obj.prop = function(){}] = foo;",
426 options: ["as-needed"],
427 parserOptions: { ecmaVersion: 6 },
428 errors: [{
429 messageId: "unnamed",
430 type: "FunctionExpression",
431 line: 1,
432 column: 13,
433 endColumn: 21
434 }]
435 },
436 {
437 code: "var { a: [b] = function(){} } = foo;",
438 options: ["as-needed"],
439 parserOptions: { ecmaVersion: 6 },
440 errors: [{
441 messageId: "unnamed",
442 type: "FunctionExpression",
443 line: 1,
444 column: 16,
445 endColumn: 24
446 }]
447 },
448 {
449 code: "function foo({ a } = function(){}) {};",
450 options: ["as-needed"],
451 parserOptions: { ecmaVersion: 6 },
452 errors: [{
453 messageId: "unnamed",
454 type: "FunctionExpression",
455 line: 1,
456 column: 22,
457 endColumn: 30
458 }]
459 },
460 {
461 code: "var x = function foo() {};",
462 options: ["never"],
463 errors: [{
464 messageId: "named",
465 data: { name: "function 'foo'" },
466 type: "FunctionExpression",
467 line: 1,
468 column: 9,
469 endColumn: 21
470 }]
471 },
472 {
473 code: "Foo.prototype.bar = function foo() {};",
474 options: ["never"],
475 errors: [{
476 messageId: "named",
477 data: { name: "function 'foo'" },
478 type: "FunctionExpression",
479 line: 1,
480 column: 21,
481 endColumn: 33
482 }]
483 },
484 {
485 code: "({foo: function foo() {}})",
486 options: ["never"],
487 errors: [{
488 messageId: "named",
489 data: { name: "method 'foo'" },
490 type: "FunctionExpression",
491 line: 1,
492 column: 3,
493 endColumn: 20
494 }]
495 },
496
497 // export default
498 {
499 code: "export default function() {}",
500 options: ["always"],
501 parserOptions: { sourceType: "module", ecmaVersion: 6 },
502 errors: [{
503 messageId: "unnamed",
504 type: "FunctionDeclaration",
505 column: 16,
506 endColumn: 24
507 }]
508 },
509 {
510 code: "export default function() {}",
511 options: ["as-needed"],
512 parserOptions: { sourceType: "module", ecmaVersion: 6 },
513 errors: [{
514 messageId: "unnamed",
515 type: "FunctionDeclaration",
516 column: 16,
517 endColumn: 24
518 }]
519 },
520 {
521 code: "export default (function(){});",
522 options: ["as-needed"],
523 parserOptions: { sourceType: "module", ecmaVersion: 6 },
524 errors: [{
525 messageId: "unnamed",
526 type: "FunctionExpression",
527 column: 17,
528 endColumn: 25
529 }]
530 },
531
532 // generators
533 {
534 code: "var foo = bar(function *() {});",
535 options: ["always"],
536 parserOptions: { ecmaVersion: 6 },
537 errors: [{
538 messageId: "unnamed",
539 type: "FunctionExpression",
540 line: 1,
541 column: 15,
542 endColumn: 25
543 }]
544 },
545 {
546 code: "var foo = function*() {};",
547 options: ["always"],
548 parserOptions: { ecmaVersion: 6 },
549 errors: [{
550 messageId: "unnamed",
551 type: "FunctionExpression",
552 line: 1,
553 column: 11,
554 endColumn: 20
555 }]
556 },
557 {
558 code: "(function*() {}())",
559 options: ["always"],
560 parserOptions: { ecmaVersion: 6 },
561 errors: [{
562 messageId: "unnamed",
563 type: "FunctionExpression",
564 line: 1,
565 column: 2,
566 endColumn: 11
567 }]
568 },
569 {
570 code: "var foo = bar(function *() {});",
571 options: ["always", { generators: "always" }],
572 parserOptions: { ecmaVersion: 6 },
573 errors: [{
574 messageId: "unnamed",
575 type: "FunctionExpression",
576 line: 1,
577 column: 15,
578 endColumn: 25
579 }]
580 },
581 {
582 code: "var foo = function*() {};",
583 options: ["always", { generators: "always" }],
584 parserOptions: { ecmaVersion: 6 },
585 errors: [{
586 messageId: "unnamed",
587 type: "FunctionExpression",
588 line: 1,
589 column: 11,
590 endColumn: 20
591 }]
592 },
593 {
594 code: "(function*() {}())",
595 options: ["always", { generators: "always" }],
596 parserOptions: { ecmaVersion: 6 },
597 errors: [{
598 messageId: "unnamed",
599 type: "FunctionExpression",
600 line: 1,
601 column: 2,
602 endColumn: 11
603 }]
604 },
605 {
606 code: "var foo = bar(function *() {});",
607 options: ["always", { generators: "as-needed" }],
608 parserOptions: { ecmaVersion: 6 },
609 errors: [{
610 messageId: "unnamed",
611 type: "FunctionExpression",
612 line: 1,
613 column: 15,
614 endColumn: 25
615 }]
616 },
617 {
618 code: "(function*() {}())",
619 options: ["always", { generators: "as-needed" }],
620 parserOptions: { ecmaVersion: 6 },
621 errors: [{
622 messageId: "unnamed",
623 type: "FunctionExpression",
624 line: 1,
625 column: 2,
626 endColumn: 11
627 }]
628 },
629 {
630 code: "var foo = bar(function *() {});",
631 options: ["as-needed"],
632 parserOptions: { ecmaVersion: 6 },
633 errors: [{
634 messageId: "unnamed",
635 type: "FunctionExpression",
636 line: 1,
637 column: 15,
638 endColumn: 25
639 }]
640 },
641 {
642 code: "(function*() {}())",
643 options: ["as-needed"],
644 parserOptions: { ecmaVersion: 6 },
645 errors: [{
646 messageId: "unnamed",
647 type: "FunctionExpression",
648 line: 1,
649 column: 2,
650 endColumn: 11
651 }]
652 },
653 {
654 code: "var foo = bar(function *() {});",
655 options: ["as-needed", { generators: "always" }],
656 parserOptions: { ecmaVersion: 6 },
657 errors: [{
658 messageId: "unnamed",
659 type: "FunctionExpression",
660 line: 1,
661 column: 15,
662 endColumn: 25
663 }]
664 },
665 {
666 code: "var foo = function*() {};",
667 options: ["as-needed", { generators: "always" }],
668 parserOptions: { ecmaVersion: 6 },
669 errors: [{
670 messageId: "unnamed",
671 type: "FunctionExpression",
672 line: 1,
673 column: 11,
674 endColumn: 20
675 }]
676 },
677 {
678 code: "(function*() {}())",
679 options: ["as-needed", { generators: "always" }],
680 parserOptions: { ecmaVersion: 6 },
681 errors: [{
682 messageId: "unnamed",
683 type: "FunctionExpression",
684 line: 1,
685 column: 2,
686 endColumn: 11
687 }]
688 },
689 {
690 code: "var foo = bar(function *() {});",
691 options: ["as-needed", { generators: "as-needed" }],
692 parserOptions: { ecmaVersion: 6 },
693 errors: [{
694 messageId: "unnamed",
695 type: "FunctionExpression",
696 line: 1,
697 column: 15,
698 endColumn: 25
699 }]
700 },
701 {
702 code: "(function*() {}())",
703 options: ["as-needed", { generators: "as-needed" }],
704 parserOptions: { ecmaVersion: 6 },
705 errors: [{
706 messageId: "unnamed",
707 type: "FunctionExpression",
708 line: 1,
709 column: 2,
710 endColumn: 11
711 }]
712 },
713 {
714 code: "var foo = bar(function *() {});",
715 options: ["never", { generators: "always" }],
716 parserOptions: { ecmaVersion: 6 },
717 errors: [{
718 messageId: "unnamed",
719 type: "FunctionExpression",
720 line: 1,
721 column: 15,
722 endColumn: 25
723 }]
724 },
725 {
726 code: "var foo = function*() {};",
727 options: ["never", { generators: "always" }],
728 parserOptions: { ecmaVersion: 6 },
729 errors: [{
730 messageId: "unnamed",
731 type: "FunctionExpression",
732 line: 1,
733 column: 11,
734 endColumn: 20
735 }]
736 },
737 {
738 code: "(function*() {}())",
739 options: ["never", { generators: "always" }],
740 parserOptions: { ecmaVersion: 6 },
741 errors: [{
742 messageId: "unnamed",
743 type: "FunctionExpression",
744 line: 1,
745 column: 2,
746 endColumn: 11
747 }]
748 },
749 {
750 code: "var foo = bar(function *() {});",
751 options: ["never", { generators: "as-needed" }],
752 parserOptions: { ecmaVersion: 6 },
753 errors: [{
754 messageId: "unnamed",
755 type: "FunctionExpression",
756 line: 1,
757 column: 15,
758 endColumn: 25
759 }]
760 },
761 {
762 code: "(function*() {}())",
763 options: ["never", { generators: "as-needed" }],
764 parserOptions: { ecmaVersion: 6 },
765 errors: [{
766 messageId: "unnamed",
767 type: "FunctionExpression",
768 line: 1,
769 column: 2,
770 endColumn: 11
771 }]
772 },
773
774 {
775 code: "var foo = bar(function *baz() {});",
776 options: ["never"],
777 parserOptions: { ecmaVersion: 6 },
778 errors: [{
779 messageId: "named",
780 data: { name: "generator function 'baz'" },
781 type: "FunctionExpression",
782 line: 1,
783 column: 15,
784 endColumn: 28
785 }]
786 },
787 {
788 code: "var foo = bar(function *baz() {});",
789 options: ["never", { generators: "never" }],
790 parserOptions: { ecmaVersion: 6 },
791 errors: [{
792 messageId: "named",
793 data: { name: "generator function 'baz'" },
794 type: "FunctionExpression",
795 line: 1,
796 column: 15,
797 endColumn: 28
798 }]
799 },
800 {
801 code: "var foo = bar(function *baz() {});",
802 options: ["always", { generators: "never" }],
803 parserOptions: { ecmaVersion: 6 },
804 errors: [{
805 messageId: "named",
806 data: { name: "generator function 'baz'" },
807 type: "FunctionExpression",
808 line: 1,
809 column: 15,
810 endColumn: 28
811 }]
812 },
813 {
814 code: "var foo = bar(function *baz() {});",
815 options: ["as-needed", { generators: "never" }],
816 parserOptions: { ecmaVersion: 6 },
817 errors: [{
818 messageId: "named",
819 data: { name: "generator function 'baz'" },
820 type: "FunctionExpression",
821 line: 1,
822 column: 15,
823 endColumn: 28
824 }]
609c276f
TL
825 },
826
827 // class fields
828 {
829 code: "class C { foo = function() {} }",
830 options: ["always"],
831 parserOptions: { ecmaVersion: 2022 },
832 errors: [{
833 messageId: "unnamed",
834 data: { name: "method 'foo'" },
835 column: 11,
836 endColumn: 25
837 }]
838 },
839 {
840 code: "class C { [foo] = function() {} }",
841 options: ["always"],
842 parserOptions: { ecmaVersion: 2022 },
843 errors: [{
844 messageId: "unnamed",
845 data: { name: "method" },
846 column: 11,
847 endColumn: 27
848 }]
849 },
850 {
851 code: "class C { #foo = function() {} }",
852 options: ["always"],
853 parserOptions: { ecmaVersion: 2022 },
854 errors: [{
855 messageId: "unnamed",
856 data: { name: "private method #foo" },
857 column: 11,
858 endColumn: 26
859 }]
860 },
861 {
862 code: "class C { foo = bar(function() {}) }",
863 options: ["as-needed"],
864 parserOptions: { ecmaVersion: 2022 },
865 errors: [{
866 messageId: "unnamed",
867 data: { name: "function" },
868 column: 21,
869 endColumn: 29
870 }]
871 },
872 {
873 code: "class C { foo = function bar() {} }",
874 options: ["never"],
875 parserOptions: { ecmaVersion: 2022 },
876 errors: [{
877 messageId: "named",
878 data: { name: "method 'foo'" },
879 column: 11,
880 endColumn: 29
881 }]
eb39fafa
DC
882 }
883 ]
884});