Open History to see the last commit
1// eslint-disable-next-line @typescript-eslint/no-var-requires
2const createUtilsObject = require('../utils')
3// eslint-disable-next-line @typescript-eslint/no-var-requires
4const createKeyReplacer = require('./utils/replacers/key-replacer')
5// eslint-disable-next-line @typescript-eslint/no-var-requires
6const createUseQueryLikeTransformer = require('../utils/transformers/use-query-like-transformer')
7// eslint-disable-next-line @typescript-eslint/no-var-requires
8const createQueryClientTransformer = require('../utils/transformers/query-client-transformer')
9// eslint-disable-next-line @typescript-eslint/no-var-requires
10const createQueryCacheTransformer = require('../utils/transformers/query-cache-transformer')
11β
12const transformQueryClientUsages = ({
13 jscodeshift,
14 utils,
15 root,
16 filePath,
17 packageName,
18}) => {
19 const transformer = createQueryClientTransformer({
20 jscodeshift,
21 utils,
22 root,
23 packageName,
24 })
25 const replacer = createKeyReplacer({ jscodeshift, root, filePath })
26β
27 transformer.execute(
28 [
29 // Not object syntax-aware methods.
30 'getMutationDefaults',
31 'getQueriesData',
32 'getQueryData',
33 'getQueryDefaults',
34 'getQueryState',
35 'isFetching',
36 'setMutationDefaults',
37 'setQueriesData',
38 'setQueryData',
39 'setQueryDefaults',
40 // Object syntax-aware methods.
41 'cancelQueries',
42 'fetchInfiniteQuery',
43 'fetchQuery',
44 'invalidateQueries',
45 'prefetchInfiniteQuery',
46 'prefetchQuery',
47 'refetchQueries',
48 'removeQueries',
49 'resetQueries',
50 ],
51 replacer,
52 )
53}
54β
55const transformUseQueriesUsages = ({
56 jscodeshift,
57 utils,
58 root,
59 packageName,
60}) => {
61 const transformer = createUseQueryLikeTransformer({
62 jscodeshift,
63 utils,
64 root,
65 packageName,
66 })
67 const replacer = ({ node }) => {
68 /**
69 * When the node doesn't have the 'original' property, that means the codemod has been already applied,
70 * so we don't need to do any changes.
71 */
72 if (!node.original) {
73 return node
74 }
75β
76 const newCallExpression = jscodeshift.callExpression(node.original.callee, [
77 jscodeshift.objectExpression([
78 jscodeshift.property(
79 'init',
80 jscodeshift.identifier('queries'),
81 node.original.arguments[0],
82 ),
83 ]),
84 ])
85β
86 // TODO: This should be part of one function!
87 if (node.typeParameters) {
88 newCallExpression.typeArguments = node.typeParameters
89 }
90β
91 return newCallExpression
92 }
93β
94 transformer.execute(['useQueries'], replacer)
95}
96β
97const transformUseQueryLikeUsages = ({
98 jscodeshift,
99 utils,
100 root,
101 filePath,
102 packageName,
103}) => {
104 const transformer = createUseQueryLikeTransformer({
105 jscodeshift,
106 utils,
107 root,
108 packageName,
109 })
110β
111 transformer.execute(
112 ['useQuery', 'useInfiniteQuery', 'useIsFetching', 'useIsMutating'],
113 createKeyReplacer({
114 jscodeshift,
115 root,
116 filePath,
117 keyName: 'queryKey',
118 }),
119 )
120 transformer.execute(
121 ['useMutation'],
122 createKeyReplacer({
123 jscodeshift,
124 root,
125 filePath,
126 keyName: 'mutationKey',
127 }),
128 )
129}
130β
131const transformQueryCacheUsages = ({
132 jscodeshift,
133 utils,
134 root,
135 filePath,
136 packageName,
137}) => {
138 const transformer = createQueryCacheTransformer({
139 jscodeshift,
140 utils,
141 root,
142 packageName,
143 })
144 const replacer = createKeyReplacer({ jscodeshift, root, filePath })
145β
146 transformer.execute(replacer)
147}
148β
149module.exports = (file, api) => {
150 const jscodeshift = api.jscodeshift
151 const root = jscodeshift(file.source)
152β
153 // TODO: Execute the transformers only when it contains a `react-query` import!
154β
155 const utils = createUtilsObject({ root, jscodeshift })
156 const filePath = file.path
157 const packageName = 'react-query'
158β
159 // This function transforms usages like `useQuery` and `useMutation`.
160 transformUseQueryLikeUsages({
161 jscodeshift,
162 utils,
163 root,
164 filePath,
165 packageName,
166 })
167 // This function transforms usages of `useQueries`.
168 transformUseQueriesUsages({
169 jscodeshift,
170 utils,
171 root,
172 packageName,
173 })
174 // This function transforms usages of `QueryClient`.
175 transformQueryClientUsages({
176 jscodeshift,
177 utils,
178 root,
179 filePath,
180 packageName,
181 })
182 // This function transforms usages of `QueryCache`.
183 transformQueryCacheUsages({ jscodeshift, utils, root, filePath, packageName })
184β
185 return root.toSource({ quote: 'single', lineTerminator: '\n' })
186}