File size: 944 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
module.exports = ({
  jscodeshift,
  utils,
  root,
  packageName = '@tanstack/react-query',
}) => {
  const filterUseQueryLikeHookCalls = (node, importIdentifiers, hooks) => {
    for (const hook of hooks) {
      const selector = utils.getSelectorByImports(importIdentifiers, hook)

      if (utils.isFunctionCallOf(node, selector)) {
        return true
      }
    }

    return false
  }

  const findUseQueryLikeHookCalls = (importIdentifiers, hooks) =>
    root
      // First, we need to find all call expressions.
      .find(jscodeshift.CallExpression, {})
      // Then we narrow the collection to the `useQuery` like hook calls.
      .filter((node) =>
        filterUseQueryLikeHookCalls(node.value, importIdentifiers, hooks),
      )

  const execute = (hooks, replacer) => {
    findUseQueryLikeHookCalls(
      utils.locateImports(hooks, packageName),
      hooks,
    ).replaceWith(replacer)
  }

  return {
    execute,
  }
}