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