@tanstack/eslint-plugin-query 5.59.7 → 5.59.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -2
- package/src/__tests__/exhaustive-deps.test.ts +0 -859
- package/src/__tests__/infinite-query-property-order.rule.test.ts +0 -222
- package/src/__tests__/infinite-query-property-order.utils.test.ts +0 -79
- package/src/__tests__/no-rest-destructuring.test.ts +0 -192
- package/src/__tests__/no-unstable-deps.test.ts +0 -129
- package/src/__tests__/stable-query-client.test.ts +0 -192
- package/src/__tests__/test-utils.test.ts +0 -104
- package/src/__tests__/test-utils.ts +0 -108
|
@@ -1,859 +0,0 @@
|
|
|
1
|
-
import { RuleTester } from '@typescript-eslint/rule-tester'
|
|
2
|
-
import { rule } from '../rules/exhaustive-deps/exhaustive-deps.rule'
|
|
3
|
-
import { normalizeIndent } from './test-utils'
|
|
4
|
-
|
|
5
|
-
const ruleTester = new RuleTester()
|
|
6
|
-
|
|
7
|
-
ruleTester.run('exhaustive-deps', rule, {
|
|
8
|
-
valid: [
|
|
9
|
-
{
|
|
10
|
-
name: 'should pass when deps are passed in array (react)',
|
|
11
|
-
code: 'useQuery({ queryKey: ["todos"], queryFn: fetchTodos });',
|
|
12
|
-
},
|
|
13
|
-
{
|
|
14
|
-
name: 'should pass when deps are passed in array (solid)',
|
|
15
|
-
code: 'createQuery({ queryKey: ["todos"], queryFn: fetchTodos });',
|
|
16
|
-
},
|
|
17
|
-
{
|
|
18
|
-
name: 'should pass when deps are passed in array',
|
|
19
|
-
code: 'useQuery({ queryKey: ["entity", id], queryFn: () => api.getEntity(id) });',
|
|
20
|
-
},
|
|
21
|
-
{
|
|
22
|
-
name: 'should pass when deps are passed in template literal',
|
|
23
|
-
code: 'useQuery({ queryKey: [`entity/${id}`], queryFn: () => api.getEntity(id) });',
|
|
24
|
-
},
|
|
25
|
-
{
|
|
26
|
-
name: 'should not pass fetch',
|
|
27
|
-
code: 'useQuery({ queryKey: ["entity", id], queryFn: () => fetch(id) });',
|
|
28
|
-
},
|
|
29
|
-
{
|
|
30
|
-
name: 'should not pass axios.get',
|
|
31
|
-
code: 'useQuery({ queryKey: ["entity", id], queryFn: () => axios.get(id) });',
|
|
32
|
-
},
|
|
33
|
-
{
|
|
34
|
-
name: 'should not pass api.entity.get',
|
|
35
|
-
code: 'useQuery({ queryKey: ["entity", id], queryFn: () => api.entity.get(id) });',
|
|
36
|
-
},
|
|
37
|
-
{
|
|
38
|
-
name: 'should not pass api when is being used for calling a function',
|
|
39
|
-
code: `
|
|
40
|
-
import useApi from './useApi'
|
|
41
|
-
|
|
42
|
-
const useFoo = () => {
|
|
43
|
-
const api = useApi();
|
|
44
|
-
return useQuery({
|
|
45
|
-
queryKey: ['foo'],
|
|
46
|
-
queryFn: () => api.fetchFoo(),
|
|
47
|
-
})
|
|
48
|
-
}
|
|
49
|
-
`,
|
|
50
|
-
},
|
|
51
|
-
{
|
|
52
|
-
name: 'should pass props.src',
|
|
53
|
-
code: `
|
|
54
|
-
function MyComponent(props) {
|
|
55
|
-
useQuery({ queryKey: ["entity", props.src], queryFn: () => api.entity.get(props.src) });
|
|
56
|
-
}
|
|
57
|
-
`,
|
|
58
|
-
},
|
|
59
|
-
{
|
|
60
|
-
name: 'identify !!props.id (unary expression)',
|
|
61
|
-
code: `
|
|
62
|
-
function MyComponent(props) {
|
|
63
|
-
useQuery({ queryKey: ["entity", !!props.id], queryFn: () => api.entity.get(props.id) });
|
|
64
|
-
}
|
|
65
|
-
`,
|
|
66
|
-
},
|
|
67
|
-
{
|
|
68
|
-
name: 'identify props?.id (chain expression)',
|
|
69
|
-
code: `
|
|
70
|
-
function MyComponent(props) {
|
|
71
|
-
useQuery({ queryKey: ["entity", props?.id], queryFn: () => api.entity.get(props?.id) });
|
|
72
|
-
}
|
|
73
|
-
`,
|
|
74
|
-
},
|
|
75
|
-
{
|
|
76
|
-
name: 'identify props!.id (ts non null expression)',
|
|
77
|
-
code: `
|
|
78
|
-
function MyComponent(props) {
|
|
79
|
-
useQuery({ queryKey: ["entity", props!.id], queryFn: () => api.entity.get(props!.id) });
|
|
80
|
-
}
|
|
81
|
-
`,
|
|
82
|
-
},
|
|
83
|
-
{
|
|
84
|
-
name: 'should ignore keys from callback',
|
|
85
|
-
code: `
|
|
86
|
-
function MyComponent(props) {
|
|
87
|
-
useQuery({
|
|
88
|
-
queryKey: ["foo", dep1],
|
|
89
|
-
queryFn: ({ queryKey: [, dep] }) => fetch(dep),
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
`,
|
|
93
|
-
},
|
|
94
|
-
{
|
|
95
|
-
name: 'should ignore type identifiers',
|
|
96
|
-
code: `
|
|
97
|
-
type Result = {};
|
|
98
|
-
function MyComponent(props) {
|
|
99
|
-
useQuery({
|
|
100
|
-
queryKey: ["foo", dep],
|
|
101
|
-
queryFn: () => api.get<Result>(dep),
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
`,
|
|
105
|
-
},
|
|
106
|
-
{
|
|
107
|
-
name: 'should add "...args" to deps',
|
|
108
|
-
code: `
|
|
109
|
-
function foo(...args) {}
|
|
110
|
-
function useData(arg, ...args) {
|
|
111
|
-
return useQuery({
|
|
112
|
-
queryKey: ['foo', arg, ...args],
|
|
113
|
-
queryFn: async () => foo([arg, ...args])
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
`,
|
|
117
|
-
},
|
|
118
|
-
{
|
|
119
|
-
name: 'should not add class to deps',
|
|
120
|
-
code: `
|
|
121
|
-
class Foo {}
|
|
122
|
-
useQuery({ queryKey: ['foo'], queryFn: async () => new Foo() });
|
|
123
|
-
`,
|
|
124
|
-
},
|
|
125
|
-
{
|
|
126
|
-
name: 'should not add `undefined` to deps',
|
|
127
|
-
code: `
|
|
128
|
-
useQuery({
|
|
129
|
-
queryKey: [],
|
|
130
|
-
queryFn: async () => {
|
|
131
|
-
if (undefined) {
|
|
132
|
-
return null;
|
|
133
|
-
}
|
|
134
|
-
return 1
|
|
135
|
-
},
|
|
136
|
-
});
|
|
137
|
-
`,
|
|
138
|
-
},
|
|
139
|
-
{
|
|
140
|
-
name: 'should not fail when queryKey is a queryKeyFactory while having a dep as first arg',
|
|
141
|
-
code: normalizeIndent`
|
|
142
|
-
const fooQueryKeyFactory = {
|
|
143
|
-
foo: () => ['foo'] as const,
|
|
144
|
-
num: (num: number) => [...fooQueryKeyFactory.foo(), num] as const,
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
const useFoo = (num: number) =>
|
|
148
|
-
useQuery({
|
|
149
|
-
queryKey: fooQueryKeyFactory.foo(num),
|
|
150
|
-
queryFn: () => Promise.resolve(num),
|
|
151
|
-
})
|
|
152
|
-
`,
|
|
153
|
-
},
|
|
154
|
-
{
|
|
155
|
-
name: 'should not fail when queryKey is a queryKeyFactory while having a dep in object',
|
|
156
|
-
code: normalizeIndent`
|
|
157
|
-
const fooQueryKeyFactory = {
|
|
158
|
-
foo: () => ['foo'] as const,
|
|
159
|
-
num: (num: number) => [...fooQueryKeyFactory.foo(), num] as const,
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
const useFoo = (num: number) =>
|
|
163
|
-
useQuery({
|
|
164
|
-
queryKey: fooQueryKeyFactory.foo({ x: num }),
|
|
165
|
-
queryFn: () => Promise.resolve(num),
|
|
166
|
-
})
|
|
167
|
-
`,
|
|
168
|
-
},
|
|
169
|
-
{
|
|
170
|
-
name: 'should not fail when queryKey is a queryKeyFactory while having a dep in object 2',
|
|
171
|
-
code: normalizeIndent`
|
|
172
|
-
const fooQueryKeyFactory = {
|
|
173
|
-
foo: () => ['foo'] as const,
|
|
174
|
-
num: (num: number) => [...fooQueryKeyFactory.foo(), num] as const,
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
const useFoo = (num: number) =>
|
|
178
|
-
useQuery({
|
|
179
|
-
queryKey: fooQueryKeyFactory.foo({ num }),
|
|
180
|
-
queryFn: () => Promise.resolve(num),
|
|
181
|
-
})
|
|
182
|
-
`,
|
|
183
|
-
},
|
|
184
|
-
{
|
|
185
|
-
name: 'should not fail when queryKey is a queryKeyFactory while having a dep in array',
|
|
186
|
-
code: normalizeIndent`
|
|
187
|
-
const fooQueryKeyFactory = {
|
|
188
|
-
foo: () => ['foo'] as const,
|
|
189
|
-
num: (num: number) => [...fooQueryKeyFactory.foo(), num] as const,
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
const useFoo = (num: number) =>
|
|
193
|
-
useQuery({
|
|
194
|
-
queryKey: fooQueryKeyFactory.foo([num]),
|
|
195
|
-
queryFn: () => Promise.resolve(num),
|
|
196
|
-
})
|
|
197
|
-
`,
|
|
198
|
-
},
|
|
199
|
-
{
|
|
200
|
-
name: 'should not fail when queryKey is a queryKeyFactory while having a dep in second arg',
|
|
201
|
-
code: normalizeIndent`
|
|
202
|
-
const fooQueryKeyFactory = {
|
|
203
|
-
foo: () => ['foo'] as const,
|
|
204
|
-
num: (num: number) => [...fooQueryKeyFactory.foo(), num] as const,
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
const useFoo = (num: number) =>
|
|
208
|
-
useQuery({
|
|
209
|
-
queryKey: fooQueryKeyFactory.foo(1, num),
|
|
210
|
-
queryFn: () => Promise.resolve(num),
|
|
211
|
-
})
|
|
212
|
-
`,
|
|
213
|
-
},
|
|
214
|
-
{
|
|
215
|
-
name: 'should not fail when queryKey is a queryKeyFactory while having a dep is object prop',
|
|
216
|
-
code: normalizeIndent`
|
|
217
|
-
const fooQueryKeyFactory = {
|
|
218
|
-
foo: () => ['foo'] as const,
|
|
219
|
-
num: (num: number) => [...fooQueryKeyFactory.foo(), num] as const,
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
const useFoo = (obj: { num: number }) =>
|
|
223
|
-
useQuery({
|
|
224
|
-
queryKey: fooQueryKeyFactory.foo(obj.num),
|
|
225
|
-
queryFn: () => Promise.resolve(obj.num),
|
|
226
|
-
})
|
|
227
|
-
`,
|
|
228
|
-
},
|
|
229
|
-
{
|
|
230
|
-
name: 'should not treat new Error as missing dependency',
|
|
231
|
-
code: normalizeIndent`
|
|
232
|
-
useQuery({
|
|
233
|
-
queryKey: ['foo'],
|
|
234
|
-
queryFn: () => Promise.reject(new Error('1')),
|
|
235
|
-
})
|
|
236
|
-
`,
|
|
237
|
-
},
|
|
238
|
-
{
|
|
239
|
-
name: 'should see id when there is a const assertion',
|
|
240
|
-
code: normalizeIndent`
|
|
241
|
-
const useX = (id: number) => {
|
|
242
|
-
return useQuery({
|
|
243
|
-
queryKey: ['foo', id] as const,
|
|
244
|
-
queryFn: async () => id,
|
|
245
|
-
})
|
|
246
|
-
}
|
|
247
|
-
`,
|
|
248
|
-
},
|
|
249
|
-
{
|
|
250
|
-
name: 'should not fail if queryKey is having the whole object while queryFn uses some props of it',
|
|
251
|
-
code: normalizeIndent`
|
|
252
|
-
const state = { foo: 'foo', bar: 'bar' }
|
|
253
|
-
|
|
254
|
-
useQuery({
|
|
255
|
-
queryKey: ['state', state],
|
|
256
|
-
queryFn: () => Promise.resolve({ foo: state.foo, bar: state.bar })
|
|
257
|
-
})
|
|
258
|
-
`,
|
|
259
|
-
},
|
|
260
|
-
{
|
|
261
|
-
name: 'should not fail if queryKey does not include an internal dependency',
|
|
262
|
-
code: normalizeIndent`
|
|
263
|
-
useQuery({
|
|
264
|
-
queryKey: ["api"],
|
|
265
|
-
queryFn: async () => {
|
|
266
|
-
const response = Promise.resolve([]);
|
|
267
|
-
const data = await response.json();
|
|
268
|
-
return data[0].name;
|
|
269
|
-
},
|
|
270
|
-
});
|
|
271
|
-
`,
|
|
272
|
-
},
|
|
273
|
-
{
|
|
274
|
-
name: 'should ignore constants defined out of scope (react component, function declaration)',
|
|
275
|
-
code: `
|
|
276
|
-
const CONST_VAL = 1
|
|
277
|
-
function MyComponent() {
|
|
278
|
-
useQuery({
|
|
279
|
-
queryKey: ["foo"],
|
|
280
|
-
queryFn: () => CONST_VAL
|
|
281
|
-
});
|
|
282
|
-
}
|
|
283
|
-
`,
|
|
284
|
-
},
|
|
285
|
-
{
|
|
286
|
-
name: 'should ignore constants defined out of scope (react component, function expression)',
|
|
287
|
-
code: `
|
|
288
|
-
const CONST_VAL = 1
|
|
289
|
-
const MyComponent = () => {
|
|
290
|
-
useQuery({
|
|
291
|
-
queryKey: ["foo"],
|
|
292
|
-
queryFn: () => CONST_VAL
|
|
293
|
-
});
|
|
294
|
-
}
|
|
295
|
-
`,
|
|
296
|
-
},
|
|
297
|
-
{
|
|
298
|
-
name: 'should ignore constants defined out of scope (react component, anonymous function)',
|
|
299
|
-
code: `
|
|
300
|
-
const CONST_VAL = 1
|
|
301
|
-
const MyComponent = function () {
|
|
302
|
-
useQuery({
|
|
303
|
-
queryKey: ["foo"],
|
|
304
|
-
queryFn: () => CONST_VAL
|
|
305
|
-
});
|
|
306
|
-
}
|
|
307
|
-
`,
|
|
308
|
-
},
|
|
309
|
-
{
|
|
310
|
-
name: 'should ignore constants defined out of scope (non react component/hook function)',
|
|
311
|
-
code: `
|
|
312
|
-
const CONST_VAL = 1
|
|
313
|
-
function fn() {
|
|
314
|
-
return {
|
|
315
|
-
queryKey: ["foo"],
|
|
316
|
-
queryFn: () => CONST_VAL
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
`,
|
|
320
|
-
},
|
|
321
|
-
{
|
|
322
|
-
name: 'should ignore constants defined out of scope (react hook, function declaration)',
|
|
323
|
-
code: `
|
|
324
|
-
const CONST_VAL = 1
|
|
325
|
-
function useHook() {
|
|
326
|
-
useQuery({
|
|
327
|
-
queryKey: ["foo"],
|
|
328
|
-
queryFn: () => CONST_VAL
|
|
329
|
-
});
|
|
330
|
-
}
|
|
331
|
-
`,
|
|
332
|
-
},
|
|
333
|
-
{
|
|
334
|
-
name: 'should ignore constants defined out of scope (react hook, function expression)',
|
|
335
|
-
code: `
|
|
336
|
-
const CONST_VAL = 1
|
|
337
|
-
const useHook = () => {
|
|
338
|
-
useQuery({
|
|
339
|
-
queryKey: ["foo"],
|
|
340
|
-
queryFn: () => CONST_VAL
|
|
341
|
-
});
|
|
342
|
-
}
|
|
343
|
-
`,
|
|
344
|
-
},
|
|
345
|
-
{
|
|
346
|
-
name: 'should ignore constants defined out of scope (react hook, anonymous function)',
|
|
347
|
-
code: `
|
|
348
|
-
const CONST_VAL = 1
|
|
349
|
-
const useHook = function () {
|
|
350
|
-
useQuery({
|
|
351
|
-
queryKey: ["foo"],
|
|
352
|
-
queryFn: () => CONST_VAL
|
|
353
|
-
});
|
|
354
|
-
}
|
|
355
|
-
`,
|
|
356
|
-
},
|
|
357
|
-
{
|
|
358
|
-
name: 'should ignore references of the queryClient',
|
|
359
|
-
code: `
|
|
360
|
-
const CONST_VAL = 1
|
|
361
|
-
function useHook() {
|
|
362
|
-
const queryClient = useQueryClient()
|
|
363
|
-
const queryClient2 = useQueryClient()
|
|
364
|
-
useQuery({
|
|
365
|
-
queryKey: ["foo"],
|
|
366
|
-
queryFn: () => {
|
|
367
|
-
doSomething(queryClient)
|
|
368
|
-
queryClient.invalidateQueries()
|
|
369
|
-
doSomethingSus(queryClient2)
|
|
370
|
-
}
|
|
371
|
-
});
|
|
372
|
-
}
|
|
373
|
-
`,
|
|
374
|
-
},
|
|
375
|
-
{
|
|
376
|
-
name: 'query key with nullish coalescing operator',
|
|
377
|
-
code: `
|
|
378
|
-
const factory = (id: number) => ['foo', id];
|
|
379
|
-
function Component({ id }) {
|
|
380
|
-
useQuery({
|
|
381
|
-
queryKey: factory(id ?? -1),
|
|
382
|
-
queryFn: () => Promise.resolve({ id })
|
|
383
|
-
});
|
|
384
|
-
}
|
|
385
|
-
`,
|
|
386
|
-
},
|
|
387
|
-
{
|
|
388
|
-
name: 'instanceof value should not be in query key',
|
|
389
|
-
code: `
|
|
390
|
-
class SomeClass {}
|
|
391
|
-
|
|
392
|
-
function Component({ value }) {
|
|
393
|
-
useQuery({
|
|
394
|
-
queryKey: ['foo'],
|
|
395
|
-
queryFn: () => {
|
|
396
|
-
return value instanceof SomeClass;
|
|
397
|
-
}
|
|
398
|
-
});
|
|
399
|
-
}
|
|
400
|
-
`,
|
|
401
|
-
},
|
|
402
|
-
{
|
|
403
|
-
name: 'queryFn as a ternary expression with dep and a skipToken',
|
|
404
|
-
code: normalizeIndent`
|
|
405
|
-
import { useQuery, skipToken } from "@tanstack/react-query";
|
|
406
|
-
const fetch = true
|
|
407
|
-
|
|
408
|
-
function Component({ id }) {
|
|
409
|
-
useQuery({
|
|
410
|
-
queryKey: [id],
|
|
411
|
-
queryFn: fetch ? () => Promise.resolve(id) : skipToken
|
|
412
|
-
})
|
|
413
|
-
}
|
|
414
|
-
`,
|
|
415
|
-
},
|
|
416
|
-
{
|
|
417
|
-
name: 'should not fail when queryFn uses nullish coalescing operator',
|
|
418
|
-
code: normalizeIndent`
|
|
419
|
-
useQuery({
|
|
420
|
-
queryKey: ["foo", options],
|
|
421
|
-
queryFn: () => options?.params ?? options
|
|
422
|
-
});
|
|
423
|
-
`,
|
|
424
|
-
},
|
|
425
|
-
{
|
|
426
|
-
name: 'should not fail when queryKey uses arrow function to produce a key',
|
|
427
|
-
code: normalizeIndent`
|
|
428
|
-
const obj = reactive<{ boo?: string }>({});
|
|
429
|
-
|
|
430
|
-
const query = useQuery({
|
|
431
|
-
queryKey: ['foo', () => obj.boo],
|
|
432
|
-
queryFn: () => fetch(\`/mock/getSomething/\${obj.boo}\`),
|
|
433
|
-
enable: () => !!obj.boo,
|
|
434
|
-
});
|
|
435
|
-
`,
|
|
436
|
-
},
|
|
437
|
-
{
|
|
438
|
-
name: 'should not fail when queryKey uses arrow function to produce a key as the body return',
|
|
439
|
-
code: normalizeIndent`
|
|
440
|
-
const obj = reactive<{ boo?: string }>({});
|
|
441
|
-
|
|
442
|
-
const query = useQuery({
|
|
443
|
-
queryKey: ['foo', () => { return obj.boo }],
|
|
444
|
-
queryFn: () => fetch(\`/mock/getSomething/\${obj.boo}\`),
|
|
445
|
-
enable: () => !!obj.boo,
|
|
446
|
-
});
|
|
447
|
-
`,
|
|
448
|
-
},
|
|
449
|
-
{
|
|
450
|
-
name: 'should not fail when queryKey uses function expression to produce a key as the body return',
|
|
451
|
-
code: normalizeIndent`
|
|
452
|
-
const obj = reactive<{ boo?: string }>({});
|
|
453
|
-
|
|
454
|
-
const query = useQuery({
|
|
455
|
-
queryKey: ['foo', function() {
|
|
456
|
-
return obj.boo
|
|
457
|
-
}],
|
|
458
|
-
queryFn: () => fetch(\`/mock/getSomething/\${obj.boo}\`),
|
|
459
|
-
enable: () => !!obj.boo,
|
|
460
|
-
});
|
|
461
|
-
`,
|
|
462
|
-
},
|
|
463
|
-
],
|
|
464
|
-
invalid: [
|
|
465
|
-
{
|
|
466
|
-
name: 'should fail when deps are missing in query factory',
|
|
467
|
-
code: normalizeIndent`
|
|
468
|
-
const todoQueries = {
|
|
469
|
-
list: () => ({ queryKey: ['entity'], queryFn: fetchEntities }),
|
|
470
|
-
detail: (id) => ({ queryKey: ['entity'], queryFn: () => fetchEntity(id) })
|
|
471
|
-
}
|
|
472
|
-
`,
|
|
473
|
-
errors: [
|
|
474
|
-
{
|
|
475
|
-
messageId: 'missingDeps',
|
|
476
|
-
data: { deps: 'id' },
|
|
477
|
-
suggestions: [
|
|
478
|
-
{
|
|
479
|
-
messageId: 'fixTo',
|
|
480
|
-
data: { result: "['entity', id]" },
|
|
481
|
-
output: normalizeIndent`
|
|
482
|
-
const todoQueries = {
|
|
483
|
-
list: () => ({ queryKey: ['entity'], queryFn: fetchEntities }),
|
|
484
|
-
detail: (id) => ({ queryKey: ['entity', id], queryFn: () => fetchEntity(id) })
|
|
485
|
-
}
|
|
486
|
-
`,
|
|
487
|
-
},
|
|
488
|
-
],
|
|
489
|
-
},
|
|
490
|
-
],
|
|
491
|
-
},
|
|
492
|
-
{
|
|
493
|
-
name: 'should fail when no deps are passed (react)',
|
|
494
|
-
code: normalizeIndent`
|
|
495
|
-
const id = 1;
|
|
496
|
-
useQuery({ queryKey: ["entity"], queryFn: () => api.getEntity(id) });
|
|
497
|
-
`,
|
|
498
|
-
errors: [
|
|
499
|
-
{
|
|
500
|
-
messageId: 'missingDeps',
|
|
501
|
-
data: { deps: 'id' },
|
|
502
|
-
suggestions: [
|
|
503
|
-
{
|
|
504
|
-
messageId: 'fixTo',
|
|
505
|
-
data: { result: '["entity", id]' },
|
|
506
|
-
output: normalizeIndent`
|
|
507
|
-
const id = 1;
|
|
508
|
-
useQuery({ queryKey: ["entity", id], queryFn: () => api.getEntity(id) });
|
|
509
|
-
`,
|
|
510
|
-
},
|
|
511
|
-
],
|
|
512
|
-
},
|
|
513
|
-
],
|
|
514
|
-
},
|
|
515
|
-
{
|
|
516
|
-
name: 'should fail when no deps are passed (solid)',
|
|
517
|
-
code: normalizeIndent`
|
|
518
|
-
const id = 1;
|
|
519
|
-
createQuery({ queryKey: ["entity"], queryFn: () => api.getEntity(id) });
|
|
520
|
-
`,
|
|
521
|
-
errors: [
|
|
522
|
-
{
|
|
523
|
-
messageId: 'missingDeps',
|
|
524
|
-
data: { deps: 'id' },
|
|
525
|
-
suggestions: [
|
|
526
|
-
{
|
|
527
|
-
messageId: 'fixTo',
|
|
528
|
-
data: { result: '["entity", id]' },
|
|
529
|
-
output: normalizeIndent`
|
|
530
|
-
const id = 1;
|
|
531
|
-
createQuery({ queryKey: ["entity", id], queryFn: () => api.getEntity(id) });
|
|
532
|
-
`,
|
|
533
|
-
},
|
|
534
|
-
],
|
|
535
|
-
},
|
|
536
|
-
],
|
|
537
|
-
},
|
|
538
|
-
{
|
|
539
|
-
name: 'should fail when deps are passed incorrectly',
|
|
540
|
-
code: normalizeIndent`
|
|
541
|
-
const id = 1;
|
|
542
|
-
useQuery({ queryKey: ["entity/\${id}"], queryFn: () => api.getEntity(id) });
|
|
543
|
-
`,
|
|
544
|
-
errors: [
|
|
545
|
-
{
|
|
546
|
-
messageId: 'missingDeps',
|
|
547
|
-
data: { deps: 'id' },
|
|
548
|
-
suggestions: [
|
|
549
|
-
{
|
|
550
|
-
messageId: 'fixTo',
|
|
551
|
-
data: { result: '["entity/${id}", id]' },
|
|
552
|
-
output: normalizeIndent`
|
|
553
|
-
const id = 1;
|
|
554
|
-
useQuery({ queryKey: ["entity/\${id}", id], queryFn: () => api.getEntity(id) });
|
|
555
|
-
`,
|
|
556
|
-
},
|
|
557
|
-
],
|
|
558
|
-
},
|
|
559
|
-
],
|
|
560
|
-
},
|
|
561
|
-
{
|
|
562
|
-
name: 'should pass missing dep while key has a template literal',
|
|
563
|
-
code: normalizeIndent`
|
|
564
|
-
const a = 1;
|
|
565
|
-
const b = 2;
|
|
566
|
-
useQuery({ queryKey: [\`entity/\${a}\`], queryFn: () => api.getEntity(a, b) });
|
|
567
|
-
`,
|
|
568
|
-
errors: [
|
|
569
|
-
{
|
|
570
|
-
messageId: 'missingDeps',
|
|
571
|
-
data: { deps: 'b' },
|
|
572
|
-
suggestions: [
|
|
573
|
-
{
|
|
574
|
-
messageId: 'fixTo',
|
|
575
|
-
data: { result: '[`entity/${a}`, b]' },
|
|
576
|
-
output: normalizeIndent`
|
|
577
|
-
const a = 1;
|
|
578
|
-
const b = 2;
|
|
579
|
-
useQuery({ queryKey: [\`entity/\${a}\`, b], queryFn: () => api.getEntity(a, b) });
|
|
580
|
-
`,
|
|
581
|
-
},
|
|
582
|
-
],
|
|
583
|
-
},
|
|
584
|
-
],
|
|
585
|
-
},
|
|
586
|
-
{
|
|
587
|
-
name: 'should fail when dep exists inside setter and missing in queryKey',
|
|
588
|
-
code: normalizeIndent`
|
|
589
|
-
const [id] = React.useState(1);
|
|
590
|
-
useQuery({
|
|
591
|
-
queryKey: ["entity"],
|
|
592
|
-
queryFn: () => {
|
|
593
|
-
const { data } = axios.get(\`.../\${id}\`);
|
|
594
|
-
return data;
|
|
595
|
-
}
|
|
596
|
-
});
|
|
597
|
-
`,
|
|
598
|
-
errors: [
|
|
599
|
-
{
|
|
600
|
-
messageId: 'missingDeps',
|
|
601
|
-
data: { deps: 'id' },
|
|
602
|
-
suggestions: [
|
|
603
|
-
{
|
|
604
|
-
messageId: 'fixTo',
|
|
605
|
-
data: { result: '["entity", id]' },
|
|
606
|
-
output: normalizeIndent`
|
|
607
|
-
const [id] = React.useState(1);
|
|
608
|
-
useQuery({
|
|
609
|
-
queryKey: ["entity", id],
|
|
610
|
-
queryFn: () => {
|
|
611
|
-
const { data } = axios.get(\`.../\${id}\`);
|
|
612
|
-
return data;
|
|
613
|
-
}
|
|
614
|
-
});
|
|
615
|
-
`,
|
|
616
|
-
},
|
|
617
|
-
],
|
|
618
|
-
},
|
|
619
|
-
],
|
|
620
|
-
},
|
|
621
|
-
{
|
|
622
|
-
name: 'should fail when dep does not exist while having a complex queryKey',
|
|
623
|
-
code: normalizeIndent`
|
|
624
|
-
const todoQueries = {
|
|
625
|
-
key: (a, b, c, d, e) => ({
|
|
626
|
-
queryKey: ["entity", a, [b], { c }, 1, true],
|
|
627
|
-
queryFn: () => api.getEntity(a, b, c, d, e)
|
|
628
|
-
})
|
|
629
|
-
}
|
|
630
|
-
`,
|
|
631
|
-
errors: [
|
|
632
|
-
{
|
|
633
|
-
messageId: 'missingDeps',
|
|
634
|
-
data: { deps: 'd, e' },
|
|
635
|
-
suggestions: [
|
|
636
|
-
{
|
|
637
|
-
messageId: 'fixTo',
|
|
638
|
-
data: { result: '["entity", a, [b], { c }, 1, true, d, e]' },
|
|
639
|
-
output: normalizeIndent`
|
|
640
|
-
const todoQueries = {
|
|
641
|
-
key: (a, b, c, d, e) => ({
|
|
642
|
-
queryKey: ["entity", a, [b], { c }, 1, true, d, e],
|
|
643
|
-
queryFn: () => api.getEntity(a, b, c, d, e)
|
|
644
|
-
})
|
|
645
|
-
}
|
|
646
|
-
`,
|
|
647
|
-
},
|
|
648
|
-
],
|
|
649
|
-
},
|
|
650
|
-
],
|
|
651
|
-
},
|
|
652
|
-
{
|
|
653
|
-
name: 'should fail when dep does not exist while having a complex queryKey #2',
|
|
654
|
-
code: normalizeIndent`
|
|
655
|
-
const todoQueries = {
|
|
656
|
-
key: (dep1, dep2, dep3, dep4, dep5, dep6, dep7, dep8) => ({
|
|
657
|
-
queryKey: ['foo', {dep1, dep2: dep2, bar: dep3, baz: [dep4, dep5]}, [dep6, dep7]],
|
|
658
|
-
queryFn: () => api.getEntity(dep1, dep2, dep3, dep4, dep5, dep6, dep7, dep8),
|
|
659
|
-
}),
|
|
660
|
-
};
|
|
661
|
-
`,
|
|
662
|
-
errors: [
|
|
663
|
-
{
|
|
664
|
-
messageId: 'missingDeps',
|
|
665
|
-
data: { deps: 'dep8' },
|
|
666
|
-
suggestions: [
|
|
667
|
-
{
|
|
668
|
-
messageId: 'fixTo',
|
|
669
|
-
data: {
|
|
670
|
-
result:
|
|
671
|
-
"['foo', {dep1, dep2: dep2, bar: dep3, baz: [dep4, dep5]}, [dep6, dep7], dep8]",
|
|
672
|
-
},
|
|
673
|
-
output: normalizeIndent`
|
|
674
|
-
const todoQueries = {
|
|
675
|
-
key: (dep1, dep2, dep3, dep4, dep5, dep6, dep7, dep8) => ({
|
|
676
|
-
queryKey: ['foo', {dep1, dep2: dep2, bar: dep3, baz: [dep4, dep5]}, [dep6, dep7], dep8],
|
|
677
|
-
queryFn: () => api.getEntity(dep1, dep2, dep3, dep4, dep5, dep6, dep7, dep8),
|
|
678
|
-
}),
|
|
679
|
-
};
|
|
680
|
-
`,
|
|
681
|
-
},
|
|
682
|
-
],
|
|
683
|
-
},
|
|
684
|
-
],
|
|
685
|
-
},
|
|
686
|
-
{
|
|
687
|
-
name: 'should fail when two deps that depend on each other are missing',
|
|
688
|
-
code: normalizeIndent`
|
|
689
|
-
function Component({ map, key }) {
|
|
690
|
-
useQuery({ queryKey: ["key"], queryFn: () => api.get(map[key]) });
|
|
691
|
-
}
|
|
692
|
-
`,
|
|
693
|
-
errors: [
|
|
694
|
-
{
|
|
695
|
-
messageId: 'missingDeps',
|
|
696
|
-
data: { deps: 'map[key]' },
|
|
697
|
-
suggestions: [
|
|
698
|
-
{
|
|
699
|
-
messageId: 'fixTo',
|
|
700
|
-
data: {
|
|
701
|
-
result: '["key", map[key]]',
|
|
702
|
-
},
|
|
703
|
-
output: normalizeIndent`
|
|
704
|
-
function Component({ map, key }) {
|
|
705
|
-
useQuery({ queryKey: ["key", map[key]], queryFn: () => api.get(map[key]) });
|
|
706
|
-
}
|
|
707
|
-
`,
|
|
708
|
-
},
|
|
709
|
-
],
|
|
710
|
-
},
|
|
711
|
-
],
|
|
712
|
-
},
|
|
713
|
-
{
|
|
714
|
-
name: 'should fail when a queryKey is a reference of an array expression with a missing dep',
|
|
715
|
-
code: normalizeIndent`
|
|
716
|
-
const x = 5;
|
|
717
|
-
const queryKey = ['foo']
|
|
718
|
-
useQuery({ queryKey, queryFn: () => x })
|
|
719
|
-
`,
|
|
720
|
-
errors: [
|
|
721
|
-
{
|
|
722
|
-
messageId: 'missingDeps',
|
|
723
|
-
data: { deps: 'x' },
|
|
724
|
-
suggestions: [
|
|
725
|
-
{
|
|
726
|
-
messageId: 'fixTo',
|
|
727
|
-
data: {
|
|
728
|
-
result: "['foo', x]",
|
|
729
|
-
},
|
|
730
|
-
output: normalizeIndent`
|
|
731
|
-
const x = 5;
|
|
732
|
-
const queryKey = ['foo', x]
|
|
733
|
-
useQuery({ queryKey, queryFn: () => x })
|
|
734
|
-
`,
|
|
735
|
-
},
|
|
736
|
-
],
|
|
737
|
-
},
|
|
738
|
-
],
|
|
739
|
-
},
|
|
740
|
-
{
|
|
741
|
-
name: 'should fail when queryKey is a queryKeyFactory while having missing dep',
|
|
742
|
-
code: normalizeIndent`
|
|
743
|
-
const fooQueryKeyFactory = { foo: () => ['foo'] as const }
|
|
744
|
-
|
|
745
|
-
const useFoo = (num: number) =>
|
|
746
|
-
useQuery({
|
|
747
|
-
queryKey: fooQueryKeyFactory.foo(),
|
|
748
|
-
queryFn: () => Promise.resolve(num),
|
|
749
|
-
})
|
|
750
|
-
`,
|
|
751
|
-
errors: [
|
|
752
|
-
{
|
|
753
|
-
messageId: 'missingDeps',
|
|
754
|
-
data: { deps: 'num' },
|
|
755
|
-
},
|
|
756
|
-
],
|
|
757
|
-
},
|
|
758
|
-
{
|
|
759
|
-
name: 'should fail if queryFn is using multiple object props when only one of them is in the queryKey',
|
|
760
|
-
code: normalizeIndent`
|
|
761
|
-
const state = { foo: 'foo', bar: 'bar' }
|
|
762
|
-
|
|
763
|
-
useQuery({
|
|
764
|
-
queryKey: ['state', state.foo],
|
|
765
|
-
queryFn: () => Promise.resolve({ foo: state.foo, bar: state.bar })
|
|
766
|
-
})
|
|
767
|
-
`,
|
|
768
|
-
errors: [
|
|
769
|
-
{
|
|
770
|
-
suggestions: [
|
|
771
|
-
{
|
|
772
|
-
messageId: 'fixTo',
|
|
773
|
-
output: normalizeIndent`
|
|
774
|
-
const state = { foo: 'foo', bar: 'bar' }
|
|
775
|
-
|
|
776
|
-
useQuery({
|
|
777
|
-
queryKey: ['state', state.foo, state.bar],
|
|
778
|
-
queryFn: () => Promise.resolve({ foo: state.foo, bar: state.bar })
|
|
779
|
-
})
|
|
780
|
-
`,
|
|
781
|
-
},
|
|
782
|
-
],
|
|
783
|
-
messageId: 'missingDeps',
|
|
784
|
-
data: { deps: 'state.bar' },
|
|
785
|
-
},
|
|
786
|
-
],
|
|
787
|
-
},
|
|
788
|
-
{
|
|
789
|
-
name: 'should fail if queryFn is invalid while using FunctionExpression syntax',
|
|
790
|
-
code: normalizeIndent`
|
|
791
|
-
const id = 1;
|
|
792
|
-
|
|
793
|
-
useQuery({
|
|
794
|
-
queryKey: [],
|
|
795
|
-
queryFn() {
|
|
796
|
-
Promise.resolve(id)
|
|
797
|
-
}
|
|
798
|
-
})
|
|
799
|
-
`,
|
|
800
|
-
errors: [
|
|
801
|
-
{
|
|
802
|
-
suggestions: [
|
|
803
|
-
{
|
|
804
|
-
messageId: 'fixTo',
|
|
805
|
-
output: normalizeIndent`
|
|
806
|
-
const id = 1;
|
|
807
|
-
|
|
808
|
-
useQuery({
|
|
809
|
-
queryKey: [id],
|
|
810
|
-
queryFn() {
|
|
811
|
-
Promise.resolve(id)
|
|
812
|
-
}
|
|
813
|
-
})
|
|
814
|
-
`,
|
|
815
|
-
},
|
|
816
|
-
],
|
|
817
|
-
messageId: 'missingDeps',
|
|
818
|
-
data: { deps: 'id' },
|
|
819
|
-
},
|
|
820
|
-
],
|
|
821
|
-
},
|
|
822
|
-
{
|
|
823
|
-
name: 'should fail if queryFn is a ternary expression with missing dep and a skipToken',
|
|
824
|
-
code: normalizeIndent`
|
|
825
|
-
import { useQuery, skipToken } from "@tanstack/react-query";
|
|
826
|
-
const fetch = true
|
|
827
|
-
|
|
828
|
-
function Component({ id }) {
|
|
829
|
-
useQuery({
|
|
830
|
-
queryKey: [],
|
|
831
|
-
queryFn: fetch ? () => Promise.resolve(id) : skipToken
|
|
832
|
-
})
|
|
833
|
-
}
|
|
834
|
-
`,
|
|
835
|
-
errors: [
|
|
836
|
-
{
|
|
837
|
-
suggestions: [
|
|
838
|
-
{
|
|
839
|
-
messageId: 'fixTo',
|
|
840
|
-
output: normalizeIndent`
|
|
841
|
-
import { useQuery, skipToken } from "@tanstack/react-query";
|
|
842
|
-
const fetch = true
|
|
843
|
-
|
|
844
|
-
function Component({ id }) {
|
|
845
|
-
useQuery({
|
|
846
|
-
queryKey: [id],
|
|
847
|
-
queryFn: fetch ? () => Promise.resolve(id) : skipToken
|
|
848
|
-
})
|
|
849
|
-
}
|
|
850
|
-
`,
|
|
851
|
-
},
|
|
852
|
-
],
|
|
853
|
-
messageId: 'missingDeps',
|
|
854
|
-
data: { deps: 'id' },
|
|
855
|
-
},
|
|
856
|
-
],
|
|
857
|
-
},
|
|
858
|
-
],
|
|
859
|
-
})
|