Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

query-cache-transformer.cjs

124 lines3.5 KB

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