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 =
65 utils.queryClient.findQueryClientIdentifiers(importIdentifiers)
66 /**
67 * Here we collect all query cache instantiations. The reason is simple: the methods can be called on query cache
68 * instances, to locate the possible usages we need to be aware of the identifier names.
69 */
70 const queryCacheIdentifiers = findQueryCacheInstantiations(
71 importIdentifiers,
72 queryClientIdentifiers,
73 )
74 .paths()
75 .map((node) => node.value.id.name)
76β
77 return (
78 utils
79 // First, we need to find all method calls.
80 .findAllMethodCalls()
81 // Then we narrow the collection to all `fetch` and `fetchAll` methods.
82 .filter((node) =>
83 filterQueryCacheMethodCalls(node.value.callee.property),
84 )
85 .filter((node) => {
86 const object = node.value.callee.object
87β
88 // If the method is called on a `QueryCache` instance, we keep it in the collection.
89 if (utils.isIdentifier(object)) {
90 return queryCacheIdentifiers.includes(object.name)
91 }
92β
93 // If the method is called on a `QueryClient` instance, we keep it in the collection.
94 if (utils.isCallExpression(object)) {
95 return isGetQueryCacheMethodCall(
96 object,
97 importIdentifiers,
98 queryClientIdentifiers,
99 )
100 }
101β
102 return false
103 })
104 )
105 }
106β
107 const execute = (replacer) => {
108 findQueryCacheMethodCalls(
109 utils.locateImports(['QueryCache', 'QueryClient', 'useQueryClient']),
110 ).replaceWith(replacer)
111 }
112β
113 return {
114 execute,
115 }
116}