Skip to content

Commit d4c9f42

Browse files
feat: placeholderData codemod (#6196)
* feat(codemod): add initial implementation of the `placeholderData` codemod * chore(codemod): remove the parameter from the `placeholderData` function examples It was a request by @TkDodo, since in `v4` the `placeholderData` function didn't have a parameter. For further details see: #6196 (comment) * chore(codemod): add an example where the `placeholderData` property is not a function It was a request by @TkDodo, see: #6196 (comment) * refactor(codemod): transform the usage only when the `keepPreviousData` has value `true` --------- Co-authored-by: Dominik Dorfmeister <[email protected]>
1 parent 2760f75 commit d4c9f42

File tree

7 files changed

+424
-0
lines changed

7 files changed

+424
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import * as React from 'react'
2+
import axios from 'axios'
3+
import { useQueries, useQuery } from '@tanstack/react-query'
4+
5+
type Post = {
6+
id: number
7+
title: string
8+
body: string
9+
}
10+
11+
const queryFn = async (): Promise<Array<Post>> => {
12+
const { data } = await axios.get(
13+
'https://jsonplaceholder.typicode.com/posts',
14+
)
15+
return data
16+
}
17+
18+
/**
19+
* The object expression has a `keepPreviousData` property with value `true`, so the codemod should transform
20+
* this usage.
21+
*/
22+
export const Example1 = () => {
23+
const { data } = useQuery({
24+
queryKey: ['posts'],
25+
queryFn: queryFn,
26+
keepPreviousData: true,
27+
})
28+
29+
return <div>{JSON.stringify(data)}</div>
30+
}
31+
32+
/**
33+
* The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a function.
34+
* The codemod shouldn't transform this case, only warn the user about the manual migration.
35+
*/
36+
export const Example2 = () => {
37+
const { data } = useQueries({
38+
queryKey: ['posts'],
39+
queryFn: queryFn,
40+
keepPreviousData: true,
41+
placeholderData: () => previousData
42+
})
43+
44+
return <div>{JSON.stringify(data)}</div>
45+
}
46+
47+
/**
48+
* The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a string.
49+
* The codemod shouldn't transform this case, only warn the user about the manual migration.
50+
*/
51+
export const Example3 = () => {
52+
const { data } = useQueries({
53+
queryKey: ['posts'],
54+
queryFn: queryFn,
55+
keepPreviousData: true,
56+
placeholderData: "somePlaceholderData"
57+
})
58+
59+
return <div>{JSON.stringify(data)}</div>
60+
}
61+
62+
/**
63+
* The object expression has a `keepPreviousData` property with value `false`.
64+
* The codemod shouldn't transform this case, only warn the user about the manual migration.
65+
*/
66+
export const Example4 = () => {
67+
const { data } = useQueries({
68+
queryKey: ['posts'],
69+
queryFn: queryFn,
70+
keepPreviousData: false,
71+
})
72+
73+
return <div>{JSON.stringify(data)}</div>
74+
}
75+
76+
/**
77+
* The object expression has a `keepPreviousData` property with which is an identifier.
78+
* The codemod shouldn't transform this case, only warn the user about the manual migration.
79+
*/
80+
export const Example5 = () => {
81+
const keepPreviousDataIdentifier = false
82+
const { data } = useQueries({
83+
queryKey: ['posts'],
84+
queryFn: queryFn,
85+
keepPreviousData: keepPreviousDataIdentifier,
86+
placeholderData: () => previousData
87+
})
88+
89+
return <div>{JSON.stringify(data)}</div>
90+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import * as React from 'react'
2+
import axios from 'axios'
3+
import { keepPreviousData, useQueries, useQuery } from '@tanstack/react-query';
4+
5+
type Post = {
6+
id: number
7+
title: string
8+
body: string
9+
}
10+
11+
const queryFn = async (): Promise<Array<Post>> => {
12+
const { data } = await axios.get(
13+
'https://jsonplaceholder.typicode.com/posts',
14+
)
15+
return data
16+
}
17+
18+
/**
19+
* The object expression has a `keepPreviousData` property with value `true`, so the codemod should transform
20+
* this usage.
21+
*/
22+
export const Example1 = () => {
23+
const { data } = useQuery({
24+
queryKey: ['posts'],
25+
queryFn: queryFn,
26+
placeholderData: keepPreviousData
27+
})
28+
29+
return <div>{JSON.stringify(data)}</div>
30+
}
31+
32+
/**
33+
* The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a function.
34+
* The codemod shouldn't transform this case, only warn the user about the manual migration.
35+
*/
36+
export const Example2 = () => {
37+
const { data } = useQueries({
38+
queryKey: ['posts'],
39+
queryFn: queryFn,
40+
keepPreviousData: true,
41+
placeholderData: () => previousData
42+
})
43+
44+
return <div>{JSON.stringify(data)}</div>
45+
}
46+
47+
/**
48+
* The object expression has a `keepPreviousData` property with value `true`, but the `placeholderData` is a string.
49+
* The codemod shouldn't transform this case, only warn the user about the manual migration.
50+
*/
51+
export const Example3 = () => {
52+
const { data } = useQueries({
53+
queryKey: ['posts'],
54+
queryFn: queryFn,
55+
keepPreviousData: true,
56+
placeholderData: "somePlaceholderData"
57+
})
58+
59+
return <div>{JSON.stringify(data)}</div>
60+
}
61+
62+
/**
63+
* The object expression has a `keepPreviousData` property with value `false`.
64+
* The codemod shouldn't transform this case, only warn the user about the manual migration.
65+
*/
66+
export const Example4 = () => {
67+
const { data } = useQueries({
68+
queryKey: ['posts'],
69+
queryFn: queryFn,
70+
keepPreviousData: false,
71+
})
72+
73+
return <div>{JSON.stringify(data)}</div>
74+
}
75+
76+
/**
77+
* The object expression has a `keepPreviousData` property with which is an identifier.
78+
* The codemod shouldn't transform this case, only warn the user about the manual migration.
79+
*/
80+
export const Example5 = () => {
81+
const keepPreviousDataIdentifier = false
82+
const { data } = useQueries({
83+
queryKey: ['posts'],
84+
queryFn: queryFn,
85+
keepPreviousData: keepPreviousDataIdentifier,
86+
placeholderData: () => previousData
87+
})
88+
89+
return <div>{JSON.stringify(data)}</div>
90+
}

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

Whitespace-only changes.

packages/codemods/src/v5/keep-previous-data/__testfixtures__/named.output.tsx

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// eslint-disable-next-line @typescript-eslint/no-var-requires
2+
const defineTest = require('jscodeshift/dist/testUtils').defineTest
3+
4+
defineTest(__dirname, 'keep-previous-data', null, 'default', {
5+
parser: 'tsx',
6+
})
7+
8+
defineTest(__dirname, 'keep-previous-data', null, 'named', {
9+
parser: 'tsx',
10+
})

0 commit comments

Comments
 (0)