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

index.js

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