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

index.cjs

208 lines5.5 KB

1module.exports = ({ root, jscodeshift }) => {
2 const findImportIdentifierOf = (importSpecifiers, identifier) => {
3 const specifier = importSpecifiers
4 .filter((node) => node.value.imported.name === identifier)
5 .paths()
6​
7 if (specifier.length > 0) {
8 return specifier[0].value.local
9 }
10​
11 return jscodeshift.identifier(identifier)
12 }
13​
14 const findImportSpecifiers = (packageName) =>
15 root
16 .find(jscodeshift.ImportDeclaration, {
17 source: {
18 value: packageName,
19 },
20 })
21 .find(jscodeshift.ImportSpecifier, {})
22​
23 const locateImports = (
24 identifiers,
25 packageName = '@tanstack/react-query',
26 ) => {
27 const findNamespaceImportIdentifier = () => {
28 const specifier = root
29 .find(jscodeshift.ImportDeclaration, {
30 source: {
31 value: packageName,
32 },
33 })
34 .find(jscodeshift.ImportNamespaceSpecifier)
35 .paths()
36​
37 return specifier.length > 0 ? specifier[0].value.local : null
38 }
39​
40 /**
41 * First, we search for the namespace import identifier because if we have any, we assume the consumer uses
42 * namespace imports. In this case, we won't search for named imports at all.
43 */
44 const namespaceImportIdentifier = findNamespaceImportIdentifier()
45​
46 if (namespaceImportIdentifier) {
47 const identifierMap = {}
48​
49 for (const identifier of identifiers) {
50 identifierMap[identifier] = jscodeshift.identifier(identifier)
51 }
52​
53 return {
54 namespace: namespaceImportIdentifier,
55 ...identifierMap,
56 }
57 }
58​
59 const importSpecifiers = findImportSpecifiers(packageName)
60 const identifierMap = {}
61​
62 for (const identifier of identifiers) {
63 identifierMap[identifier] = findImportIdentifierOf(
64 importSpecifiers,
65 identifier,
66 )
67 }
68​
69 return {
70 namespace: null,
71 ...identifierMap,
72 }
73 }
74​
75 const findAllMethodCalls = () =>
76 root
77 // First, we need to find all method calls.
78 .find(jscodeshift.CallExpression, {
79 callee: {
80 type: jscodeshift.MemberExpression.name,
81 property: {
82 type: jscodeshift.Identifier.name,
83 },
84 },
85 })
86​
87 const findQueryClientIdentifiers = (importIdentifiers) =>
88 root
89 .find(jscodeshift.VariableDeclarator, {})
90 .filter((node) => {
91 if (node.value.init) {
92 const initializer = node.value.init
93​
94 return (
95 isClassInstantiationOf(
96 initializer,
97 getSelectorByImports(importIdentifiers, 'QueryClient'),
98 ) ||
99 isFunctionCallOf(
100 initializer,
101 getSelectorByImports(importIdentifiers, 'useQueryClient'),
102 )
103 )
104 }
105​
106 return false
107 })
108 .paths()
109 .map((node) => node.value.id.name)
110​
111 const isCallExpression = (node) =>
112 jscodeshift.match(node, { type: jscodeshift.CallExpression.name })
113​
114 const isIdentifier = (node) =>
115 jscodeshift.match(node, { type: jscodeshift.Identifier.name })
116​
117 const isMemberExpression = (node) =>
118 jscodeshift.match(node, { type: jscodeshift.MemberExpression.name })
119​
120 const isNewExpression = (node) =>
121 jscodeshift.match(node, { type: jscodeshift.NewExpression.name })
122​
123 const isArrayExpression = (node) =>
124 jscodeshift.match(node, { type: jscodeshift.ArrayExpression.name })
125​
126 const isObjectExpression = (node) =>
127 jscodeshift.match(node, { type: jscodeshift.ObjectExpression.name })
128​
129 const isObjectProperty = (node) =>
130 jscodeshift.match(node, { type: jscodeshift.ObjectProperty.name })
131​
132 const isSpreadElement = (node) =>
133 jscodeshift.match(node, { type: jscodeshift.SpreadElement.name })
134​
135 /**
136 * @param {import('jscodeshift').Node} node
137 * @returns {boolean}
138 */
139 const isFunctionDefinition = (node) => {
140 const isArrowFunctionExpression = jscodeshift.match(node, {
141 type: jscodeshift.ArrowFunctionExpression.name,
142 })
143 const isFunctionExpression = jscodeshift.match(node, {
144 type: jscodeshift.FunctionExpression.name,
145 })
146​
147 return isArrowFunctionExpression || isFunctionExpression
148 }
149​
150 const warn = (message) => {
151 if (process.env.NODE_ENV !== 'test') {
152 console.warn(message)
153 }
154 }
155​
156 const isClassInstantiationOf = (node, selector) => {
157 if (!isNewExpression(node)) {
158 return false
159 }
160​
161 const parts = selector.split('.')
162​
163 return parts.length === 1
164 ? isIdentifier(node.callee) && node.callee.name === parts[0]
165 : isMemberExpression(node.callee) &&
166 node.callee.object.name === parts[0] &&
167 node.callee.property.name === parts[1]
168 }
169​
170 const isFunctionCallOf = (node, selector) => {
171 if (!isCallExpression(node)) {
172 return false
173 }
174​
175 const parts = selector.split('.')
176​
177 return parts.length === 1
178 ? isIdentifier(node.callee) && node.callee.name === parts[0]
179 : isMemberExpression(node.callee) &&
180 node.callee.object.name === parts[0] &&
181 node.callee.property.name === parts[1]
182 }
183​
184 const getSelectorByImports = (imports, path) =>
185 imports.namespace
186 ? `${imports.namespace.name}.${imports[path].name}`
187 : imports[path].name
188​
189 return {
190 findAllMethodCalls,
191 getSelectorByImports,
192 isCallExpression,
193 isClassInstantiationOf,
194 isFunctionCallOf,
195 isIdentifier,
196 isMemberExpression,
197 isArrayExpression,
198 isObjectExpression,
199 isObjectProperty,
200 isSpreadElement,
201 isFunctionDefinition,
202 locateImports,
203 warn,
204 queryClient: {
205 findQueryClientIdentifiers,
206 },
207 }