Skip to content

Commit 5a156e2

Browse files
chore(codemods): move existing codemods into a separate package (#4949)
* chore(codemods): move existing codemods into a separate package * chore(codemods): fix formatting of the existing codemods * refactor(codemods): move existing utility functions to a shared place These utility functions can be reused for the version 5 codemods, so I decided to move them to a shared place. * refactor(codemods): move `UnprocessableKeyError` class to the `key-replacer.js` file Since it was used only here, it doesn't make any sense to keep it in a separate file. * chore(codemods): add `README.md` file under the `v5` directory --------- Co-authored-by: Dominik Dorfmeister <[email protected]>
1 parent aee932b commit 5a156e2

33 files changed

+177
-131
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/packages/svelte-query/.svelte-kit
2+
/packages/codemods/**/__testfixtures__

packages/codemods/.eslintrc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"parserOptions": {
3+
"project": "./tsconfig.json",
4+
"sourceType": "module"
5+
},
6+
"overrides": [
7+
{
8+
"files": ["**/__testfixtures__/*"],
9+
"rules": {
10+
"import/no-unresolved": "off",
11+
"@typescript-eslint/no-unused-vars": "off"
12+
}
13+
}
14+
]
15+
}

packages/codemods/jest.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default {
2+
displayName: 'query-codemods',
3+
preset: '../../jest-preset.js'
4+
}

packages/codemods/package.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "@tanstack/query-codemods",
3+
"version": "4.24.3",
4+
"description": "Collection of codemods to make the migration easier.",
5+
"author": "Balázs Máté Petró",
6+
"license": "MIT",
7+
"repository": "tanstack/query",
8+
"homepage": "https://tanstack.com/query",
9+
"funding": {
10+
"type": "github",
11+
"url": "https://github.com/sponsors/tannerlinsley"
12+
},
13+
"exports": {
14+
"./package.json": "./package.json"
15+
},
16+
"sideEffects": false,
17+
"scripts": {
18+
"test:eslint": "eslint --ext .ts,.tsx ./src",
19+
"test:lib": "jest --config ./jest.config.ts",
20+
"test:lib:dev": "pnpm run test:lib --watch"
21+
},
22+
"files": [
23+
"src",
24+
"!src/jest.config.js",
25+
"!src/**/__testfixtures__",
26+
"!src/**/__tests__"
27+
],
28+
"devDependencies": {
29+
"@types/jscodeshift": "0.11.5",
30+
"jscodeshift": "0.13.1"
31+
}
32+
}

