/
query-fn-aware-usage-transformer.cjs
188 lines (154 loc) 路 5.27 KB
/
query-fn-aware-usage-transformer.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
// eslint-disable-next-line @typescript-eslint/no-var-requires
const createV5UtilsObject = require('../utils/index.cjs')
// eslint-disable-next-line @typescript-eslint/no-var-requires
const UnknownUsageError = require('../utils/unknown-usage-error.cjs')
// eslint-disable-next-line @typescript-eslint/no-var-requires
const createQueryClientTransformer = require('../../../utils/transformers/query-client-transformer.cjs')
/**
* @param {import('jscodeshift').api} jscodeshift
* @param {Object} utils
* @param {import('jscodeshift').Collection} root
* @param {string} filePath
* @param {{keyName: "mutationKey"|"queryKey", queryClientMethods: ReadonlyArray<string>, hooks: ReadonlyArray<string>}} config
*/
const transformQueryFnAwareUsages = ({
jscodeshift,
utils,
root,
filePath,
config,
}) => {
const v5Utils = createV5UtilsObject({ jscodeshift, utils })
/**
* @param {import('jscodeshift').CallExpression} node
* @returns {boolean}
*/
const canSkipReplacement = (node) => {
const callArguments = node.arguments
const hasKeyProperty = () =>
callArguments[0].properties.some(
(property) =>
utils.isObjectProperty(property) &&
[config.keyName, 'queryFn'].includes(property.key.name),
)
return (
callArguments.length > 0 &&
utils.isObjectExpression(callArguments[0]) &&
hasKeyProperty()
)
}
const predicate = (property) => {
const isSpreadElement = utils.isSpreadElement(property)
const isObjectProperty = utils.isObjectProperty(property)
return (
isSpreadElement ||
(isObjectProperty && property.key.name !== config.keyName)
)
}
const transformArgumentToQueryFunction = (path, node) => {
const isIdentifier = utils.isIdentifier(node)
const isFunctionDefinition = utils.isFunctionDefinition(node)
if (!isIdentifier && !isFunctionDefinition) {
return undefined
}
if (isFunctionDefinition) {
return jscodeshift.property(
'init',
jscodeshift.identifier('queryFn'),
node,
)
}
const binding = v5Utils.getBindingFromScope(path, node.name, filePath)
const initializer = v5Utils.getInitializerByDeclarator(binding)
if (!utils.isFunctionDefinition(initializer)) {
return undefined
}
return jscodeshift.property(
'init',
jscodeshift.identifier('queryFn'),
binding.id,
)
}
const transformArgumentToOptionsObject = (path, node) => {
if (!utils.isIdentifier(node)) {
return undefined
}
const binding = v5Utils.getBindingFromScope(path, node.name, filePath)
const initializer = v5Utils.getInitializerByDeclarator(binding)
if (utils.isObjectExpression(initializer)) {
return jscodeshift.spreadElement(binding.id)
}
}
const replacer = (path) => {
const node = path.node
try {
// If the given method/function call matches certain criteria, the node doesn't need to be replaced, this step can be skipped.
if (canSkipReplacement(node)) {
return node
}
const keyProperty = v5Utils.transformArgumentToKey(
path,
node.arguments[0],
config.keyName,
filePath,
)
if (!keyProperty) {
throw new UnknownUsageError(node, filePath)
}
const parameters = [jscodeshift.objectExpression([keyProperty])]
const createdObjectExpression = parameters[0]
const secondParameter = node.arguments[1]
if (secondParameter) {
const queryFnProperty = transformArgumentToQueryFunction(
path,
secondParameter,
)
if (queryFnProperty) {
createdObjectExpression.properties.push(queryFnProperty)
const thirdParameter = node.arguments[2]
if (utils.isObjectExpression(thirdParameter)) {
v5Utils.copyPropertiesFromSource(
thirdParameter,
createdObjectExpression,
predicate,
)
} else {
createdObjectExpression.properties.push(
jscodeshift.spreadElement(thirdParameter),
)
}
return jscodeshift.callExpression(node.original.callee, parameters)
}
const optionsProperty = transformArgumentToOptionsObject(
path,
secondParameter,
)
if (optionsProperty) {
createdObjectExpression.properties.push(optionsProperty)
return jscodeshift.callExpression(node.original.callee, parameters)
}
if (utils.isObjectExpression(secondParameter)) {
v5Utils.copyPropertiesFromSource(
secondParameter,
createdObjectExpression,
predicate,
)
}
return jscodeshift.callExpression(node.original.callee, parameters)
}
return jscodeshift.callExpression(node.original.callee, parameters)
} catch (error) {
utils.warn(
error.name === UnknownUsageError.name
? error.message
: `An unknown error occurred while processing the "${filePath}" file. Please review this file, because the codemod couldn't be applied.`,
)
return node
}
}
createQueryClientTransformer({ jscodeshift, utils, root }).execute(
config.queryClientMethods,
replacer,
)
}
module.exports = transformQueryFnAwareUsages