All files / utils index.js

0% Statements 0/69
0% Branches 0/35
0% Functions 0/23
0% Lines 0/69

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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209                                                                                                                                                                                                                                                                                                                                                                                                                                 
module.exports = ({ root, jscodeshift }) => {
  const findImportIdentifierOf = (importSpecifiers, identifier) => {
    const specifier = importSpecifiers
      .filter((node) => node.value.imported.name === identifier)
      .paths()
 
    if (specifier.length > 0) {
      return specifier[0].value.local
    }
 
    return jscodeshift.identifier(identifier)
  }
 
  const findImportSpecifiers = (packageName) =>
    root
      .find(jscodeshift.ImportDeclaration, {
        source: {
          value: packageName,
        },
      })
      .find(jscodeshift.ImportSpecifier, {})
 
  const locateImports = (
    identifiers,
    packageName = '@tanstack/react-query',
  ) => {
    const findNamespaceImportIdentifier = () => {
      const specifier = root
        .find(jscodeshift.ImportDeclaration, {
          source: {
            value: packageName,
          },
        })
        .find(jscodeshift.ImportNamespaceSpecifier)
        .paths()
 
      return specifier.length > 0 ? specifier[0].value.local : null
    }
 
    /**
     * First, we search for the namespace import identifier because if we have any, we assume the consumer uses
     * namespace imports. In this case, we won't search for named imports at all.
     */
    const namespaceImportIdentifier = findNamespaceImportIdentifier()
 
    if (namespaceImportIdentifier) {
      const identifierMap = {}
 
      for (const identifier of identifiers) {
        identifierMap[identifier] = jscodeshift.identifier(identifier)
      }
 
      return {
        namespace: namespaceImportIdentifier,
        ...identifierMap,
      }
    }
 
    const importSpecifiers = findImportSpecifiers(packageName)
    const identifierMap = {}
 
    for (const identifier of identifiers) {
      identifierMap[identifier] = findImportIdentifierOf(
        importSpecifiers,
        identifier,
      )
    }
 
    return {
      namespace: null,
      ...identifierMap,
    }
  }
 
  const findAllMethodCalls = () =>
    root
      // First, we need to find all method calls.
      .find(jscodeshift.CallExpression, {
        callee: {
          type: jscodeshift.MemberExpression.name,
          property: {
            type: jscodeshift.Identifier.name,
          },
        },
      })
 
  const findQueryClientIdentifiers = (importIdentifiers) =>
    root
      .find(jscodeshift.VariableDeclarator, {})
      .filter((node) => {
        if (node.value.init) {
          const initializer = node.value.init
 
          return (
            isClassInstantiationOf(
              initializer,
              getSelectorByImports(importIdentifiers, 'QueryClient'),
            ) ||
            isFunctionCallOf(
              initializer,
              getSelectorByImports(importIdentifiers, 'useQueryClient'),
            )
          )
        }
 
        return false
      })
      .paths()
      .map((node) => node.value.id.name)
 
  const isCallExpression = (node) =>
    jscodeshift.match(node, { type: jscodeshift.CallExpression.name })
 
  const isIdentifier = (node) =>
    jscodeshift.match(node, { type: jscodeshift.Identifier.name })
 
  const isMemberExpression = (node) =>
    jscodeshift.match(node, { type: jscodeshift.MemberExpression.name })
 
  const isNewExpression = (node) =>
    jscodeshift.match(node, { type: jscodeshift.NewExpression.name })
 
  const isArrayExpression = (node) =>
    jscodeshift.match(node, { type: jscodeshift.ArrayExpression.name })
 
  const isObjectExpression = (node) =>
    jscodeshift.match(node, { type: jscodeshift.ObjectExpression.name })
 
  const isObjectProperty = (node) =>
    jscodeshift.match(node, { type: jscodeshift.ObjectProperty.name })
 
  const isSpreadElement = (node) =>
    jscodeshift.match(node, { type: jscodeshift.SpreadElement.name })
 
  /**
   * @param {import('jscodeshift').Node} node
   * @returns {boolean}
   */
  const isFunctionDefinition = (node) => {
    const isArrowFunctionExpression = jscodeshift.match(node, {
      type: jscodeshift.ArrowFunctionExpression.name,
    })
    const isFunctionExpression = jscodeshift.match(node, {
      type: jscodeshift.FunctionExpression.name,
    })
 
    return isArrowFunctionExpression || isFunctionExpression
  }
 
  const warn = (message) => {
    if (process.env.NODE_ENV !== 'test') {
      console.warn(message)
    }
  }
 
  const isClassInstantiationOf = (node, selector) => {
    if (!isNewExpression(node)) {
      return false
    }
 
    const parts = selector.split('.')
 
    return parts.length === 1
      ? isIdentifier(node.callee) && node.callee.name === parts[0]
      : isMemberExpression(node.callee) &&
          node.callee.object.name === parts[0] &&
          node.callee.property.name === parts[1]
  }
 
  const isFunctionCallOf = (node, selector) => {
    if (!isCallExpression(node)) {
      return false
    }
 
    const parts = selector.split('.')
 
    return parts.length === 1
      ? isIdentifier(node.callee) && node.callee.name === parts[0]
      : isMemberExpression(node.callee) &&
          node.callee.object.name === parts[0] &&
          node.callee.property.name === parts[1]
  }
 
  const getSelectorByImports = (imports, path) =>
    imports.namespace
      ? `${imports.namespace.name}.${imports[path].name}`
      : imports[path].name
 
  return {
    findAllMethodCalls,
    getSelectorByImports,
    isCallExpression,
    isClassInstantiationOf,
    isFunctionCallOf,
    isIdentifier,
    isMemberExpression,
    isArrayExpression,
    isObjectExpression,
    isObjectProperty,
    isSpreadElement,
    isFunctionDefinition,
    locateImports,
    warn,
    queryClient: {
      findQueryClientIdentifiers,
    },
  }
}