packages/react-query/codemods/v4/utils/index.js renamed to packages/codemods/src/utils/index.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = ({ root, jscodeshift }) => {
22
const findImportIdentifierOf = (importSpecifiers, identifier) => {
33
const specifier = importSpecifiers
4-
.filter(node => node.value.imported.name === identifier)
4+
.filter((node) => node.value.imported.name === identifier)
55
.paths()
66

77
if (specifier.length > 0) {
@@ -20,7 +20,7 @@ module.exports = ({ root, jscodeshift }) => {
2020
})
2121
.find(jscodeshift.ImportSpecifier, {})
2222

23-
const locateImports = identifiers => {
23+
const locateImports = (identifiers) => {
2424
const findNamespaceImportIdentifier = () => {
2525
const specifier = root
2626
.find(jscodeshift.ImportDeclaration, {
@@ -59,7 +59,7 @@ module.exports = ({ root, jscodeshift }) => {
5959
for (const identifier of identifiers) {
6060
identifierMap[identifier] = findImportIdentifierOf(
6161
importSpecifiers,
62-
identifier
62+
identifier,
6363
)
6464
}
6565

@@ -81,40 +81,40 @@ module.exports = ({ root, jscodeshift }) => {
8181
},
8282
})
8383

84-
const findQueryClientIdentifiers = importIdentifiers =>
84+
const findQueryClientIdentifiers = (importIdentifiers) =>
8585
root
8686
.find(jscodeshift.VariableDeclarator, {})
87-
.filter(node => {
87+
.filter((node) => {
8888
if (node.value.init) {
8989
const initializer = node.value.init
9090

9191
return (
9292
isClassInstantiationOf(
9393
initializer,
94-
getSelectorByImports(importIdentifiers, 'QueryClient')
94+
getSelectorByImports(importIdentifiers, 'QueryClient'),
9595
) ||
9696
isFunctionCallOf(
9797
initializer,
98-
getSelectorByImports(importIdentifiers, 'useQueryClient')
98+
getSelectorByImports(importIdentifiers, 'useQueryClient'),
9999
)
100100
)
101101
}
102102

103103
return false
104104
})
105105
.paths()
106-
.map(node => node.value.id.name)
106+
.map((node) => node.value.id.name)
107107

108-
const isCallExpression = node =>
108+
const isCallExpression = (node) =>
109109
jscodeshift.match(node, { type: jscodeshift.CallExpression.name })
110110

111-
const isIdentifier = node =>
111+
const isIdentifier = (node) =>
112112
jscodeshift.match(node, { type: jscodeshift.Identifier.name })
113113

114-
const isMemberExpression = node =>
114+
const isMemberExpression = (node) =>
115115
jscodeshift.match(node, { type: jscodeshift.MemberExpression.name })
116116

117-
const isNewExpression = node =>
117+
const isNewExpression = (node) =>
118118
jscodeshift.match(node, { type: jscodeshift.NewExpression.name })
119119

120120
const isClassInstantiationOf = (node, selector) => {

packages/react-query/codemods/v4/utils/transformers/query-cache-transformer.js renamed to packages/codemods/src/utils/transformers/query-cache-transformer.js

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ module.exports = ({ jscodeshift, utils, root }) => {
22
const isGetQueryCacheMethodCall = (
33
initializer,
44
importIdentifiers,
5-
knownQueryClientIds
5+
knownQueryClientIds,
66
) => {
7-
const isKnownQueryClient = node =>
7+
const isKnownQueryClient = (node) =>
88
utils.isIdentifier(node) && knownQueryClientIds.includes(node.name)
99

10-
const isGetQueryCacheIdentifier = node =>
10+
const isGetQueryCacheIdentifier = (node) =>
1111
utils.isIdentifier(node) && node.name === 'getQueryCache'
1212

13-
const isValidInitializer = node =>
13+
const isValidInitializer = (node) =>
1414
utils.isCallExpression(node) && utils.isMemberExpression(node.callee)
1515

1616
if (isValidInitializer(initializer)) {
@@ -21,7 +21,7 @@ module.exports = ({ jscodeshift, utils, root }) => {
2121
(isKnownQueryClient(instance) ||
2222
utils.isFunctionCallOf(
2323
instance,
24-
utils.getSelectorByImports(importIdentifiers, 'useQueryClient')
24+
utils.getSelectorByImports(importIdentifiers, 'useQueryClient'),
2525
))
2626
)
2727
}
@@ -31,57 +31,58 @@ module.exports = ({ jscodeshift, utils, root }) => {
3131

3232
const findQueryCacheInstantiations = (
3333
importIdentifiers,
34-
knownQueryClientIds
34+
knownQueryClientIds,
3535
) =>
36-
root.find(jscodeshift.VariableDeclarator, {}).filter(node => {
36+
root.find(jscodeshift.VariableDeclarator, {}).filter((node) => {
3737
if (node.value.init) {
3838
const initializer = node.value.init
3939

4040
return (
4141
utils.isClassInstantiationOf(
4242
initializer,
43-
utils.getSelectorByImports(importIdentifiers, 'QueryCache')
43+
utils.getSelectorByImports(importIdentifiers, 'QueryCache'),
4444
) ||
4545
isGetQueryCacheMethodCall(
4646
initializer,
4747
importIdentifiers,
48-
knownQueryClientIds
48+
knownQueryClientIds,
4949
)
5050
)
5151
}
5252

5353
return false
5454
})
5555

56-
const filterQueryCacheMethodCalls = node =>
56+
const filterQueryCacheMethodCalls = (node) =>
5757
utils.isIdentifier(node) && ['find', 'findAll'].includes(node.name)
5858

59-
const findQueryCacheMethodCalls = importIdentifiers => {
59+
const findQueryCacheMethodCalls = (importIdentifiers) => {
6060
/**
6161
* Here we collect all query client instantiations. We have to make aware of them because the query cache can be
6262
* accessed by the query client as well.
6363
*/
64-
const queryClientIdentifiers = utils.queryClient.findQueryClientIdentifiers(
65-
importIdentifiers
66-
)
64+
const queryClientIdentifiers =
65+
utils.queryClient.findQueryClientIdentifiers(importIdentifiers)
6766
/**
6867
* Here we collect all query cache instantiations. The reason is simple: the methods can be called on query cache
6968
* instances, to locate the possible usages we need to be aware of the identifier names.
7069
*/
7170
const queryCacheIdentifiers = findQueryCacheInstantiations(
7271
importIdentifiers,
73-
queryClientIdentifiers
72+
queryClientIdentifiers,
7473
)
7574
.paths()
76-
.map(node => node.value.id.name)
75+
.map((node) => node.value.id.name)
7776

7877
return (
7978
utils
8079
// First, we need to find all method calls.
8180
.findAllMethodCalls()
8281
// Then we narrow the collection to all `fetch` and `fetchAll` methods.
83-
.filter(node => filterQueryCacheMethodCalls(node.value.callee.property))
84-
.filter(node => {
82+
.filter((node) =>
83+
filterQueryCacheMethodCalls(node.value.callee.property),
84+
)
85+
.filter((node) => {
8586
const object = node.value.callee.object
8687

8788
// If the method is called on a `QueryCache` instance, we keep it in the collection.
@@ -94,7 +95,7 @@ module.exports = ({ jscodeshift, utils, root }) => {
9495
return isGetQueryCacheMethodCall(
9596
object,
9697
importIdentifiers,
97-
queryClientIdentifiers
98+
queryClientIdentifiers,
9899
)
99100
}
100101

@@ -103,9 +104,9 @@ module.exports = ({ jscodeshift, utils, root }) => {
103104
)
104105
}
105106

106-
const execute = replacer => {
107+
const execute = (replacer) => {
107108
findQueryCacheMethodCalls(
108-
utils.locateImports(['QueryCache', 'QueryClient', 'useQueryClient'])
109+
utils.locateImports(['QueryCache', 'QueryClient', 'useQueryClient']),
109110
).replaceWith(replacer)
110111
}
111112

packages/react-query/codemods/v4/utils/transformers/query-client-transformer.js renamed to packages/codemods/src/utils/transformers/query-client-transformer.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,18 @@ module.exports = ({ jscodeshift, utils, root }) => {
77
* Here we collect all query client instantiations. We have to make aware of them because some method calls might
88
* be invoked on these instances.
99
*/
10-
const queryClientIdentifiers = utils.queryClient.findQueryClientIdentifiers(
11-
importIdentifiers
12-
)
10+
const queryClientIdentifiers =
11+
utils.queryClient.findQueryClientIdentifiers(importIdentifiers)
1312

1413
return (
1514
utils
1615
// First, we need to find all method calls.
1716
.findAllMethodCalls()
1817
// Then we narrow the collection to `QueryClient` methods.
19-
.filter(node =>
20-
filterQueryClientMethodCalls(node.value.callee.property, methods)
18+
.filter((node) =>
19+
filterQueryClientMethodCalls(node.value.callee.property, methods),
2120
)
22-
.filter(node => {
21+
.filter((node) => {
2322
const object = node.value.callee.object
2423

2524
// If the method is called on a `QueryClient` instance, we keep it in the collection.
@@ -30,7 +29,7 @@ module.exports = ({ jscodeshift, utils, root }) => {
3029
// If the method is called on the return value of `useQueryClient` hook, we keep it in the collection.
3130
return utils.isFunctionCallOf(
3231
object,
33-
utils.getSelectorByImports(importIdentifiers, 'useQueryClient')
32+
utils.getSelectorByImports(importIdentifiers, 'useQueryClient'),
3433
)
3534
})
3635
)
@@ -39,7 +38,7 @@ module.exports = ({ jscodeshift, utils, root }) => {
3938
const execute = (methods, replacer) => {
4039
findQueryClientMethodCalls(
4140
utils.locateImports(['QueryClient', 'useQueryClient']),
42-
methods
41+
methods,
4342
).replaceWith(replacer)
4443
}
4544

packages/react-query/codemods/v4/utils/transformers/use-query-like-transformer.js renamed to packages/codemods/src/utils/transformers/use-query-like-transformer.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ module.exports = ({ jscodeshift, utils, root }) => {
1616
// First, we need to find all call expressions.
1717
.find(jscodeshift.CallExpression, {})
1818
// Then we narrow the collection to the `useQuery` like hook calls.
19-
.filter(node =>
20-
filterUseQueryLikeHookCalls(node.value, importIdentifiers, hooks)
19+
.filter((node) =>
20+
filterUseQueryLikeHookCalls(node.value, importIdentifiers, hooks),
2121
)
2222

2323
const execute = (hooks, replacer) => {
2424
findUseQueryLikeHookCalls(utils.locateImports(hooks), hooks).replaceWith(
25-
replacer
25+
replacer,
2626
)
2727
}
2828

0 commit comments

Comments
 (0)