Open History to see the last commit
1module.exports = ({ jscodeshift, utils, root }) => {
2 const isGetQueryCacheMethodCall = (
3 initializer,
4 importIdentifiers,
5 knownQueryClientIds
6 ) => {
7 const isKnownQueryClient = node =>
8 utils.isIdentifier(node) && knownQueryClientIds.includes(node.name)
9β
10 const isGetQueryCacheIdentifier = node =>
11 utils.isIdentifier(node) && node.name === 'getQueryCache'
12β
13 const isValidInitializer = node =>
14 utils.isCallExpression(node) && utils.isMemberExpression(node.callee)
15β
16 if (isValidInitializer(initializer)) {
17 const instance = initializer.callee.object
18β
19 return (
20 isGetQueryCacheIdentifier(initializer.callee.property) &&
21 (isKnownQueryClient(instance) ||
22 utils.isFunctionCallOf(
23 instance,
24 utils.getSelectorByImports(importIdentifiers, 'useQueryClient')
25 ))
26 )
27 }
28β
29 return false
30 }
31β
32 const findQueryCacheInstantiations = (
33 importIdentifiers,
34 knownQueryClientIds
35 ) =>
36 root.find(jscodeshift.VariableDeclarator, {}).filter(node => {
37 if (node.value.init) {
38 const initializer = node.value.init
39β
40 return (
41 utils.isClassInstantiationOf(
42 initializer,
43 utils.getSelectorByImports(importIdentifiers, 'QueryCache')
44 ) ||
45 isGetQueryCacheMethodCall(
46 initializer,
47 importIdentifiers,
48 knownQueryClientIds
49 )
50 )
51 }
52β
53 return false
54 })
55β
56 const filterQueryCacheMethodCalls = node =>
57 utils.isIdentifier(node) && ['find', 'findAll'].includes(node.name)
58β
59 const findQueryCacheMethodCalls = importIdentifiers => {
60 /**
61 * Here we collect all query client instantiations. We have to make aware of them because the query cache can be
62 * accessed by the query client as well.
63 */
64 const queryClientIdentifiers = utils.queryClient.findQueryClientIdentifiers(
65 importIdentifiers
66 )
67 /**
68 * Here we collect all query cache instantiations. The reason is simple: the methods can be called on query cache
69 * instances, to locate the possible usages we need to be aware of the identifier names.
70 */
71 const queryCacheIdentifiers = findQueryCacheInstantiations(
72 importIdentifiers,
73 queryClientIdentifiers
74 )
75 .paths()
76 .map(node => node.value.id.name)
77β
78 return (
79 utils
80 // First, we need to find all method calls.
81 .findAllMethodCalls()
82 // Then we narrow the collection to all `fetch` and `fetchAll` methods.
83 .filter(node => filterQueryCacheMethodCalls(node.value.callee.property))
84 .filter(node => {
85 const object = node.value.callee.object
86β
87 // If the method is called on a `QueryCache` instance, we keep it in the collection.
88 if (utils.isIdentifier(object)) {
89 return queryCacheIdentifiers.includes(object.name)
90 }
91β
92 // If the method is called on a `QueryClient` instance, we keep it in the collection.
93 if (utils.isCallExpression(object)) {
94 return isGetQueryCacheMethodCall(
95 object,
96 importIdentifiers,
97 queryClientIdentifiers
98 )
99 }
100β
101 return false
102 })
103 )
104 }
105β
106 const execute = replacer => {
107 findQueryCacheMethodCalls(
108 utils.locateImports(['QueryCache', 'QueryClient', 'useQueryClient'])
109 ).replaceWith(replacer)
110 }
111β
112 return {
113 execute,
114 }
115}