All files / utils/transformers query-cache-transformer.js

0% Statements 0/35
0% Branches 0/22
0% Functions 0/13
0% Lines 0/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125                                                                                                                                                                                                                                                         
module.exports = ({
  jscodeshift,
  utils,
  root,
  packageName = '@tanstack/react-query',
}) => {
  const isGetQueryCacheMethodCall = (
    initializer,
    importIdentifiers,
    knownQueryClientIds,
  ) => {
    const isKnownQueryClient = (node) =>
      utils.isIdentifier(node) && knownQueryClientIds.includes(node.name)
 
    const isGetQueryCacheIdentifier = (node) =>
      utils.isIdentifier(node) && node.name === 'getQueryCache'
 
    const isValidInitializer = (node) =>
      utils.isCallExpression(node) && utils.isMemberExpression(node.callee)
 
    if (isValidInitializer(initializer)) {
      const instance = initializer.callee.object
 
      return (
        isGetQueryCacheIdentifier(initializer.callee.property) &&
        (isKnownQueryClient(instance) ||
          utils.isFunctionCallOf(
            instance,
            utils.getSelectorByImports(importIdentifiers, 'useQueryClient'),
          ))
      )
    }
 
    return false
  }
 
  const findQueryCacheInstantiations = (
    importIdentifiers,
    knownQueryClientIds,
  ) =>
    root.find(jscodeshift.VariableDeclarator, {}).filter((node) => {
      if (node.value.init) {
        const initializer = node.value.init
 
        return (
          utils.isClassInstantiationOf(
            initializer,
            utils.getSelectorByImports(importIdentifiers, 'QueryCache'),
          ) ||
          isGetQueryCacheMethodCall(
            initializer,
            importIdentifiers,
            knownQueryClientIds,
          )
        )
      }
 
      return false
    })
 
  const filterQueryCacheMethodCalls = (node) =>
    utils.isIdentifier(node) && ['find', 'findAll'].includes(node.name)
 
  const findQueryCacheMethodCalls = (importIdentifiers) => {
    /**
     * Here we collect all query client instantiations. We have to make aware of them because the query cache can be
     * accessed by the query client as well.
     */
    const queryClientIdentifiers =
      utils.queryClient.findQueryClientIdentifiers(importIdentifiers)
    /**
     * Here we collect all query cache instantiations. The reason is simple: the methods can be called on query cache
     * instances, to locate the possible usages we need to be aware of the identifier names.
     */
    const queryCacheIdentifiers = findQueryCacheInstantiations(
      importIdentifiers,
      queryClientIdentifiers,
    )
      .paths()
      .map((node) => node.value.id.name)
 
    return (
      utils
        // First, we need to find all method calls.
        .findAllMethodCalls()
        // Then we narrow the collection to all `fetch` and `fetchAll` methods.
        .filter((node) =>
          filterQueryCacheMethodCalls(node.value.callee.property),
        )
        .filter((node) => {
          const object = node.value.callee.object
 
          // If the method is called on a `QueryCache` instance, we keep it in the collection.
          if (utils.isIdentifier(object)) {
            return queryCacheIdentifiers.includes(object.name)
          }
 
          // If the method is called on a `QueryClient` instance, we keep it in the collection.
          if (utils.isCallExpression(object)) {
            return isGetQueryCacheMethodCall(
              object,
              importIdentifiers,
              queryClientIdentifiers,
            )
          }
 
          return false
        })
    )
  }
 
  const execute = (replacer) => {
    findQueryCacheMethodCalls(
      utils.locateImports(
        ['QueryCache', 'QueryClient', 'useQueryClient'],
        packageName,
      ),
    ).replaceWith(replacer)
  }
 
  return {
    execute,
  }
}