Skip to content

Commit b3c7dba

Browse files
fix(codemod): improve the keep previous data codemod (#6283)
* chore(codemod): add named examples to the `keep-previous-data` codemod, because these were missing * chore(codemod): remove the unnecessary `return` statement * feat(codemod): cover the `setQueryDefaults` method usages on the `QueryClient` instance with the codemod For further details see: #6196 (comment) * chore(codemod): rename the `isKeepPreviousDataInUse` to `shouldAddKeepPreviousDataImport` The new name describes better the purpose of this variable. * feat(codemod): cover the `QueryClient` class instantiations with the codemod For further details see: #6196 (comment) * chore(codemod): fix `eslint` issues within the `keep-previous-data` codemod * chore(codemod): fix the `prettier` issues within the `keep-previous-data` codemod README file * chore(codemod): turn off the `sort-imports` ESLint rule in case of the codemods Since the codemods might not respect the import order, it's easier to turn off the `sort-imports` ESLint rule. --------- Co-authored-by: Dominik Dorfmeister <[email protected]>
1 parent 41e0938 commit b3c7dba

File tree

7 files changed

+1097
-30
lines changed

7 files changed

+1097
-30
lines changed

packages/codemods/.eslintrc.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const config = {
1111
files: ['**/__testfixtures__/*'],
1212
rules: {
1313
'import/no-unresolved': 'off',
14+
'sort-imports': 'off',
1415
'@typescript-eslint/no-unused-vars': 'off',
1516
},
1617
},
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
### Intro
2+
3+
The prerequisite for this code mod is to migrate your usages to the new syntax, so overloads for hooks and `QueryClient` methods shouldn't be available anymore.
4+
5+
### Affected usages
6+
7+
Please note, this code mod transforms usages only where the first argument is an object expression.
8+
9+
The following usage should be transformed by the code mod:
10+
11+
```ts
12+
const { data } = useQuery({
13+
queryKey: ['posts'],
14+
queryFn: queryFn,
15+
keepPreviousData: true,
16+
})
17+
```
18+
19+
But the following usage won't be transformed by the code mod, because the first argument an identifier:
20+
21+
```ts
22+
const hookArgument = {
23+
queryKey: ['posts'],
24+
queryFn: queryFn,
25+
keepPreviousData: true,
26+
}
27+
const { data } = useQuery(hookArgument)
28+
```
29+
30+
### Troubleshooting
31+
32+
In case of any errors, feel free to reach us out via Discord or open an issue. If you open an issue, please provide a code snippet as well, because without a snippet we cannot find the bug in the code mod.

packages/codemods/src/v5/keep-previous-data/__testfixtures__/default.input.tsx

Lines changed: 196 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react'
22
import axios from 'axios'
3-
import { useQueries, useQuery } from '@tanstack/react-query'
3+
import { QueryClient, useQueries, useQuery, useQueryClient } from '@tanstack/react-query'
44

55
type Post = {
66
id: number
@@ -53,7 +53,7 @@ export const Example3 = () => {
5353
queryKey: ['posts'],
5454
queryFn: queryFn,
5555
keepPreviousData: true,
56-
placeholderData: "somePlaceholderData"
56+
placeholderData: 'somePlaceholderData'
5757
})
5858

5959
return <div>{JSON.stringify(data)}</div>
@@ -88,3 +88,197 @@ export const Example5 = () => {
8888

8989
return <div>{JSON.stringify(data)}</div>
9090
}
91+
92+
/**
93+
* The object expression has a `keepPreviousData` property with value `true`, so the codemod should transform
94+
* this usage.
95+
*/
96+
export const Example6 = () => {
97+
const queryClient = new QueryClient()
98+
99+
queryClient.setQueryDefaults(['key'], {
100+
keepPreviousData: true
101+
})
102+
103+
useQueryClient().setQueryDefaults(['key'], {
104+
keepPreviousData: true
105+
})
106+
107+
const anotherQueryClient = useQueryClient()
108+
109+
anotherQueryClient.setQueryDefaults(['key'], {
110+
keepPreviousData: true
111+
})
112+
}
113+
114+
/**
115+
* The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a function.
116+
* The codemod shouldn't transform this case, only warn the user about the manual migration.
117+
*/
118+
export const Example7 = () => {
119+
const queryClient = new QueryClient()
120+
121+
queryClient.setQueryDefaults(['key'], {
122+
keepPreviousData: true,
123+
placeholderData: () => previousData
124+
})
125+
126+
useQueryClient().setQueryDefaults(['key'], {
127+
keepPreviousData: true,
128+
placeholderData: () => previousData
129+
})
130+
131+
const anotherQueryClient = useQueryClient()
132+
133+
anotherQueryClient.setQueryDefaults(['key'], {
134+
keepPreviousData: true,
135+
placeholderData: () => previousData
136+
})
137+
}
138+
139+
/**
140+
* The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a string.
141+
* The codemod shouldn't transform this case, only warn the user about the manual migration.
142+
*/
143+
export const Example8 = () => {
144+
const queryClient = new QueryClient()
145+
146+
queryClient.setQueryDefaults(['key'], {
147+
keepPreviousData: true,
148+
placeholderData: 'somePlaceholderData'
149+
})
150+
151+
useQueryClient().setQueryDefaults(['key'], {
152+
keepPreviousData: true,
153+
placeholderData: 'somePlaceholderData'
154+
})
155+
156+
const anotherQueryClient = useQueryClient()
157+
158+
anotherQueryClient.setQueryDefaults(['key'], {
159+
keepPreviousData: true,
160+
placeholderData: 'somePlaceholderData'
161+
})
162+
}
163+
164+
/**
165+
* The object expression has a `keepPreviousData` property with value `false`.
166+
* The codemod shouldn't transform this case, only warn the user about the manual migration.
167+
*/
168+
export const Example9 = () => {
169+
const queryClient = new QueryClient()
170+
171+
queryClient.setQueryDefaults(['key'], {
172+
keepPreviousData: false,
173+
})
174+
175+
useQueryClient().setQueryDefaults(['key'], {
176+
keepPreviousData: false,
177+
})
178+
179+
const anotherQueryClient = useQueryClient()
180+
181+
anotherQueryClient.setQueryDefaults(['key'], {
182+
keepPreviousData: false,
183+
})
184+
}
185+
186+
/**
187+
* The object expression has a `keepPreviousData` property with which is an identifier.
188+
* The codemod shouldn't transform this case, only warn the user about the manual migration.
189+
*/
190+
export const Example10 = () => {
191+
const queryClient = new QueryClient()
192+
const keepPreviousDataIdentifier = false
193+
194+
queryClient.setQueryDefaults(['key'], {
195+
keepPreviousData: keepPreviousDataIdentifier,
196+
placeholderData: () => previousData
197+
})
198+
199+
useQueryClient().setQueryDefaults(['key'], {
200+
keepPreviousData: keepPreviousDataIdentifier,
201+
placeholderData: () => previousData
202+
})
203+
204+
const anotherQueryClient = useQueryClient()
205+
206+
anotherQueryClient.setQueryDefaults(['key'], {
207+
keepPreviousData: keepPreviousDataIdentifier,
208+
placeholderData: () => previousData
209+
})
210+
}
211+
212+
/**
213+
* The object expression has a `keepPreviousData` property with value `true`, so the codemod should transform
214+
* this usage.
215+
*/
216+
export const Example11 = () => {
217+
new QueryClient({
218+
defaultOptions: {
219+
queries: {
220+
keepPreviousData: true
221+
}
222+
}
223+
})
224+
}
225+
226+
/**
227+
* The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a function.
228+
* The codemod shouldn't transform this case, only warn the user about the manual migration.
229+
*/
230+
export const Example12 = () => {
231+
new QueryClient({
232+
defaultOptions: {
233+
queries: {
234+
keepPreviousData: true,
235+
placeholderData: () => previousData
236+
}
237+
}
238+
})
239+
}
240+
241+
/**
242+
* The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a string.
243+
* The codemod shouldn't transform this case, only warn the user about the manual migration.
244+
*/
245+
export const Example13 = () => {
246+
new QueryClient({
247+
defaultOptions: {
248+
queries: {
249+
keepPreviousData: true,
250+
placeholderData: 'somePlaceholderData'
251+
}
252+
}
253+
})
254+
}
255+
256+
/**
257+
* The object expression has a `keepPreviousData` property with value `false`.
258+
* The codemod shouldn't transform this case, only warn the user about the manual migration.
259+
*/
260+
export const Example14 = () => {
261+
new QueryClient({
262+
defaultOptions: {
263+
queries: {
264+
keepPreviousData: false,
265+
}
266+
}
267+
})
268+
}
269+
270+
/**
271+
* The object expression has a `keepPreviousData` property with which is an identifier.
272+
* The codemod shouldn't transform this case, only warn the user about the manual migration.
273+
*/
274+
export const Example15 = () => {
275+
const keepPreviousDataIdentifier = false
276+
new QueryClient({
277+
defaultOptions: {
278+
queries: {
279+
keepPreviousData: keepPreviousDataIdentifier,
280+
placeholderData: () => previousData
281+
}
282+
}
283+
})
284+
}

0 commit comments

Comments
 (0)