]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/camelcase.js
bump version to 8.41.0-3
[pve-eslint.git] / eslint / tests / lib / rules / camelcase.js
1 /**
2 * @fileoverview Tests for camelcase rule.
3 * @author Nicholas C. Zakas
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/camelcase"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15 //------------------------------------------------------------------------------
16 // Tests
17 //------------------------------------------------------------------------------
18
19 const ruleTester = new RuleTester();
20
21 ruleTester.run("camelcase", rule, {
22 valid: [
23 "firstName = \"Nicholas\"",
24 "FIRST_NAME = \"Nicholas\"",
25 "__myPrivateVariable = \"Patrick\"",
26 "myPrivateVariable_ = \"Patrick\"",
27 "function doSomething(){}",
28 "do_something()",
29 "new do_something",
30 "new do_something()",
31 "foo.do_something()",
32 "var foo = bar.baz_boom;",
33 "var foo = bar.baz_boom.something;",
34 "foo.boom_pow.qux = bar.baz_boom.something;",
35 "if (bar.baz_boom) {}",
36 "var obj = { key: foo.bar_baz };",
37 "var arr = [foo.bar_baz];",
38 "[foo.bar_baz]",
39 "var arr = [foo.bar_baz.qux];",
40 "[foo.bar_baz.nesting]",
41 "if (foo.bar_baz === boom.bam_pow) { [foo.baz_boom] }",
42 {
43 code: "var o = {key: 1}",
44 options: [{ properties: "always" }]
45 },
46 {
47 code: "var o = {_leading: 1}",
48 options: [{ properties: "always" }]
49 },
50 {
51 code: "var o = {trailing_: 1}",
52 options: [{ properties: "always" }]
53 },
54 {
55 code: "var o = {bar_baz: 1}",
56 options: [{ properties: "never" }]
57 },
58 {
59 code: "var o = {_leading: 1}",
60 options: [{ properties: "never" }]
61 },
62 {
63 code: "var o = {trailing_: 1}",
64 options: [{ properties: "never" }]
65 },
66 {
67 code: "obj.a_b = 2;",
68 options: [{ properties: "never" }]
69 },
70 {
71 code: "obj._a = 2;",
72 options: [{ properties: "always" }]
73 },
74 {
75 code: "obj.a_ = 2;",
76 options: [{ properties: "always" }]
77 },
78 {
79 code: "obj._a = 2;",
80 options: [{ properties: "never" }]
81 },
82 {
83 code: "obj.a_ = 2;",
84 options: [{ properties: "never" }]
85 },
86 {
87 code: "var obj = {\n a_a: 1 \n};\n obj.a_b = 2;",
88 options: [{ properties: "never" }]
89 },
90 {
91 code: "obj.foo_bar = function(){};",
92 options: [{ properties: "never" }]
93 },
94 {
95 code: "const { ['foo']: _foo } = obj;",
96 parserOptions: { ecmaVersion: 6 }
97 },
98 {
99 code: "const { [_foo_]: foo } = obj;",
100 parserOptions: { ecmaVersion: 6 }
101 },
102 {
103 code: "var { category_id } = query;",
104 options: [{ ignoreDestructuring: true }],
105 parserOptions: { ecmaVersion: 6 }
106 },
107 {
108 code: "var { category_id: category_id } = query;",
109 options: [{ ignoreDestructuring: true }],
110 parserOptions: { ecmaVersion: 6 }
111 },
112 {
113 code: "var { category_id = 1 } = query;",
114 options: [{ ignoreDestructuring: true }],
115 parserOptions: { ecmaVersion: 6 }
116 },
117 {
118 code: "var { [{category_id} = query]: categoryId } = query;",
119 options: [{ ignoreDestructuring: true }],
120 parserOptions: { ecmaVersion: 6 }
121 },
122 {
123 code: "var { category_id: category } = query;",
124 parserOptions: { ecmaVersion: 6 }
125 },
126 {
127 code: "var { _leading } = query;",
128 parserOptions: { ecmaVersion: 6 }
129 },
130 {
131 code: "var { trailing_ } = query;",
132 parserOptions: { ecmaVersion: 6 }
133 },
134 {
135 code: "import { camelCased } from \"external module\";",
136 parserOptions: { ecmaVersion: 6, sourceType: "module" }
137 },
138 {
139 code: "import { _leading } from \"external module\";",
140 parserOptions: { ecmaVersion: 6, sourceType: "module" }
141 },
142 {
143 code: "import { trailing_ } from \"external module\";",
144 parserOptions: { ecmaVersion: 6, sourceType: "module" }
145 },
146 {
147 code: "import { no_camelcased as camelCased } from \"external-module\";",
148 parserOptions: { ecmaVersion: 6, sourceType: "module" }
149 },
150 {
151 code: "import { no_camelcased as _leading } from \"external-module\";",
152 parserOptions: { ecmaVersion: 6, sourceType: "module" }
153 },
154 {
155 code: "import { no_camelcased as trailing_ } from \"external-module\";",
156 parserOptions: { ecmaVersion: 6, sourceType: "module" }
157 },
158 {
159 code: "import { no_camelcased as camelCased, anotherCamelCased } from \"external-module\";",
160 parserOptions: { ecmaVersion: 6, sourceType: "module" }
161 },
162 {
163 code: "import { snake_cased } from 'mod'",
164 options: [{ ignoreImports: true }],
165 parserOptions: { ecmaVersion: 6, sourceType: "module" }
166 },
167 {
168 code: "import { snake_cased as snake_cased } from 'mod'",
169 options: [{ ignoreImports: true }],
170 parserOptions: { ecmaVersion: 2022, sourceType: "module" }
171 },
172 {
173 code: "import { 'snake_cased' as snake_cased } from 'mod'",
174 options: [{ ignoreImports: true }],
175 parserOptions: { ecmaVersion: 2022, sourceType: "module" }
176 },
177 {
178 code: "import { camelCased } from 'mod'",
179 options: [{ ignoreImports: false }],
180 parserOptions: { ecmaVersion: 6, sourceType: "module" }
181 },
182
183 // this rule doesn't apply to quoted module export names, as it doesn't apply to quoted property names.
184 {
185 code: "export { a as 'snake_cased' } from 'mod'",
186 parserOptions: { ecmaVersion: 2022, sourceType: "module" }
187 },
188 {
189 code: "export * as 'snake_cased' from 'mod'",
190 parserOptions: { ecmaVersion: 2022, sourceType: "module" }
191 },
192
193 {
194 code: "var _camelCased = aGlobalVariable",
195 options: [{ ignoreGlobals: false }],
196 globals: { aGlobalVariable: "readonly" }
197 },
198 {
199 code: "var camelCased = _aGlobalVariable",
200 options: [{ ignoreGlobals: false }],
201 globals: { _aGlobalVariable: "readonly" }
202 },
203 {
204 code: "var camelCased = a_global_variable",
205 options: [{ ignoreGlobals: true }],
206 globals: { a_global_variable: "readonly" } // eslint-disable-line camelcase -- Testing non-CamelCase
207 },
208 {
209 code: "a_global_variable.foo()",
210 options: [{ ignoreGlobals: true }],
211 globals: { a_global_variable: "readonly" } // eslint-disable-line camelcase -- Testing non-CamelCase
212 },
213 {
214 code: "a_global_variable[undefined]",
215 options: [{ ignoreGlobals: true }],
216 globals: { a_global_variable: "readonly" } // eslint-disable-line camelcase -- Testing non-CamelCase
217 },
218 {
219 code: "var foo = a_global_variable.bar",
220 options: [{ ignoreGlobals: true }],
221 globals: { a_global_variable: "readonly" } // eslint-disable-line camelcase -- Testing non-CamelCase
222 },
223 {
224 code: "a_global_variable.foo = bar",
225 options: [{ ignoreGlobals: true }],
226 globals: { a_global_variable: "readonly" } // eslint-disable-line camelcase -- Testing non-CamelCase
227 },
228 {
229 code: "( { foo: a_global_variable.bar } = baz )",
230 options: [{ ignoreGlobals: true }],
231 parserOptions: { ecmaVersion: 6 },
232 globals: { a_global_variable: "readonly" } // eslint-disable-line camelcase -- Testing non-CamelCase
233 },
234 {
235 code: "a_global_variable = foo",
236 options: [{ ignoreGlobals: true }],
237 globals: { a_global_variable: "writable" } // eslint-disable-line camelcase -- Testing non-CamelCase
238 },
239 {
240 code: "a_global_variable = foo",
241 options: [{ ignoreGlobals: true }],
242 globals: { a_global_variable: "readonly" } // eslint-disable-line camelcase -- Testing non-CamelCase
243 },
244 {
245 code: "({ a_global_variable } = foo)",
246 options: [{ ignoreGlobals: true }],
247 parserOptions: { ecmaVersion: 6 },
248 globals: { a_global_variable: "writable" } // eslint-disable-line camelcase -- Testing non-CamelCase
249 },
250 {
251 code: "({ snake_cased: a_global_variable } = foo)",
252 options: [{ ignoreGlobals: true }],
253 parserOptions: { ecmaVersion: 6 },
254 globals: { a_global_variable: "writable" } // eslint-disable-line camelcase -- Testing non-CamelCase
255 },
256 {
257 code: "({ snake_cased: a_global_variable = foo } = bar)",
258 options: [{ ignoreGlobals: true }],
259 parserOptions: { ecmaVersion: 6 },
260 globals: { a_global_variable: "writable" } // eslint-disable-line camelcase -- Testing non-CamelCase
261 },
262 {
263 code: "[a_global_variable] = bar",
264 options: [{ ignoreGlobals: true }],
265 parserOptions: { ecmaVersion: 6 },
266 globals: { a_global_variable: "writable" } // eslint-disable-line camelcase -- Testing non-CamelCase
267 },
268 {
269 code: "[a_global_variable = foo] = bar",
270 options: [{ ignoreGlobals: true }],
271 parserOptions: { ecmaVersion: 6 },
272 globals: { a_global_variable: "writable" } // eslint-disable-line camelcase -- Testing non-CamelCase
273 },
274 {
275 code: "foo[a_global_variable] = bar",
276 options: [{ ignoreGlobals: true }],
277 globals: { a_global_variable: "readonly" } // eslint-disable-line camelcase -- Testing non-CamelCase
278 },
279 {
280 code: "var foo = { [a_global_variable]: bar }",
281 options: [{ ignoreGlobals: true }],
282 parserOptions: { ecmaVersion: 6 },
283 globals: { a_global_variable: "readonly" } // eslint-disable-line camelcase -- Testing non-CamelCase
284 },
285 {
286 code: "var { [a_global_variable]: foo } = bar",
287 options: [{ ignoreGlobals: true }],
288 parserOptions: { ecmaVersion: 6 },
289 globals: { a_global_variable: "readonly" } // eslint-disable-line camelcase -- Testing non-CamelCase
290 },
291 {
292 code: "function foo({ no_camelcased: camelCased }) {};",
293 parserOptions: { ecmaVersion: 6 }
294 },
295 {
296 code: "function foo({ no_camelcased: _leading }) {};",
297 parserOptions: { ecmaVersion: 6 }
298 },
299 {
300 code: "function foo({ no_camelcased: trailing_ }) {};",
301 parserOptions: { ecmaVersion: 6 }
302 },
303 {
304 code: "function foo({ camelCased = 'default value' }) {};",
305 parserOptions: { ecmaVersion: 6 }
306 },
307 {
308 code: "function foo({ _leading = 'default value' }) {};",
309 parserOptions: { ecmaVersion: 6 }
310 },
311 {
312 code: "function foo({ trailing_ = 'default value' }) {};",
313 parserOptions: { ecmaVersion: 6 }
314 },
315 {
316 code: "function foo({ camelCased }) {};",
317 parserOptions: { ecmaVersion: 6 }
318 },
319 {
320 code: "function foo({ _leading }) {}",
321 parserOptions: { ecmaVersion: 6 }
322 },
323 {
324 code: "function foo({ trailing_ }) {}",
325 parserOptions: { ecmaVersion: 6 }
326 },
327 {
328 code: "ignored_foo = 0;",
329 options: [{ allow: ["ignored_foo"] }]
330 },
331 {
332 code: "ignored_foo = 0; ignored_bar = 1;",
333 options: [{ allow: ["ignored_foo", "ignored_bar"] }]
334 },
335 {
336 code: "user_id = 0;",
337 options: [{ allow: ["_id$"] }]
338 },
339 {
340 code: "__option_foo__ = 0;",
341 options: [{ allow: ["__option_foo__"] }]
342 },
343 {
344 code: "foo = { [computedBar]: 0 };",
345 options: [{ ignoreDestructuring: true }],
346 parserOptions: { ecmaVersion: 6 }
347 },
348 {
349 code: "({ a: obj.fo_o } = bar);",
350 options: [{ allow: ["fo_o"] }],
351 parserOptions: { ecmaVersion: 6 }
352 },
353 {
354 code: "({ a: obj.foo } = bar);",
355 options: [{ allow: ["fo_o"] }],
356 parserOptions: { ecmaVersion: 6 }
357 },
358 {
359 code: "({ a: obj.fo_o } = bar);",
360 options: [{ properties: "never" }],
361 parserOptions: { ecmaVersion: 6 }
362 },
363 {
364 code: "({ a: obj.fo_o.b_ar } = bar);",
365 options: [{ properties: "never" }],
366 parserOptions: { ecmaVersion: 6 }
367 },
368 {
369 code: "({ a: { b: obj.fo_o } } = bar);",
370 options: [{ properties: "never" }],
371 parserOptions: { ecmaVersion: 6 }
372 },
373 {
374 code: "([obj.fo_o] = bar);",
375 options: [{ properties: "never" }],
376 parserOptions: { ecmaVersion: 6 }
377 },
378 {
379 code: "({ c: [ob.fo_o]} = bar);",
380 options: [{ properties: "never" }],
381 parserOptions: { ecmaVersion: 6 }
382 },
383 {
384 code: "([obj.fo_o.b_ar] = bar);",
385 options: [{ properties: "never" }],
386 parserOptions: { ecmaVersion: 6 }
387 },
388 {
389 code: "({obj} = baz.fo_o);",
390 parserOptions: { ecmaVersion: 6 }
391 },
392 {
393 code: "([obj] = baz.fo_o);",
394 parserOptions: { ecmaVersion: 6 }
395 },
396 {
397 code: "([obj.foo = obj.fo_o] = bar);",
398 options: [{ properties: "always" }],
399 parserOptions: { ecmaVersion: 6 }
400 },
401 {
402 code: "class C { camelCase; #camelCase; #camelCase2() {} }",
403 options: [{ properties: "always" }],
404 parserOptions: { ecmaVersion: 2022 }
405 },
406 {
407 code: "class C { snake_case; #snake_case; #snake_case2() {} }",
408 options: [{ properties: "never" }],
409 parserOptions: { ecmaVersion: 2022 }
410 },
411
412 // Combinations of `properties` and `ignoreDestructuring`
413 {
414 code: `
415 const { some_property } = obj;
416
417 const bar = { some_property };
418
419 obj.some_property = 10;
420
421 const xyz = { some_property: obj.some_property };
422
423 const foo = ({ some_property }) => {
424 console.log(some_property)
425 };
426 `,
427 options: [{ properties: "never", ignoreDestructuring: true }],
428 parserOptions: { ecmaVersion: 2022 }
429 },
430
431 // https://github.com/eslint/eslint/issues/15572
432 {
433 code: `
434 const { some_property } = obj;
435 doSomething({ some_property });
436 `,
437 options: [{ properties: "never", ignoreDestructuring: true }],
438 parserOptions: { ecmaVersion: 2022 }
439 }
440 ],
441 invalid: [
442 {
443 code: "first_name = \"Nicholas\"",
444 errors: [
445 {
446 messageId: "notCamelCase",
447 data: { name: "first_name" },
448 type: "Identifier"
449 }
450 ]
451 },
452 {
453 code: "__private_first_name = \"Patrick\"",
454 errors: [
455 {
456 messageId: "notCamelCase",
457 data: { name: "__private_first_name" },
458 type: "Identifier"
459 }
460 ]
461 },
462 {
463 code: "function foo_bar(){}",
464 errors: [
465 {
466 messageId: "notCamelCase",
467 data: { name: "foo_bar" },
468 type: "Identifier"
469 }
470 ]
471 },
472 {
473 code: "obj.foo_bar = function(){};",
474 errors: [
475 {
476 messageId: "notCamelCase",
477 data: { name: "foo_bar" },
478 type: "Identifier"
479 }
480 ]
481 },
482 {
483 code: "bar_baz.foo = function(){};",
484 errors: [
485 {
486 messageId: "notCamelCase",
487 data: { name: "bar_baz" },
488 type: "Identifier"
489 }
490 ]
491 },
492 {
493 code: "[foo_bar.baz]",
494 errors: [
495 {
496 messageId: "notCamelCase",
497 data: { name: "foo_bar" },
498 type: "Identifier"
499 }
500 ]
501 },
502 {
503 code: "if (foo.bar_baz === boom.bam_pow) { [foo_bar.baz] }",
504 errors: [
505 {
506 messageId: "notCamelCase",
507 data: { name: "foo_bar" },
508 type: "Identifier"
509 }
510 ]
511 },
512 {
513 code: "foo.bar_baz = boom.bam_pow",
514 errors: [
515 {
516 messageId: "notCamelCase",
517 data: { name: "bar_baz" },
518 type: "Identifier"
519 }
520 ]
521 },
522 {
523 code: "var foo = { bar_baz: boom.bam_pow }",
524 errors: [
525 {
526 messageId: "notCamelCase",
527 data: { name: "bar_baz" },
528 type: "Identifier"
529 }
530 ]
531 },
532 {
533 code: "var foo = { bar_baz: boom.bam_pow }",
534 options: [{ ignoreDestructuring: true }],
535 errors: [
536 {
537 messageId: "notCamelCase",
538 data: { name: "bar_baz" },
539 type: "Identifier"
540 }
541 ]
542 },
543 {
544 code: "foo.qux.boom_pow = { bar: boom.bam_pow }",
545 errors: [
546 {
547 messageId: "notCamelCase",
548 data: { name: "boom_pow" },
549 type: "Identifier"
550 }
551 ]
552 },
553 {
554 code: "var o = {bar_baz: 1}",
555 options: [{ properties: "always" }],
556 errors: [
557 {
558 messageId: "notCamelCase",
559 data: { name: "bar_baz" },
560 type: "Identifier"
561 }
562 ]
563 },
564 {
565 code: "obj.a_b = 2;",
566 options: [{ properties: "always" }],
567 errors: [
568 {
569 messageId: "notCamelCase",
570 data: { name: "a_b" },
571 type: "Identifier"
572 }
573 ]
574 },
575 {
576 code: "var { category_id: category_alias } = query;",
577 parserOptions: { ecmaVersion: 6 },
578 errors: [
579 {
580 messageId: "notCamelCase",
581 data: { name: "category_alias" },
582 type: "Identifier"
583 }
584 ]
585 },
586 {
587 code: "var { category_id: category_alias } = query;",
588 options: [{ ignoreDestructuring: true }],
589 parserOptions: { ecmaVersion: 6 },
590 errors: [
591 {
592 messageId: "notCamelCase",
593 data: { name: "category_alias" },
594 type: "Identifier"
595 }
596 ]
597 },
598 {
599 code: "var { [category_id]: categoryId } = query;",
600 options: [{ ignoreDestructuring: true }],
601 parserOptions: { ecmaVersion: 6 },
602 errors: [
603 {
604 messageId: "notCamelCase",
605 data: { name: "category_id" },
606 type: "Identifier"
607 }
608 ]
609 },
610 {
611 code: "var { [category_id]: categoryId } = query;",
612 parserOptions: { ecmaVersion: 6 },
613 errors: [
614 {
615 messageId: "notCamelCase",
616 data: { name: "category_id" },
617 type: "Identifier"
618 }
619 ]
620 },
621 {
622 code: "var { category_id: categoryId, ...other_props } = query;",
623 options: [{ ignoreDestructuring: true }],
624 parserOptions: { ecmaVersion: 2018 },
625 errors: [
626 {
627 messageId: "notCamelCase",
628 data: { name: "other_props" },
629 type: "Identifier"
630 }
631 ]
632 },
633 {
634 code: "var { category_id } = query;",
635 parserOptions: { ecmaVersion: 6 },
636 errors: [
637 {
638 messageId: "notCamelCase",
639 data: { name: "category_id" },
640 type: "Identifier"
641 }
642 ]
643 },
644 {
645 code: "var { category_id: category_id } = query;",
646 parserOptions: { ecmaVersion: 6 },
647 errors: [
648 {
649 messageId: "notCamelCase",
650 data: { name: "category_id" },
651 type: "Identifier"
652 }
653 ]
654 },
655 {
656 code: "var { category_id = 1 } = query;",
657 parserOptions: { ecmaVersion: 6 },
658 errors: [
659 {
660 messageId: "notCamelCase",
661 data: { name: "category_id" },
662 type: "Identifier"
663 }
664 ]
665 },
666 {
667 code: "import no_camelcased from \"external-module\";",
668 parserOptions: { ecmaVersion: 6, sourceType: "module" },
669 errors: [
670 {
671 messageId: "notCamelCase",
672 data: { name: "no_camelcased" },
673 type: "Identifier"
674 }
675 ]
676 },
677 {
678 code: "import * as no_camelcased from \"external-module\";",
679 parserOptions: { ecmaVersion: 6, sourceType: "module" },
680 errors: [
681 {
682 messageId: "notCamelCase",
683 data: { name: "no_camelcased" },
684 type: "Identifier"
685 }
686 ]
687 },
688 {
689 code: "import { no_camelcased } from \"external-module\";",
690 parserOptions: { ecmaVersion: 6, sourceType: "module" },
691 errors: [
692 {
693 messageId: "notCamelCase",
694 data: { name: "no_camelcased" },
695 type: "Identifier"
696 }
697 ]
698 },
699 {
700 code: "import { no_camelcased as no_camel_cased } from \"external module\";",
701 parserOptions: { ecmaVersion: 6, sourceType: "module" },
702 errors: [
703 {
704 messageId: "notCamelCase",
705 data: { name: "no_camel_cased" },
706 type: "Identifier"
707 }
708 ]
709 },
710 {
711 code: "import { camelCased as no_camel_cased } from \"external module\";",
712 parserOptions: { ecmaVersion: 6, sourceType: "module" },
713 errors: [
714 {
715 messageId: "notCamelCase",
716 data: { name: "no_camel_cased" },
717 type: "Identifier"
718 }
719 ]
720 },
721 {
722 code: "import { 'snake_cased' as snake_cased } from 'mod'",
723 parserOptions: { ecmaVersion: 2022, sourceType: "module" },
724 errors: [
725 {
726 messageId: "notCamelCase",
727 data: { name: "snake_cased" },
728 type: "Identifier"
729 }
730 ]
731 },
732 {
733 code: "import { 'snake_cased' as another_snake_cased } from 'mod'",
734 options: [{ ignoreImports: true }],
735 parserOptions: { ecmaVersion: 2022, sourceType: "module" },
736 errors: [
737 {
738 messageId: "notCamelCase",
739 data: { name: "another_snake_cased" },
740 type: "Identifier"
741 }
742 ]
743 },
744 {
745 code: "import { camelCased, no_camelcased } from \"external-module\";",
746 parserOptions: { ecmaVersion: 6, sourceType: "module" },
747 errors: [
748 {
749 messageId: "notCamelCase",
750 data: { name: "no_camelcased" },
751 type: "Identifier"
752 }
753 ]
754 },
755 {
756 code: "import { no_camelcased as camelCased, another_no_camelcased } from \"external-module\";",
757 parserOptions: { ecmaVersion: 6, sourceType: "module" },
758 errors: [
759 {
760 messageId: "notCamelCase",
761 data: { name: "another_no_camelcased" },
762 type: "Identifier"
763 }
764 ]
765 },
766 {
767 code: "import camelCased, { no_camelcased } from \"external-module\";",
768 parserOptions: { ecmaVersion: 6, sourceType: "module" },
769 errors: [
770 {
771 messageId: "notCamelCase",
772 data: { name: "no_camelcased" },
773 type: "Identifier"
774 }
775 ]
776 },
777 {
778 code: "import no_camelcased, { another_no_camelcased as camelCased } from \"external-module\";",
779 parserOptions: { ecmaVersion: 6, sourceType: "module" },
780 errors: [
781 {
782 messageId: "notCamelCase",
783 data: { name: "no_camelcased" },
784 type: "Identifier"
785 }
786 ]
787 },
788 {
789 code: "import snake_cased from 'mod'",
790 options: [{ ignoreImports: true }],
791 parserOptions: { ecmaVersion: 6, sourceType: "module" },
792 errors: [
793 {
794 messageId: "notCamelCase",
795 data: { name: "snake_cased" },
796 type: "Identifier"
797 }
798 ]
799 },
800 {
801 code: "import * as snake_cased from 'mod'",
802 options: [{ ignoreImports: true }],
803 parserOptions: { ecmaVersion: 6, sourceType: "module" },
804 errors: [
805 {
806 messageId: "notCamelCase",
807 data: { name: "snake_cased" },
808 type: "Identifier"
809 }
810 ]
811 },
812 {
813 code: "import snake_cased from 'mod'",
814 options: [{ ignoreImports: false }],
815 parserOptions: { ecmaVersion: 6, sourceType: "module" },
816 errors: [
817 {
818 messageId: "notCamelCase",
819 data: { name: "snake_cased" },
820 type: "Identifier"
821 }
822 ]
823 },
824 {
825 code: "import * as snake_cased from 'mod'",
826 options: [{ ignoreImports: false }],
827 parserOptions: { ecmaVersion: 6, sourceType: "module" },
828 errors: [
829 {
830 messageId: "notCamelCase",
831 data: { name: "snake_cased" },
832 type: "Identifier"
833 }
834 ]
835 },
836 {
837 code: "var camelCased = snake_cased",
838 options: [{ ignoreGlobals: false }],
839 globals: { snake_cased: "readonly" }, // eslint-disable-line camelcase -- Testing non-CamelCase
840 errors: [
841 {
842 messageId: "notCamelCase",
843 data: { name: "snake_cased" },
844 type: "Identifier"
845 }
846 ]
847 },
848 {
849 code: "a_global_variable.foo()",
850 options: [{ ignoreGlobals: false }],
851 globals: { snake_cased: "readonly" }, // eslint-disable-line camelcase -- Testing non-CamelCase
852 errors: [
853 {
854 messageId: "notCamelCase",
855 data: { name: "a_global_variable" },
856 type: "Identifier"
857 }
858 ]
859 },
860 {
861 code: "a_global_variable[undefined]",
862 options: [{ ignoreGlobals: false }],
863 globals: { snake_cased: "readonly" }, // eslint-disable-line camelcase -- Testing non-CamelCase
864 errors: [
865 {
866 messageId: "notCamelCase",
867 data: { name: "a_global_variable" },
868 type: "Identifier"
869 }
870 ]
871 },
872 {
873 code: "var camelCased = snake_cased",
874 globals: { snake_cased: "readonly" }, // eslint-disable-line camelcase -- Testing non-CamelCase
875 errors: [
876 {
877 messageId: "notCamelCase",
878 data: { name: "snake_cased" },
879 type: "Identifier"
880 }
881 ]
882 },
883 {
884 code: "var camelCased = snake_cased",
885 options: [{}],
886 globals: { snake_cased: "readonly" }, // eslint-disable-line camelcase -- Testing non-CamelCase
887 errors: [
888 {
889 messageId: "notCamelCase",
890 data: { name: "snake_cased" },
891 type: "Identifier"
892 }
893 ]
894 },
895 {
896 code: "foo.a_global_variable = bar",
897 options: [{ ignoreGlobals: true }],
898 globals: { a_global_variable: "writable" }, // eslint-disable-line camelcase -- Testing non-CamelCase
899 errors: [
900 {
901 messageId: "notCamelCase",
902 data: { name: "a_global_variable" },
903 type: "Identifier"
904 }
905 ]
906 },
907 {
908 code: "var foo = { a_global_variable: bar }",
909 options: [{ ignoreGlobals: true }],
910 globals: { a_global_variable: "writable" }, // eslint-disable-line camelcase -- Testing non-CamelCase
911 errors: [
912 {
913 messageId: "notCamelCase",
914 data: { name: "a_global_variable" },
915 type: "Identifier"
916 }
917 ]
918 },
919 {
920 code: "var foo = { a_global_variable: a_global_variable }",
921 options: [{ ignoreGlobals: true }],
922 globals: { a_global_variable: "writable" }, // eslint-disable-line camelcase -- Testing non-CamelCase
923 errors: [
924 {
925 messageId: "notCamelCase",
926 data: { name: "a_global_variable" },
927 type: "Identifier",
928 column: 13
929 }
930 ]
931 },
932 {
933 code: "var foo = { a_global_variable() {} }",
934 options: [{ ignoreGlobals: true }],
935 parserOptions: { ecmaVersion: 6 },
936 globals: { a_global_variable: "writable" }, // eslint-disable-line camelcase -- Testing non-CamelCase
937 errors: [
938 {
939 messageId: "notCamelCase",
940 data: { name: "a_global_variable" },
941 type: "Identifier"
942 }
943 ]
944 },
945 {
946 code: "class Foo { a_global_variable() {} }",
947 options: [{ ignoreGlobals: true }],
948 parserOptions: { ecmaVersion: 6 },
949 globals: { a_global_variable: "writable" }, // eslint-disable-line camelcase -- Testing non-CamelCase
950 errors: [
951 {
952 messageId: "notCamelCase",
953 data: { name: "a_global_variable" },
954 type: "Identifier"
955 }
956 ]
957 },
958 {
959 code: "a_global_variable: for (;;);",
960 options: [{ ignoreGlobals: true }],
961 globals: { a_global_variable: "writable" }, // eslint-disable-line camelcase -- Testing non-CamelCase
962 errors: [
963 {
964 messageId: "notCamelCase",
965 data: { name: "a_global_variable" },
966 type: "Identifier"
967 }
968 ]
969 },
970 {
971 code: "if (foo) { let a_global_variable; a_global_variable = bar; }",
972 options: [{ ignoreGlobals: true }],
973 parserOptions: { ecmaVersion: 6 },
974 globals: { a_global_variable: "writable" }, // eslint-disable-line camelcase -- Testing non-CamelCase
975 errors: [
976 {
977 messageId: "notCamelCase",
978 data: { name: "a_global_variable" },
979 type: "Identifier",
980 column: 16
981 },
982 {
983 messageId: "notCamelCase",
984 data: { name: "a_global_variable" },
985 type: "Identifier",
986 column: 35
987 }
988 ]
989 },
990 {
991 code: "function foo(a_global_variable) { foo = a_global_variable; }",
992 options: [{ ignoreGlobals: true }],
993 parserOptions: { ecmaVersion: 6 },
994 globals: { a_global_variable: "writable" }, // eslint-disable-line camelcase -- Testing non-CamelCase
995 errors: [
996 {
997 messageId: "notCamelCase",
998 data: { name: "a_global_variable" },
999 type: "Identifier",
1000 column: 14
1001 },
1002 {
1003 messageId: "notCamelCase",
1004 data: { name: "a_global_variable" },
1005 type: "Identifier",
1006 column: 41
1007 }
1008 ]
1009 },
1010 {
1011 code: "var a_global_variable",
1012 options: [{ ignoreGlobals: true }],
1013 parserOptions: { ecmaVersion: 6 },
1014 errors: [
1015 {
1016 messageId: "notCamelCase",
1017 data: { name: "a_global_variable" },
1018 type: "Identifier"
1019 }
1020 ]
1021 },
1022 {
1023 code: "function a_global_variable () {}",
1024 options: [{ ignoreGlobals: true }],
1025 parserOptions: { ecmaVersion: 6 },
1026 errors: [
1027 {
1028 messageId: "notCamelCase",
1029 data: { name: "a_global_variable" },
1030 type: "Identifier"
1031 }
1032 ]
1033 },
1034 {
1035 code: "const a_global_variable = foo; bar = a_global_variable",
1036 options: [{ ignoreGlobals: true }],
1037 parserOptions: { ecmaVersion: 6 },
1038 globals: { a_global_variable: "writable" }, // eslint-disable-line camelcase -- Testing non-CamelCase
1039 errors: [
1040 {
1041 messageId: "notCamelCase",
1042 data: { name: "a_global_variable" },
1043 type: "Identifier",
1044 column: 7
1045 },
1046 {
1047 messageId: "notCamelCase",
1048 data: { name: "a_global_variable" },
1049 type: "Identifier",
1050 column: 38
1051 }
1052 ]
1053 },
1054 {
1055 code: "bar = a_global_variable; var a_global_variable;",
1056 options: [{ ignoreGlobals: true }],
1057 parserOptions: { ecmaVersion: 6 },
1058 globals: { a_global_variable: "writable" }, // eslint-disable-line camelcase -- Testing non-CamelCase
1059 errors: [
1060 {
1061 messageId: "notCamelCase",
1062 data: { name: "a_global_variable" },
1063 type: "Identifier",
1064 column: 7
1065 },
1066 {
1067 messageId: "notCamelCase",
1068 data: { name: "a_global_variable" },
1069 type: "Identifier",
1070 column: 30
1071 }
1072 ]
1073 },
1074 {
1075 code: "var foo = { a_global_variable }",
1076 options: [{ ignoreGlobals: true }],
1077 parserOptions: { ecmaVersion: 6 },
1078 globals: { a_global_variable: "readonly" }, // eslint-disable-line camelcase -- Testing non-CamelCase
1079 errors: [
1080 {
1081 messageId: "notCamelCase",
1082 data: { name: "a_global_variable" },
1083 type: "Identifier"
1084 }
1085 ]
1086 },
1087 {
1088 code: "undefined_variable;",
1089 options: [{ ignoreGlobals: true }],
1090 errors: [
1091 {
1092 messageId: "notCamelCase",
1093 data: { name: "undefined_variable" },
1094 type: "Identifier"
1095 }
1096 ]
1097 },
1098 {
1099 code: "implicit_global = 1;",
1100 options: [{ ignoreGlobals: true }],
1101 errors: [
1102 {
1103 messageId: "notCamelCase",
1104 data: { name: "implicit_global" },
1105 type: "Identifier"
1106 }
1107 ]
1108 },
1109 {
1110 code: "export * as snake_cased from 'mod'",
1111 parserOptions: { ecmaVersion: 2020, sourceType: "module" },
1112 errors: [
1113 {
1114 messageId: "notCamelCase",
1115 data: { name: "snake_cased" },
1116 type: "Identifier"
1117 }
1118 ]
1119 },
1120 {
1121 code: "function foo({ no_camelcased }) {};",
1122 parserOptions: { ecmaVersion: 6 },
1123 errors: [
1124 {
1125 messageId: "notCamelCase",
1126 data: { name: "no_camelcased" },
1127 type: "Identifier"
1128 }
1129 ]
1130 },
1131 {
1132 code: "function foo({ no_camelcased = 'default value' }) {};",
1133 parserOptions: { ecmaVersion: 6 },
1134 errors: [
1135 {
1136 messageId: "notCamelCase",
1137 data: { name: "no_camelcased" },
1138 type: "Identifier"
1139 }
1140 ]
1141 },
1142 {
1143 code: "const no_camelcased = 0; function foo({ camelcased_value = no_camelcased}) {}",
1144 parserOptions: { ecmaVersion: 6 },
1145 errors: [
1146 {
1147 messageId: "notCamelCase",
1148 data: { name: "no_camelcased" },
1149 type: "Identifier"
1150 },
1151 {
1152 messageId: "notCamelCase",
1153 data: { name: "camelcased_value" },
1154 type: "Identifier"
1155 }
1156 ]
1157 },
1158 {
1159 code: "const { bar: no_camelcased } = foo;",
1160 parserOptions: { ecmaVersion: 6 },
1161 errors: [
1162 {
1163 messageId: "notCamelCase",
1164 data: { name: "no_camelcased" },
1165 type: "Identifier"
1166 }
1167 ]
1168 },
1169 {
1170 code: "function foo({ value_1: my_default }) {}",
1171 parserOptions: { ecmaVersion: 6 },
1172 errors: [
1173 {
1174 messageId: "notCamelCase",
1175 data: { name: "my_default" },
1176 type: "Identifier"
1177 }
1178 ]
1179 },
1180 {
1181 code: "function foo({ isCamelcased: no_camelcased }) {};",
1182 parserOptions: { ecmaVersion: 6 },
1183 errors: [
1184 {
1185 messageId: "notCamelCase",
1186 data: { name: "no_camelcased" },
1187 type: "Identifier"
1188 }
1189 ]
1190 },
1191 {
1192 code: "var { foo: bar_baz = 1 } = quz;",
1193 parserOptions: { ecmaVersion: 6 },
1194 errors: [
1195 {
1196 messageId: "notCamelCase",
1197 data: { name: "bar_baz" },
1198 type: "Identifier"
1199 }
1200 ]
1201 },
1202 {
1203 code: "const { no_camelcased = false } = bar;",
1204 parserOptions: { ecmaVersion: 6 },
1205 errors: [
1206 {
1207 messageId: "notCamelCase",
1208 data: { name: "no_camelcased" },
1209 type: "Identifier"
1210 }
1211 ]
1212 },
1213 {
1214 code: "const { no_camelcased = foo_bar } = bar;",
1215 parserOptions: { ecmaVersion: 6 },
1216 errors: [
1217 {
1218 messageId: "notCamelCase",
1219 data: { name: "no_camelcased" },
1220 type: "Identifier"
1221 }
1222 ]
1223 },
1224 {
1225 code: "not_ignored_foo = 0;",
1226 options: [{ allow: ["ignored_bar"] }],
1227 errors: [
1228 {
1229 messageId: "notCamelCase",
1230 data: { name: "not_ignored_foo" },
1231 type: "Identifier"
1232 }
1233 ]
1234 },
1235 {
1236 code: "not_ignored_foo = 0;",
1237 options: [{ allow: ["_id$"] }],
1238 errors: [
1239 {
1240 messageId: "notCamelCase",
1241 data: { name: "not_ignored_foo" },
1242 type: "Identifier"
1243 }
1244 ]
1245 },
1246 {
1247 code: "foo = { [computed_bar]: 0 };",
1248 options: [{ ignoreDestructuring: true }],
1249 parserOptions: { ecmaVersion: 6 },
1250 errors: [
1251 {
1252 messageId: "notCamelCase",
1253 data: { name: "computed_bar" },
1254 type: "Identifier"
1255 }
1256 ]
1257 },
1258 {
1259 code: "({ a: obj.fo_o } = bar);",
1260 parserOptions: { ecmaVersion: 6 },
1261 errors: [
1262 {
1263 messageId: "notCamelCase",
1264 data: { name: "fo_o" },
1265 type: "Identifier"
1266 }
1267 ]
1268 },
1269 {
1270 code: "({ a: obj.fo_o } = bar);",
1271 options: [{ ignoreDestructuring: true }],
1272 parserOptions: { ecmaVersion: 6 },
1273 errors: [
1274 {
1275 messageId: "notCamelCase",
1276 data: { name: "fo_o" },
1277 type: "Identifier"
1278 }
1279 ]
1280 },
1281 {
1282 code: "({ a: obj.fo_o.b_ar } = baz);",
1283 parserOptions: { ecmaVersion: 6 },
1284 errors: [
1285 {
1286 messageId: "notCamelCase",
1287 data: { name: "b_ar" },
1288 type: "Identifier"
1289 }
1290 ]
1291 },
1292 {
1293 code: "({ a: { b: { c: obj.fo_o } } } = bar);",
1294 parserOptions: { ecmaVersion: 6 },
1295 errors: [
1296 {
1297 messageId: "notCamelCase",
1298 data: { name: "fo_o" },
1299 type: "Identifier"
1300 }
1301 ]
1302 },
1303 {
1304 code: "({ a: { b: { c: obj.fo_o.b_ar } } } = baz);",
1305 parserOptions: { ecmaVersion: 6 },
1306 errors: [
1307 {
1308 messageId: "notCamelCase",
1309 data: { name: "b_ar" },
1310 type: "Identifier"
1311 }
1312 ]
1313 },
1314 {
1315 code: "([obj.fo_o] = bar);",
1316 parserOptions: { ecmaVersion: 6 },
1317 errors: [
1318 {
1319 messageId: "notCamelCase",
1320 data: { name: "fo_o" },
1321 type: "Identifier"
1322 }
1323 ]
1324 },
1325 {
1326 code: "([obj.fo_o] = bar);",
1327 options: [{ ignoreDestructuring: true }],
1328 parserOptions: { ecmaVersion: 6 },
1329 errors: [
1330 {
1331 messageId: "notCamelCase",
1332 data: { name: "fo_o" },
1333 type: "Identifier"
1334 }
1335 ]
1336 },
1337 {
1338 code: "([obj.fo_o = 1] = bar);",
1339 options: [{ properties: "always" }],
1340 parserOptions: { ecmaVersion: 6 },
1341 errors: [
1342 {
1343 messageId: "notCamelCase",
1344 data: { name: "fo_o" },
1345 type: "Identifier"
1346 }
1347 ]
1348 },
1349 {
1350 code: "({ a: [obj.fo_o] } = bar);",
1351 parserOptions: { ecmaVersion: 6 },
1352 errors: [
1353 {
1354 messageId: "notCamelCase",
1355 data: { name: "fo_o" },
1356 type: "Identifier"
1357 }
1358 ]
1359 },
1360 {
1361 code: "({ a: { b: [obj.fo_o] } } = bar);",
1362 parserOptions: { ecmaVersion: 6 },
1363 errors: [
1364 {
1365 messageId: "notCamelCase",
1366 data: { name: "fo_o" },
1367 type: "Identifier"
1368 }
1369 ]
1370 },
1371 {
1372 code: "([obj.fo_o.ba_r] = baz);",
1373 parserOptions: { ecmaVersion: 6 },
1374 errors: [
1375 {
1376 messageId: "notCamelCase",
1377 data: { name: "ba_r" },
1378 type: "Identifier"
1379 }
1380 ]
1381 },
1382 {
1383 code: "({...obj.fo_o} = baz);",
1384 parserOptions: { ecmaVersion: 9 },
1385 errors: [
1386 {
1387 messageId: "notCamelCase",
1388 data: { name: "fo_o" },
1389 type: "Identifier"
1390 }
1391 ]
1392 },
1393 {
1394 code: "({...obj.fo_o.ba_r} = baz);",
1395 parserOptions: { ecmaVersion: 9 },
1396 errors: [
1397 {
1398 messageId: "notCamelCase",
1399 data: { name: "ba_r" },
1400 type: "Identifier"
1401 }
1402 ]
1403 },
1404 {
1405 code: "({c: {...obj.fo_o }} = baz);",
1406 parserOptions: { ecmaVersion: 9 },
1407 errors: [
1408 {
1409 messageId: "notCamelCase",
1410 data: { name: "fo_o" },
1411 type: "Identifier"
1412 }
1413 ]
1414 },
1415
1416 // Optional chaining.
1417 {
1418 code: "obj.o_k.non_camelcase = 0",
1419 options: [{ properties: "always" }],
1420 parserOptions: { ecmaVersion: 2020 },
1421 errors: [{ messageId: "notCamelCase", data: { name: "non_camelcase" } }]
1422 },
1423 {
1424 code: "(obj?.o_k).non_camelcase = 0",
1425 options: [{ properties: "always" }],
1426 parserOptions: { ecmaVersion: 2020 },
1427 errors: [{ messageId: "notCamelCase", data: { name: "non_camelcase" } }]
1428 },
1429
1430 // class public/private fields, private methods.
1431 {
1432 code: "class C { snake_case; }",
1433 options: [{ properties: "always" }],
1434 parserOptions: { ecmaVersion: 2022 },
1435 errors: [{ messageId: "notCamelCase", data: { name: "snake_case" } }]
1436 },
1437 {
1438 code: "class C { #snake_case; foo() { this.#snake_case; } }",
1439 options: [{ properties: "always" }],
1440 parserOptions: { ecmaVersion: 2022 },
1441 errors: [{ messageId: "notCamelCasePrivate", data: { name: "snake_case" }, column: 11 }]
1442 },
1443 {
1444 code: "class C { #snake_case() {} }",
1445 options: [{ properties: "always" }],
1446 parserOptions: { ecmaVersion: 2022 },
1447 errors: [{ messageId: "notCamelCasePrivate", data: { name: "snake_case" } }]
1448 },
1449
1450 // Combinations of `properties` and `ignoreDestructuring`
1451 {
1452 code: `
1453 const { some_property } = obj;
1454 doSomething({ some_property });
1455 `,
1456 options: [{ properties: "always", ignoreDestructuring: true }],
1457 parserOptions: { ecmaVersion: 2022 },
1458 errors: [
1459 {
1460 messageId: "notCamelCase",
1461 data: { name: "some_property" },
1462 line: 3,
1463 column: 27
1464 }
1465 ]
1466 },
1467 {
1468 code: `
1469 const { some_property } = obj;
1470 doSomething({ some_property });
1471 doSomething({ [some_property]: "bar" });
1472 `,
1473 options: [{ properties: "never", ignoreDestructuring: true }],
1474 parserOptions: { ecmaVersion: 2022 },
1475 errors: [
1476 {
1477 messageId: "notCamelCase",
1478 data: { name: "some_property" },
1479 line: 4,
1480 column: 28
1481 }
1482 ]
1483 },
1484 {
1485 code: `
1486 const { some_property } = obj;
1487
1488 const bar = { some_property };
1489
1490 obj.some_property = 10;
1491
1492 const xyz = { some_property: obj.some_property };
1493
1494 const foo = ({ some_property }) => {
1495 console.log(some_property)
1496 };
1497 `,
1498 options: [{ properties: "always", ignoreDestructuring: true }],
1499 parserOptions: { ecmaVersion: 2022 },
1500 errors: [
1501 {
1502 messageId: "notCamelCase",
1503 data: { name: "some_property" },
1504 line: 4,
1505 column: 27
1506 },
1507 {
1508 messageId: "notCamelCase",
1509 data: { name: "some_property" },
1510 line: 6,
1511 column: 17
1512 },
1513 {
1514 messageId: "notCamelCase",
1515 data: { name: "some_property" },
1516 line: 8,
1517 column: 27
1518 }
1519 ]
1520 }
1521 ]
1522 });