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