Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

is-loading.js

247 lines7.0 KB

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 createUseQueryLikeTransformer = require('../../utils/transformers/use-query-like-transformer')
5// eslint-disable-next-line @typescript-eslint/no-var-requires
6const createQueryClientTransformer = require('../../utils/transformers/query-client-transformer')
7​
8const originalName = 'isLoading'
9const newName = 'isPending'
10​
11/**
12 * @param {import('jscodeshift')} jscodeshift
13 * @param {Object} utils
14 * @param {import('jscodeshift').Collection} root
15 * @param {string} filePath
16 * @param {{keyName: "mutationKey"|"queryKey", queryClientMethods: ReadonlyArray<string>, hooks: ReadonlyArray<string>}} config
17 */
18const transformUsages = ({ jscodeshift, utils, root, filePath, config }) => {
19 /**
20 * @param {import('jscodeshift').CallExpression | import('jscodeshift').ExpressionStatement} node
21 * @returns {{start: number, end: number}}
22 */
23 const getNodeLocation = (node) => {
24 const location = utils.isCallExpression(node) ? node.callee.loc : node.loc
25 const start = location.start.line
26 const end = location.end.line
27​
28 return { start, end }
29 }
30​
31 /**
32 * @param {import('jscodeshift').ASTNode} node
33 * @returns {boolean}
34 */
35 const isObjectExpression = (node) => {
36 return jscodeshift.match(node, {
37 type: jscodeshift.ObjectExpression.name,
38 })
39 }
40​
41 /**
42 * @param {import('jscodeshift').ASTNode} node
43 * @returns {boolean}
44 */
45 const isObjectPattern = (node) => {
46 return jscodeshift.match(node, {
47 type: jscodeshift.ObjectPattern.name,
48 })
49 }
50​
51 /**
52 * @param {import('jscodeshift').ASTNode} node
53 * @returns {boolean}
54 */
55 const isVariableDeclarator = (node) => {
56 return jscodeshift.match(node, {
57 type: jscodeshift.VariableDeclarator.name,
58 })
59 }
60​
61 /**
62 * @param {import('jscodeshift').Node} node
63 * @param {import('jscodeshift').Identifier} identifier
64 * @returns {Collection<import('jscodeshift').MemberExpression>}
65 */
66 const findIsLoadingPropertiesOfIdentifier = (node, identifier) => {
67 return jscodeshift(node).find(jscodeshift.MemberExpression, {
68 object: {
69 type: jscodeshift.Identifier.name,
70 name: identifier.name,
71 },
72 property: {
73 type: jscodeshift.Identifier.name,
74 name: originalName,
75 },
76 })
77 }
78​
79 /**
80 * @param {import('jscodeshift').ObjectPattern} node
81 * @returns {import('jscodeshift').ObjectProperty|null}
82 */
83 const findIsLoadingObjectPropertyInObjectPattern = (node) => {
84 return (
85 node.properties.find((property) =>
86 jscodeshift.match(property, {
87 key: {
88 type: jscodeshift.Identifier.name,
89 name: originalName,
90 },
91 }),
92 ) ?? null
93 )
94 }
95​
96 /**
97 * @param {import('jscodeshift').ObjectPattern} node
98 * @returns {import('jscodeshift').RestElement|null}
99 */
100 const findRestElementInObjectPattern = (node) => {
101 return (
102 node.properties.find((property) =>
103 jscodeshift.match(property, {
104 type: jscodeshift.RestElement.name,
105 }),
106 ) ?? null
107 )
108 }
109​
110 const replacer = (path, transformNode) => {
111 const node = path.node
112 const parentNode = path.parentPath.value
113 const { start, end } = getNodeLocation(node)
114​
115 try {
116 if (!isVariableDeclarator(parentNode)) {
117 // The parent node is not a variable declarator, the transformation will be skipped.
118 return node
119 }
120​
121 const lookupNode = path.scope.node
122 const variableDeclaratorId = parentNode.id
123​
124 if (isObjectPattern(variableDeclaratorId)) {
125 const isLoadingObjectProperty =
126 findIsLoadingObjectPropertyInObjectPattern(variableDeclaratorId)
127​
128 if (isLoadingObjectProperty) {
129 jscodeshift(lookupNode)
130 .find(jscodeshift.ObjectProperty, {
131 key: {
132 type: jscodeshift.Identifier.name,
133 name: originalName,
134 },
135 })
136 .replaceWith((mutablePath) => {
137 if (isObjectPattern(mutablePath.parent)) {
138 const affectedProperty = mutablePath.value.value.shorthand
139 ? 'value'
140 : 'key'
141​
142 mutablePath.value[affectedProperty].name = newName
143​
144 return mutablePath.value
145 }
146​
147 if (isObjectExpression(mutablePath.parent)) {
148 const affectedProperty = mutablePath.value.value.shorthand
149 ? 'key'
150 : 'value'
151​
152 mutablePath.value[affectedProperty].name = newName
153​
154 return mutablePath.value
155 }
156​
157 return mutablePath.value
158 })
159​
160 // Renaming all other 'isLoading' references that are object properties.
161 jscodeshift(lookupNode)
162 .find(jscodeshift.Identifier, { name: originalName })
163 .replaceWith((mutablePath) => {
164 if (
165 !jscodeshift.match(mutablePath.parent, {
166 type: jscodeshift.ObjectProperty.name,
167 })
168 ) {
169 mutablePath.value.name = newName
170 }
171​
172 return mutablePath.value
173 })
174 }
175​
176 const restElement = findRestElementInObjectPattern(variableDeclaratorId)
177​
178 if (restElement) {
179 findIsLoadingPropertiesOfIdentifier(
180 lookupNode,
181 restElement.argument,
182 ).replaceWith(({ node: mutableNode }) => {
183 mutableNode.property.name = newName
184​
185 return mutableNode
186 })
187 }
188​
189 return node
190 }
191​
192 if (utils.isIdentifier(variableDeclaratorId)) {
193 findIsLoadingPropertiesOfIdentifier(
194 lookupNode,
195 variableDeclaratorId,
196 ).replaceWith(({ node: mutableNode }) => {
197 mutableNode.property.name = newName
198​
199 return mutableNode
200 })
201​
202 return node
203 }
204​
205 utils.warn(
206 `The usage in file "${filePath}" at line ${start}:${end} could not be transformed. Please migrate this usage manually.`,
207 )
208​
209 return node
210 } catch (error) {
211 utils.warn(
212 `An unknown error occurred while processing the "${filePath}" file. Please review this file, because the codemod couldn't be applied.`,
213 )
214​
215 return node
216 }
217 }
218​
219 createUseQueryLikeTransformer({ jscodeshift, utils, root }).execute(
220 config.hooks,
221 replacer,
222 )
223​
224 createQueryClientTransformer({ jscodeshift, utils, root }).execute(
225 config.queryClientMethods,
226 replacer,
227 )
229​
230module.exports = (file, api) => {
231 const jscodeshift = api.jscodeshift
232 const root = jscodeshift(file.source)
233 const utils = createUtilsObject({ root, jscodeshift })
234 const filePath = file.path
235​
236 const dependencies = { jscodeshift, utils, root, filePath }
237​
238 transformUsages({
239 ...dependencies,
240 config: {
241 hooks: ['useQuery', 'useMutation'],
242 queryClientMethods: [],
243 },
244 })
245​
246 return root.toSource({ quote: 'single', lineTerminator: '\n' })