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

index.js

166 lines4.3 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 isClassInstantiationOf = (node, selector) => {
121 if (!isNewExpression(node)) {
122 return false
123 }
124​
125 const parts = selector.split('.')
126​
127 return parts.length === 1
128 ? isIdentifier(node.callee) && node.callee.name === parts[0]
129 : isMemberExpression(node.callee) &&
130 node.callee.object.name === parts[0] &&
131 node.callee.property.name === parts[1]
132 }
133​
134 const isFunctionCallOf = (node, selector) => {
135 if (!isCallExpression(node)) {
136 return false
137 }
138​
139 const parts = selector.split('.')
140​
141 return parts.length === 1
142 ? isIdentifier(node.callee) && node.callee.name === parts[0]
143 : isMemberExpression(node.callee) &&
144 node.callee.object.name === parts[0] &&
145 node.callee.property.name === parts[1]
146 }
147​
148 const getSelectorByImports = (imports, path) =>
149 imports.namespace
150 ? `${imports.namespace.name}.${imports[path].name}`
151 : imports[path].name
152​
153 return {
154 findAllMethodCalls,
155 getSelectorByImports,
156 isCallExpression,
157 isClassInstantiationOf,
158 isFunctionCallOf,
159 isIdentifier,
160 isMemberExpression,
161 locateImports,
162 queryClient: {
163 findQueryClientIdentifiers,
164 },
165 }