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

is-loading.cjs

244 lines6.8 KB

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