Skip to content

Commit c5d6919

Browse files
committed
fix(eslint-plugin-query): recognize query client methods
- Update prefer-query-options for `query` and `infiniteQuery` - Refresh infinite-query type tests and lint rule coverage
1 parent 12027ef commit c5d6919

9 files changed

Lines changed: 37 additions & 233 deletions

File tree

.changeset/tough-queries-listen.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/eslint-plugin-query': patch
3+
---
4+
5+
Recognize `query` and `infiniteQuery` client calls in the `prefer-query-options` rule.

packages/angular-query-experimental/src/__tests__/infinite-query-options.test-d.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -218,15 +218,7 @@ describe('infiniteQueryOptions', () => {
218218
)
219219
assertType(
220220
// @ts-expect-error cannot pass infinite options to non-infinite query functions
221-
queryClient.ensureQueryData(options),
222-
)
223-
assertType(
224-
// @ts-expect-error cannot pass infinite options to non-infinite query functions
225-
queryClient.fetchQuery(options),
226-
)
227-
assertType(
228-
// @ts-expect-error cannot pass infinite options to non-infinite query functions
229-
queryClient.prefetchQuery(options),
221+
queryClient.query(options),
230222
)
231223
})
232224

packages/eslint-plugin-query/src/__tests__/no-void-query-fn.test.ts

