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