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

use-query-like-transformer.cjs

38 lines944 Bytes

1module.exports = ({
2 jscodeshift,
3 utils,
4 root,
5 packageName = '@tanstack/react-query',
6}) => {
7 const filterUseQueryLikeHookCalls = (node, importIdentifiers, hooks) => {
8 for (const hook of hooks) {
9 const selector = utils.getSelectorByImports(importIdentifiers, hook)
10​
11 if (utils.isFunctionCallOf(node, selector)) {
12 return true
13 }
14 }
15​
16 return false
17 }
18​
19 const findUseQueryLikeHookCalls = (importIdentifiers, hooks) =>
20 root
21 // First, we need to find all call expressions.
22 .find(jscodeshift.CallExpression, {})
23 // Then we narrow the collection to the `useQuery` like hook calls.
24 .filter((node) =>
25 filterUseQueryLikeHookCalls(node.value, importIdentifiers, hooks),
26 )
27​
28 const execute = (hooks, replacer) => {
29 findUseQueryLikeHookCalls(
30 utils.locateImports(hooks, packageName),
31 hooks,
32 ).replaceWith(replacer)
33 }
34​
35 return {
36 execute,
37 }