Lines changed: 10 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -204,66 +204,29 @@ ruleTester.run('no-void-query-fn', rule, {
204204
`,
205205
},
206206
{
207-
name: 'fetchQuery queryFn returns a value',
207+
name: 'query queryFn returns a value',
208208
code: normalizeIndent`
209209
import { QueryClient } from '@tanstack/react-query'
210210
211211
const queryClient = new QueryClient()
212-
queryClient.fetchQuery({
212+
queryClient.query({
213213
queryKey: ['test'],
214214
queryFn: () => fetch('/api/test').then((r) => r.json()),
215215
})
216216
`,
217217
},
218218
{
219-
name: 'prefetchQuery queryFn returns a value',
219+
name: 'infiniteQuery queryFn returns a value',
220220
code: normalizeIndent`
221221
import { QueryClient } from '@tanstack/react-query'
222222
223223
const queryClient = new QueryClient()
224-
queryClient.prefetchQuery({
225-
queryKey: ['test'],
226-
queryFn: () => fetch('/api/test').then((r) => r.json()),
227-
})
228-
`,
229-
},
230-
{
231-
name: 'prefetchInfiniteQuery queryFn returns a value',
232-
code: normalizeIndent`
233-
import { QueryClient } from '@tanstack/react-query'
234-
235-
const queryClient = new QueryClient()
236-
queryClient.prefetchInfiniteQuery({
237-
queryKey: ['test'],
238-
queryFn: ({ pageParam }: { pageParam: number }) =>
239-
fetch(\`/api/test?page=\${pageParam}\`).then((r) => r.json()),
240-
initialPageParam: 0,
241-
})
242-
`,
243-
},
244-
{
245-
name: 'ensureQueryData queryFn returns a value',
246-
code: normalizeIndent`
247-
import { QueryClient } from '@tanstack/react-query'
248-
249-
const queryClient = new QueryClient()
250-
queryClient.ensureQueryData({
251-
queryKey: ['test'],
252-
queryFn: () => fetch('/api/test').then((r) => r.json()),
253-
})
254-
`,
255-
},
256-
{
257-
name: 'ensureInfiniteQueryData queryFn returns a value',
258-
code: normalizeIndent`
259-
import { QueryClient } from '@tanstack/react-query'
260-
261-
const queryClient = new QueryClient()
262-
queryClient.ensureInfiniteQueryData({
224+
queryClient.infiniteQuery({
263225
queryKey: ['test'],
264226
queryFn: ({ pageParam }: { pageParam: number }) =>
265227
fetch(\`/api/test?page=\${pageParam}\`).then((r) => r.json()),
266228
initialPageParam: 0,
229+
getNextPageParam: () => undefined,
267230
})
268231
`,
269232
},
@@ -555,58 +518,12 @@ ruleTester.run('no-void-query-fn', rule, {
555518
errors: [{ messageId: 'noVoidReturn' }],
556519
},
557520
{
558-
name: 'fetchQuery queryFn returns void',
559-
code: normalizeIndent`
560-
import { QueryClient } from '@tanstack/react-query'
561-
562-
const queryClient = new QueryClient()
563-
queryClient.fetchQuery({
564-
queryKey: ['test'],
565-
queryFn: async () => {
566-
await fetch('/api/test')
567-
},
568-
})
569-
`,
570-
errors: [{ messageId: 'noVoidReturn' }],
571-
},
572-
{
573-
name: 'prefetchQuery queryFn returns void',
574-
code: normalizeIndent`
575-
import { QueryClient } from '@tanstack/react-query'
576-
577-
const queryClient = new QueryClient()
578-
queryClient.prefetchQuery({
579-
queryKey: ['test'],
580-
queryFn: async () => {
581-
await fetch('/api/test')
582-
},
583-
})
584-
`,
585-
errors: [{ messageId: 'noVoidReturn' }],
586-
},
587-
{
588-
name: 'prefetchInfiniteQuery queryFn returns void',
589-
code: normalizeIndent`
590-
import { QueryClient } from '@tanstack/react-query'
591-
592-
const queryClient = new QueryClient()
593-
queryClient.prefetchInfiniteQuery({
594-
queryKey: ['test'],
595-
queryFn: async ({ pageParam }: { pageParam: number }) => {
596-
await fetch(\`/api/test?page=\${pageParam}\`)
597-
},
598-
initialPageParam: 0,
599-
})
600-
`,
601-
errors: [{ messageId: 'noVoidReturn' }],
602-
},
603-
{
604-
name: 'ensureQueryData queryFn returns void',
521+
name: 'query queryFn returns void',
605522
code: normalizeIndent`
606523
import { QueryClient } from '@tanstack/react-query'
607524
608525
const queryClient = new QueryClient()
609-
queryClient.ensureQueryData({
526+
queryClient.query({
610527
queryKey: ['test'],
611528
queryFn: async () => {
612529
await fetch('/api/test')
@@ -616,17 +533,18 @@ ruleTester.run('no-void-query-fn', rule, {
616533
errors: [{ messageId: 'noVoidReturn' }],
617534
},
618535
{
619-
name: 'ensureInfiniteQueryData queryFn returns void',
536+
name: 'infiniteQuery queryFn returns void',
620537
code: normalizeIndent`
621538
import { QueryClient } from '@tanstack/react-query'
622539
623540
const queryClient = new QueryClient()
624-
queryClient.ensureInfiniteQueryData({
541+
queryClient.infiniteQuery({
625542
queryKey: ['test'],
626543
queryFn: async ({ pageParam }: { pageParam: number }) => {
627544
await fetch(\`/api/test?page=\${pageParam}\`)
628545
},
629546
initialPageParam: 0,
547+
getNextPageParam: () => undefined,
630548
})
631549
`,
632550
errors: [{ messageId: 'noVoidReturn' }],

packages/eslint-plugin-query/src/__tests__/prefer-query-options.test.ts

Lines changed: 12 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ describe('prefer-query-options', () => {
221221
const queryClient = useQueryClient()
222222
223223
function run(queryClient) {
224-
queryClient.fetchQuery({
224+
queryClient.query({
225225
queryKey: ['todos'],
226226
queryFn: () => fetchTodos(),
227227
})
@@ -232,20 +232,20 @@ describe('prefer-query-options', () => {
232232
`,
233233
},
234234
{
235-
name: 'non-queryClient fetchQuery call is ignored',
235+
name: 'non-queryClient query call is ignored',
236236
code: normalizeIndent`
237237
import { useQuery } from '@tanstack/react-query'
238238
239239
const analytics = {
240-
fetchQuery(options) {
240+
query(options) {
241241
return options
242242
},
243243
}
244244
245245
function Component() {
246246
useQuery(todosOptions)
247247
248-
analytics.fetchQuery({
248+
analytics.query({
249249
queryKey: ['todos'],
250250
queryFn: () => fetchTodos(),
251251
})
@@ -581,13 +581,13 @@ describe('prefer-query-options', () => {
581581
valid: [],
582582
invalid: [
583583
{
584-
name: 'client.fetchQuery with inline queryKey + queryFn',
584+
name: 'client.query with inline queryKey + queryFn',
585585
code: normalizeIndent`
586586
import { useQueryClient } from '@tanstack/react-query'
587587
588588
function Component() {
589589
const client = useQueryClient()
590-
client.fetchQuery({
590+
client.query({
591591
queryKey: ['todos'],
592592
queryFn: () => fetchTodos(),
593593
})
@@ -603,7 +603,7 @@ describe('prefer-query-options', () => {
603603
604604
function Component() {
605605
const client = getClient()
606-
client.fetchQuery({
606+
client.query({
607607
queryKey: ['todos'],
608608
queryFn: () => fetchTodos(),
609609
})
@@ -619,7 +619,7 @@ describe('prefer-query-options', () => {
619619
620620
const queryClient = new Client()
621621
622-
queryClient.fetchQuery({
622+
queryClient.query({
623623
queryKey: ['todos'],
624624
queryFn: () => fetchTodos(),
625625
})
@@ -661,13 +661,13 @@ describe('prefer-query-options', () => {
661661
valid: [],
662662
invalid: [
663663
{
664-
name: 'queryClient.fetchQuery with inline queryKey + queryFn',
664+
name: 'queryClient.query with inline queryKey + queryFn',
665665
code: normalizeIndent`
666666
import { useQueryClient } from '@tanstack/react-query'
667667
668668
function Component() {
669669
const queryClient = useQueryClient()
670-
queryClient.fetchQuery({
670+
queryClient.query({
671671
queryKey: ['todos'],
672672
queryFn: () => fetchTodos(),
673673
})
@@ -677,81 +677,13 @@ describe('prefer-query-options', () => {
677677
errors: [{ messageId: 'preferQueryOptions' }],
678678
},
679679
{
680-
name: 'queryClient.prefetchQuery with inline queryKey + queryFn',
680+
name: 'queryClient.infiniteQuery with inline queryKey + queryFn',
681681
code: normalizeIndent`
682682
import { useQueryClient } from '@tanstack/react-query'
683683
684684
function Component() {
685685
const queryClient = useQueryClient()
686-
queryClient.prefetchQuery({
687-
queryKey: ['todos'],
688-
queryFn: () => fetchTodos(),
689-
})
690-
return null
691-
}
692-
`,
693-
errors: [{ messageId: 'preferQueryOptions' }],
694-
},
695-
{
696-
name: 'queryClient.fetchInfiniteQuery with inline queryKey + queryFn',
697-
code: normalizeIndent`
698-
import { useQueryClient } from '@tanstack/react-query'
699-
700-
function Component() {
701-
const queryClient = useQueryClient()
702-
queryClient.fetchInfiniteQuery({
703-
queryKey: ['todos'],
704-
queryFn: ({ pageParam }) => fetchTodos(pageParam),
705-
initialPageParam: 0,
706-
getNextPageParam: (lastPage) => lastPage.nextCursor,
707-
})
708-
return null
709-
}
710-
`,
711-
errors: [{ messageId: 'preferQueryOptions' }],
712-
},
713-
{
714-
name: 'queryClient.prefetchInfiniteQuery with inline queryKey + queryFn',
715-
code: normalizeIndent`
716-
import { useQueryClient } from '@tanstack/react-query'
717-
718-
function Component() {
719-
const queryClient = useQueryClient()
720-
queryClient.prefetchInfiniteQuery({
721-
queryKey: ['todos'],
722-
queryFn: ({ pageParam }) => fetchTodos(pageParam),
723-
initialPageParam: 0,
724-
getNextPageParam: (lastPage) => lastPage.nextCursor,
725-
})
726-
return null
727-
}
728-
`,
729-
errors: [{ messageId: 'preferQueryOptions' }],
730-
},
731-
{
732-
name: 'queryClient.ensureQueryData with inline queryKey + queryFn',
733-
code: normalizeIndent`
734-
import { useQueryClient } from '@tanstack/react-query'
735-
736-
function Component() {
737-
const queryClient = useQueryClient()
738-
queryClient.ensureQueryData({
739-
queryKey: ['todos'],
740-
queryFn: () => fetchTodos(),
741-
})
742-
return null
743-
}
744-
`,
745-
errors: [{ messageId: 'preferQueryOptions' }],
746-
},
747-
{
748-
name: 'queryClient.ensureInfiniteQueryData with inline queryKey + queryFn',
749-
code: normalizeIndent`
750-
import { useQueryClient } from '@tanstack/react-query'
751-
752-
function Component() {
753-
const queryClient = useQueryClient()
754-
queryClient.ensureInfiniteQueryData({
686+
queryClient.infiniteQuery({
755687
queryKey: ['todos'],
756688
queryFn: ({ pageParam }) => fetchTodos(pageParam),
757689
initialPageParam: 0,

packages/eslint-plugin-query/src/rules/prefer-query-options/prefer-query-options.rule.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,7 @@ const queriesHooks = ['useQueries', 'useSuspenseQueries']
2020

2121
const filterHooks = ['useIsFetching']
2222

23-
const queryClientOptionMethods = [
24-
'fetchQuery',
25-
'prefetchQuery',
26-
'fetchInfiniteQuery',
27-
'prefetchInfiniteQuery',
28-
'ensureQueryData',
29-
'ensureInfiniteQueryData',
30-
]
23+
const queryClientOptionMethods = ['query', 'infiniteQuery']
3124

3225
const queryClientQueryKeyMethods = [
3326
'getQueryData',

packages/preact-query/src/__tests__/infiniteQueryOptions.test-d.tsx

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -228,15 +228,7 @@ describe('infiniteQueryOptions', () => {
228228
)
229229
assertType(
230230
// @ts-expect-error cannot pass infinite options to non-infinite query functions
231-
queryClient.ensureQueryData(options),
232-
)
233-
assertType(
234-
// @ts-expect-error cannot pass infinite options to non-infinite query functions
235-
queryClient.fetchQuery(options),
236-
)
237-
assertType(
238-
// @ts-expect-error cannot pass infinite options to non-infinite query functions
239-
queryClient.prefetchQuery(options),
231+
queryClient.query(options),
240232
)
241233
})
242234

packages/react-query/src/__tests__/infiniteQueryOptions.test-d.tsx

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -267,15 +267,7 @@ describe('infiniteQueryOptions', () => {
267267
)
268268
assertType(
269269
// @ts-expect-error cannot pass infinite options to non-infinite query functions
270-
queryClient.ensureQueryData(options),
271-
)
272-
assertType(
273-
// @ts-expect-error cannot pass infinite options to non-infinite query functions
274-
queryClient.fetchQuery(options),
275-
)
276-
assertType(
277-
// @ts-expect-error cannot pass infinite options to non-infinite query functions
278-
queryClient.prefetchQuery(options),
270+
queryClient.query(options),
279271
)
280272
})
281273

0 commit comments

Comments
 (0)