@@ -460,6 +460,19 @@ ruleTester.run('exhaustive-deps', rule, {
460
460
});
461
461
` ,
462
462
} ,
463
+ {
464
+ name : 'should not fail when queryFn inside queryOptions contains a reference to an external variable' ,
465
+ code : normalizeIndent `
466
+ const EXTERNAL = 1;
467
+
468
+ export const queries = {
469
+ foo: queryOptions({
470
+ queryKey: ['foo'],
471
+ queryFn: () => Promise.resolve(EXTERNAL),
472
+ }),
473
+ };
474
+ ` ,
475
+ } ,
463
476
] ,
464
477
invalid : [
465
478
{
@@ -492,8 +505,10 @@ ruleTester.run('exhaustive-deps', rule, {
492
505
{
493
506
name : 'should fail when no deps are passed (react)' ,
494
507
code : normalizeIndent `
495
- const id = 1;
496
- useQuery({ queryKey: ["entity"], queryFn: () => api.getEntity(id) });
508
+ function Component() {
509
+ const id = 1;
510
+ useQuery({ queryKey: ["entity"], queryFn: () => api.getEntity(id) });
511
+ }
497
512
` ,
498
513
errors : [
499
514
{
@@ -504,8 +519,10 @@ ruleTester.run('exhaustive-deps', rule, {
504
519
messageId : 'fixTo' ,
505
520
data : { result : '["entity", id]' } ,
506
521
output : normalizeIndent `
507
- const id = 1;
508
- useQuery({ queryKey: ["entity", id], queryFn: () => api.getEntity(id) });
522
+ function Component() {
523
+ const id = 1;
524
+ useQuery({ queryKey: ["entity", id], queryFn: () => api.getEntity(id) });
525
+ }
509
526
` ,
510
527
} ,
511
528
] ,
@@ -515,8 +532,10 @@ ruleTester.run('exhaustive-deps', rule, {
515
532
{
516
533
name : 'should fail when no deps are passed (solid)' ,
517
534
code : normalizeIndent `
518
- const id = 1;
519
- createQuery({ queryKey: ["entity"], queryFn: () => api.getEntity(id) });
535
+ function Component() {
536
+ const id = 1;
537
+ createQuery({ queryKey: ["entity"], queryFn: () => api.getEntity(id) });
538
+ }
520
539
` ,
521
540
errors : [
522
541
{
@@ -527,8 +546,10 @@ ruleTester.run('exhaustive-deps', rule, {
527
546
messageId : 'fixTo' ,
528
547
data : { result : '["entity", id]' } ,
529
548
output : normalizeIndent `
530
- const id = 1;
531
- createQuery({ queryKey: ["entity", id], queryFn: () => api.getEntity(id) });
549
+ function Component() {
550
+ const id = 1;
551
+ createQuery({ queryKey: ["entity", id], queryFn: () => api.getEntity(id) });
552
+ }
532
553
` ,
533
554
} ,
534
555
] ,
@@ -538,8 +559,10 @@ ruleTester.run('exhaustive-deps', rule, {
538
559
{
539
560
name : 'should fail when deps are passed incorrectly' ,
540
561
code : normalizeIndent `
541
- const id = 1;
542
- useQuery({ queryKey: ["entity/\${id}"], queryFn: () => api.getEntity(id) });
562
+ function Component() {
563
+ const id = 1;
564
+ useQuery({ queryKey: ["entity/\${id}"], queryFn: () => api.getEntity(id) });
565
+ }
543
566
` ,
544
567
errors : [
545
568
{
@@ -550,8 +573,10 @@ ruleTester.run('exhaustive-deps', rule, {
550
573
messageId : 'fixTo' ,
551
574
data : { result : '["entity/${id}", id]' } ,
552
575
output : normalizeIndent `
553
- const id = 1;
554
- useQuery({ queryKey: ["entity/\${id}", id], queryFn: () => api.getEntity(id) });
576
+ function Component() {
577
+ const id = 1;
578
+ useQuery({ queryKey: ["entity/\${id}", id], queryFn: () => api.getEntity(id) });
579
+ }
555
580
` ,
556
581
} ,
557
582
] ,
@@ -561,9 +586,11 @@ ruleTester.run('exhaustive-deps', rule, {
561
586
{
562
587
name : 'should pass missing dep while key has a template literal' ,
563
588
code : normalizeIndent `
564
- const a = 1;
565
- const b = 2;
566
- useQuery({ queryKey: [\`entity/\${a}\`], queryFn: () => api.getEntity(a, b) });
589
+ function Component() {
590
+ const a = 1;
591
+ const b = 2;
592
+ useQuery({ queryKey: [\`entity/\${a}\`], queryFn: () => api.getEntity(a, b) });
593
+ }
567
594
` ,
568
595
errors : [
569
596
{
@@ -574,9 +601,11 @@ ruleTester.run('exhaustive-deps', rule, {
574
601
messageId : 'fixTo' ,
575
602
data : { result : '[`entity/${a}`, b]' } ,
576
603
output : normalizeIndent `
577
- const a = 1;
578
- const b = 2;
579
- useQuery({ queryKey: [\`entity/\${a}\`, b], queryFn: () => api.getEntity(a, b) });
604
+ function Component() {
605
+ const a = 1;
606
+ const b = 2;
607
+ useQuery({ queryKey: [\`entity/\${a}\`, b], queryFn: () => api.getEntity(a, b) });
608
+ }
580
609
` ,
581
610
} ,
582
611
] ,
@@ -586,14 +615,16 @@ ruleTester.run('exhaustive-deps', rule, {
586
615
{
587
616
name : 'should fail when dep exists inside setter and missing in queryKey' ,
588
617
code : normalizeIndent `
589
- const [id] = React.useState(1);
590
- useQuery({
618
+ function Component() {
619
+ const [id] = React.useState(1);
620
+ useQuery({
591
621
queryKey: ["entity"],
592
622
queryFn: () => {
593
- const { data } = axios.get(\`.../\${id}\`);
594
- return data;
623
+ const { data } = axios.get(\`.../\${id}\`);
624
+ return data;
595
625
}
596
- });
626
+ });
627
+ }
597
628
` ,
598
629
errors : [
599
630
{
@@ -604,14 +635,16 @@ ruleTester.run('exhaustive-deps', rule, {
604
635
messageId : 'fixTo' ,
605
636
data : { result : '["entity", id]' } ,
606
637
output : normalizeIndent `
607
- const [id] = React.useState(1);
608
- useQuery({
638
+ function Component() {
639
+ const [id] = React.useState(1);
640
+ useQuery({
609
641
queryKey: ["entity", id],
610
642
queryFn: () => {
611
- const { data } = axios.get(\`.../\${id}\`);
612
- return data;
643
+ const { data } = axios.get(\`.../\${id}\`);
644
+ return data;
613
645
}
614
- });
646
+ });
647
+ }
615
648
` ,
616
649
} ,
617
650
] ,
@@ -713,9 +746,11 @@ ruleTester.run('exhaustive-deps', rule, {
713
746
{
714
747
name : 'should fail when a queryKey is a reference of an array expression with a missing dep' ,
715
748
code : normalizeIndent `
716
- const x = 5;
717
- const queryKey = ['foo']
718
- useQuery({ queryKey, queryFn: () => x })
749
+ function Component() {
750
+ const x = 5;
751
+ const queryKey = ['foo']
752
+ useQuery({ queryKey, queryFn: () => x })
753
+ }
719
754
` ,
720
755
errors : [
721
756
{
@@ -728,9 +763,11 @@ ruleTester.run('exhaustive-deps', rule, {
728
763
result : "['foo', x]" ,
729
764
} ,
730
765
output : normalizeIndent `
731
- const x = 5;
732
- const queryKey = ['foo', x]
733
- useQuery({ queryKey, queryFn: () => x })
766
+ function Component() {
767
+ const x = 5;
768
+ const queryKey = ['foo', x]
769
+ useQuery({ queryKey, queryFn: () => x })
770
+ }
734
771
` ,
735
772
} ,
736
773
] ,
@@ -758,25 +795,29 @@ ruleTester.run('exhaustive-deps', rule, {
758
795
{
759
796
name : 'should fail if queryFn is using multiple object props when only one of them is in the queryKey' ,
760
797
code : normalizeIndent `
761
- const state = { foo: 'foo', bar: 'bar' }
798
+ function Component() {
799
+ const state = { foo: 'foo', bar: 'bar' }
762
800
763
- useQuery({
801
+ useQuery({
764
802
queryKey: ['state', state.foo],
765
803
queryFn: () => Promise.resolve({ foo: state.foo, bar: state.bar })
766
- })
804
+ })
805
+ }
767
806
` ,
768
807
errors : [
769
808
{
770
809
suggestions : [
771
810
{
772
811
messageId : 'fixTo' ,
773
812
output : normalizeIndent `
774
- const state = { foo: 'foo', bar: 'bar' }
813
+ function Component() {
814
+ const state = { foo: 'foo', bar: 'bar' }
775
815
776
- useQuery({
816
+ useQuery({
777
817
queryKey: ['state', state.foo, state.bar],
778
818
queryFn: () => Promise.resolve({ foo: state.foo, bar: state.bar })
779
- })
819
+ })
820
+ }
780
821
` ,
781
822
} ,
782
823
] ,
@@ -788,29 +829,33 @@ ruleTester.run('exhaustive-deps', rule, {
788
829
{
789
830
name : 'should fail if queryFn is invalid while using FunctionExpression syntax' ,
790
831
code : normalizeIndent `
791
- const id = 1;
832
+ function Component() {
833
+ const id = 1;
792
834
793
- useQuery({
835
+ useQuery({
794
836
queryKey: [],
795
837
queryFn() {
796
838
Promise.resolve(id)
797
839
}
798
- })
840
+ });
841
+ }
799
842
` ,
800
843
errors : [
801
844
{
802
845
suggestions : [
803
846
{
804
847
messageId : 'fixTo' ,
805
848
output : normalizeIndent `
806
- const id = 1;
849
+ function Component() {
850
+ const id = 1;
807
851
808
- useQuery({
852
+ useQuery({
809
853
queryKey: [id],
810
854
queryFn() {
811
855
Promise.resolve(id)
812
856
}
813
- })
857
+ });
858
+ }
814
859
` ,
815
860
} ,
816
861
] ,
0 commit comments