Skip to content

Commit 41e0938

Browse files
fix(codemod): add the packageName parameter to the locateImports function (#6285)
It's necessary, because before this change the above-mentioned function was looking for imports from the `react-query` package, but the package was renamed in the version 4 release. In case of the older codemods the `react-query` is still a valid package name, but for the current and future codemods we have to locate imports from the `@tanstack/react-query` package.
1 parent 7153c57 commit 41e0938

File tree

5 files changed

+92
-22
lines changed

5 files changed

+92
-22
lines changed

packages/codemods/src/utils/index.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,24 @@ module.exports = ({ root, jscodeshift }) => {
1111
return jscodeshift.identifier(identifier)
1212
}
1313

14-
const findImportSpecifiers = () =>
14+
const findImportSpecifiers = (packageName) =>
1515
root
1616
.find(jscodeshift.ImportDeclaration, {
1717
source: {
18-
value: 'react-query',
18+
value: packageName,
1919
},
2020
})
2121
.find(jscodeshift.ImportSpecifier, {})
2222

23-
const locateImports = (identifiers) => {
23+
const locateImports = (
24+
identifiers,
25+
packageName = '@tanstack/react-query',
26+
) => {
2427
const findNamespaceImportIdentifier = () => {
2528
const specifier = root
2629
.find(jscodeshift.ImportDeclaration, {
2730
source: {
28-
value: 'react-query',
31+
value: packageName,
2932
},
3033
})
3134
.find(jscodeshift.ImportNamespaceSpecifier)
@@ -53,7 +56,7 @@ module.exports = ({ root, jscodeshift }) => {
5356
}
5457
}
5558

56-
const importSpecifiers = findImportSpecifiers()
59+
const importSpecifiers = findImportSpecifiers(packageName)
5760
const identifierMap = {}
5861

5962
for (const identifier of identifiers) {

packages/codemods/src/utils/transformers/query-cache-transformer.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
module.exports = ({ jscodeshift, utils, root }) => {
1+
module.exports = ({
2+
jscodeshift,
3+
utils,
4+
root,
5+
packageName = '@tanstack/react-query',
6+
}) => {
27
const isGetQueryCacheMethodCall = (
38
initializer,
49
importIdentifiers,
@@ -106,7 +111,10 @@ module.exports = ({ jscodeshift, utils, root }) => {
106111

107112
const execute = (replacer) => {
108113
findQueryCacheMethodCalls(
109-
utils.locateImports(['QueryCache', 'QueryClient', 'useQueryClient']),
114+
utils.locateImports(
115+
['QueryCache', 'QueryClient', 'useQueryClient'],
116+
packageName,
117+
),
110118
).replaceWith(replacer)
111119
}
112120

packages/codemods/src/utils/transformers/query-client-transformer.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
module.exports = ({ jscodeshift, utils, root }) => {
1+
module.exports = ({
2+
jscodeshift,
3+
utils,
4+
root,
5+
packageName = '@tanstack/react-query',
6+
}) => {
27
const filterQueryClientMethodCalls = (node, methods) =>
38
utils.isIdentifier(node) && methods.includes(node.name)
49

@@ -37,7 +42,7 @@ module.exports = ({ jscodeshift, utils, root }) => {
3742

3843
const execute = (methods, replacer) => {
3944
findQueryClientMethodCalls(
40-
utils.locateImports(['QueryClient', 'useQueryClient']),
45+
utils.locateImports(['QueryClient', 'useQueryClient'], packageName),
4146
methods,
4247
).replaceWith(replacer)
4348
}

packages/codemods/src/utils/transformers/use-query-like-transformer.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
module.exports = ({ jscodeshift, utils, root }) => {
1+
module.exports = ({
2+
jscodeshift,
3+
utils,
4+
root,
5+
packageName = '@tanstack/react-query',
6+
}) => {
27
const filterUseQueryLikeHookCalls = (node, importIdentifiers, hooks) => {
38
for (const hook of hooks) {
49
const selector = utils.getSelectorByImports(importIdentifiers, hook)
@@ -21,9 +26,10 @@ module.exports = ({ jscodeshift, utils, root }) => {
2126
)
2227

2328
const execute = (hooks, replacer) => {
24-
findUseQueryLikeHookCalls(utils.locateImports(hooks), hooks).replaceWith(
25-
replacer,
26-
)
29+
findUseQueryLikeHookCalls(
30+
utils.locateImports(hooks, packageName),
31+
hooks,
32+
).replaceWith(replacer)
2733
}
2834

2935
return {

packages/codemods/src/v4/key-transformation.js

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,19 @@ const createQueryClientTransformer = require('../utils/transformers/query-client
99
// eslint-disable-next-line @typescript-eslint/no-var-requires
1010
const createQueryCacheTransformer = require('../utils/transformers/query-cache-transformer')
1111

12-
const transformQueryClientUsages = ({ jscodeshift, utils, root, filePath }) => {
13-
const transformer = createQueryClientTransformer({ jscodeshift, utils, root })
12+
const transformQueryClientUsages = ({
13+
jscodeshift,
14+
utils,
15+
root,
16+
filePath,
17+
packageName,
18+
}) => {
19+
const transformer = createQueryClientTransformer({
20+
jscodeshift,
21+
utils,
22+
root,
23+
packageName,
24+
})
1425
const replacer = createKeyReplacer({ jscodeshift, root, filePath })
1526

1627
transformer.execute(
@@ -41,11 +52,17 @@ const transformQueryClientUsages = ({ jscodeshift, utils, root, filePath }) => {
4152
)
4253
}
4354

44-
const transformUseQueriesUsages = ({ jscodeshift, utils, root }) => {
55+
const transformUseQueriesUsages = ({
56+
jscodeshift,
57+
utils,
58+
root,
59+
packageName,
60+
}) => {
4561
const transformer = createUseQueryLikeTransformer({
4662
jscodeshift,
4763
utils,
4864
root,
65+
packageName,
4966
})
5067
const replacer = ({ node }) => {
5168
/**
@@ -82,11 +99,13 @@ const transformUseQueryLikeUsages = ({
8299
utils,
83100
root,
84101
filePath,
102+
packageName,
85103
}) => {
86104
const transformer = createUseQueryLikeTransformer({
87105
jscodeshift,
88106
utils,
89107
root,
108+
packageName,
90109
})
91110

92111
transformer.execute(
@@ -109,8 +128,19 @@ const transformUseQueryLikeUsages = ({
109128
)
110129
}
111130

112-
const transformQueryCacheUsages = ({ jscodeshift, utils, root, filePath }) => {
113-
const transformer = createQueryCacheTransformer({ jscodeshift, utils, root })
131+
const transformQueryCacheUsages = ({
132+
jscodeshift,
133+
utils,
134+
root,
135+
filePath,
136+
packageName,
137+
}) => {
138+
const transformer = createQueryCacheTransformer({
139+
jscodeshift,
140+
utils,
141+
root,
142+
packageName,
143+
})
114144
const replacer = createKeyReplacer({ jscodeshift, root, filePath })
115145

116146
transformer.execute(replacer)
@@ -124,15 +154,33 @@ module.exports = (file, api) => {
124154

125155
const utils = createUtilsObject({ root, jscodeshift })
126156
const filePath = file.path
157+
const packageName = 'react-query'
127158

128159
// This function transforms usages like `useQuery` and `useMutation`.
129-
transformUseQueryLikeUsages({ jscodeshift, utils, root, filePath })
160+
transformUseQueryLikeUsages({
161+
jscodeshift,
162+
utils,
163+
root,
164+
filePath,
165+
packageName,
166+
})
130167
// This function transforms usages of `useQueries`.
131-
transformUseQueriesUsages({ jscodeshift, utils, root })
168+
transformUseQueriesUsages({
169+
jscodeshift,
170+
utils,
171+
root,
172+
packageName,
173+
})
132174
// This function transforms usages of `QueryClient`.
133-
transformQueryClientUsages({ jscodeshift, utils, root, filePath })
175+
transformQueryClientUsages({
176+
jscodeshift,
177+
utils,
178+
root,
179+
filePath,
180+
packageName,
181+
})
134182
// This function transforms usages of `QueryCache`.
135-
transformQueryCacheUsages({ jscodeshift, utils, root, filePath })
183+
transformQueryCacheUsages({ jscodeshift, utils, root, filePath, packageName })
136184

137185
return root.toSource({ quote: 'single', lineTerminator: '\n' })
138186
}

0 commit comments

Comments
 (0)