Open History to see the last commit
1module.exports = ({ jscodeshift, utils, root }) => {
2 const filterQueryClientMethodCalls = (node, methods) =>
3 utils.isIdentifier(node) && methods.includes(node.name)
4β
5 const findQueryClientMethodCalls = (importIdentifiers, methods) => {
6 /**
7 * Here we collect all query client instantiations. We have to make aware of them because some method calls might
8 * be invoked on these instances.
9 */
10 const queryClientIdentifiers = utils.queryClient.findQueryClientIdentifiers(
11 importIdentifiers
12 )
13β
14 return (
15 utils
16 // First, we need to find all method calls.
17 .findAllMethodCalls()
18 // Then we narrow the collection to `QueryClient` methods.
19 .filter(node =>
20 filterQueryClientMethodCalls(node.value.callee.property, methods)
21 )
22 .filter(node => {
23 const object = node.value.callee.object
24β
25 // If the method is called on a `QueryClient` instance, we keep it in the collection.
26 if (utils.isIdentifier(object)) {
27 return queryClientIdentifiers.includes(object.name)
28 }
29β
30 // If the method is called on the return value of `useQueryClient` hook, we keep it in the collection.
31 return utils.isFunctionCallOf(
32 object,
33 utils.getSelectorByImports(importIdentifiers, 'useQueryClient')
34 )
35 })
36 )
37 }
38β
39 const execute = (methods, replacer) => {
40 findQueryClientMethodCalls(
41 utils.locateImports(['QueryClient', 'useQueryClient']),
42 methods
43 ).replaceWith(replacer)
44 }
45β
46 return {
47 execute,
48 }
49}