Open History to see the last commit
1const createV5UtilsObject = require('../utils/index.cjs')
2const UnknownUsageError = require('../utils/unknown-usage-error.cjs')
3const createQueryClientTransformer = require('../../../utils/transformers/query-client-transformer.cjs')
4β
5/**
6 * @param {import('jscodeshift').api} jscodeshift
7 * @param {Object} utils
8 * @param {import('jscodeshift').Collection} root
9 * @param {string} filePath
10 * @param {{keyName: "mutationKey"|"queryKey", queryClientMethods: ReadonlyArray<string>, hooks: ReadonlyArray<string>}} config
11 */
12const transformQueryFnAwareUsages = ({
13 jscodeshift,
14 utils,
15 root,
16 filePath,
17 config,
18}) => {
19 const v5Utils = createV5UtilsObject({ jscodeshift, utils })
20β
21 /**
22 * @param {import('jscodeshift').CallExpression} node
23 * @returns {boolean}
24 */
25 const canSkipReplacement = (node) => {
26 const callArguments = node.arguments
27β
28 const hasKeyProperty = () =>
29 callArguments[0].properties.some(
30 (property) =>
31 utils.isObjectProperty(property) &&
32 [config.keyName, 'queryFn'].includes(property.key.name),
33 )
34β
35 return (
36 callArguments.length > 0 &&
37 utils.isObjectExpression(callArguments[0]) &&
38 hasKeyProperty()
39 )
40 }
41β
42 const predicate = (property) => {
43 const isSpreadElement = utils.isSpreadElement(property)
44 const isObjectProperty = utils.isObjectProperty(property)
45β
46 return (
47 isSpreadElement ||
48 (isObjectProperty && property.key.name !== config.keyName)
49 )
50 }
51β
52 const transformArgumentToQueryFunction = (path, node) => {
53 const isIdentifier = utils.isIdentifier(node)
54 const isFunctionDefinition = utils.isFunctionDefinition(node)
55β
56 if (!isIdentifier && !isFunctionDefinition) {
57 return undefined
58 }
59β
60 if (isFunctionDefinition) {
61 return jscodeshift.property(
62 'init',
63 jscodeshift.identifier('queryFn'),
64 node,
65 )
66 }
67β
68 const binding = v5Utils.getBindingFromScope(path, node.name, filePath)
69 const initializer = v5Utils.getInitializerByDeclarator(binding)
70β
71 if (!utils.isFunctionDefinition(initializer)) {
72 return undefined
73 }
74β
75 return jscodeshift.property(
76 'init',
77 jscodeshift.identifier('queryFn'),
78 binding.id,
79 )
80 }
81β
82 const transformArgumentToOptionsObject = (path, node) => {
83 if (!utils.isIdentifier(node)) {
84 return undefined
85 }
86β
87 const binding = v5Utils.getBindingFromScope(path, node.name, filePath)
88 const initializer = v5Utils.getInitializerByDeclarator(binding)
89β
90 if (utils.isObjectExpression(initializer)) {
91 return jscodeshift.spreadElement(binding.id)
92 }
93 }
94β
95 const replacer = (path) => {
96 const node = path.node
97β
98 try {
99 // If the given method/function call matches certain criteria, the node doesn't need to be replaced, this step can be skipped.
100 if (canSkipReplacement(node)) {
101 return node
102 }
103β
104 const keyProperty = v5Utils.transformArgumentToKey(
105 path,
106 node.arguments[0],
107 config.keyName,
108 filePath,
109 )
110β
111 if (!keyProperty) {
112 throw new UnknownUsageError(node, filePath)
113 }
114β
115 const parameters = [jscodeshift.objectExpression([keyProperty])]
116 const createdObjectExpression = parameters[0]
117 const secondParameter = node.arguments[1]
118β
119 if (secondParameter) {
120 const queryFnProperty = transformArgumentToQueryFunction(
121 path,
122 secondParameter,
123 )
124β
125 if (queryFnProperty) {
126 createdObjectExpression.properties.push(queryFnProperty)
127β
128 const thirdParameter = node.arguments[2]
129β
130 if (utils.isObjectExpression(thirdParameter)) {
131 v5Utils.copyPropertiesFromSource(
132 thirdParameter,
133 createdObjectExpression,
134 predicate,
135 )
136 } else {
137 createdObjectExpression.properties.push(
138 jscodeshift.spreadElement(thirdParameter),
139 )
140 }
141β
142 return jscodeshift.callExpression(node.original.callee, parameters)
143 }
144β
145 const optionsProperty = transformArgumentToOptionsObject(
146 path,
147 secondParameter,
148 )
149β
150 if (optionsProperty) {
151 createdObjectExpression.properties.push(optionsProperty)
152β
153 return jscodeshift.callExpression(node.original.callee, parameters)
154 }
155β
156 if (utils.isObjectExpression(secondParameter)) {
157 v5Utils.copyPropertiesFromSource(
158 secondParameter,
159 createdObjectExpression,
160 predicate,
161 )
162 }
163β
164 return jscodeshift.callExpression(node.original.callee, parameters)
165 }
166β
167 return jscodeshift.callExpression(node.original.callee, parameters)
168 } catch (error) {
169 utils.warn(
170 error.name === UnknownUsageError.name
171 ? error.message
172 : `An unknown error occurred while processing the "${filePath}" file. Please review this file, because the codemod couldn't be applied.`,
173 )
174β
175 return node
176 }
177 }
178β
179 createQueryClientTransformer({ jscodeshift, utils, root }).execute(
180 config.queryClientMethods,
181 replacer,
182 )
183}
184β
185module.exports = transformQueryFnAwareUsages