MCPcopy
hub / github.com/TanStack/query / locateImports

Function locateImports

packages/query-codemods/src/utils/index.cjs:23–73  ·  view source on GitHub ↗
(
    identifiers,
    packageName = '@tanstack/react-query',
  )

Source from the content-addressed store, hash-verified

21 .find(jscodeshift.ImportSpecifier, {})
22
23 const locateImports = (
24 identifiers,
25 packageName = '@tanstack/react-query',
26 ) => {
27 const findNamespaceImportIdentifier = () => {
28 const specifier = root
29 .find(jscodeshift.ImportDeclaration, {
30 source: {
31 value: packageName,
32 },
33 })
34 .find(jscodeshift.ImportNamespaceSpecifier)
35 .paths()
36
37 return specifier.length > 0 ? specifier[0].value.local : null
38 }
39
40 /**
41 * First, we search for the namespace import identifier because if we have any, we assume the consumer uses
42 * namespace imports. In this case, we won't search for named imports at all.
43 */
44 const namespaceImportIdentifier = findNamespaceImportIdentifier()
45
46 if (namespaceImportIdentifier) {
47 const identifierMap = {}
48
49 for (const identifier of identifiers) {
50 identifierMap[identifier] = jscodeshift.identifier(identifier)
51 }
52
53 return {
54 namespace: namespaceImportIdentifier,
55 ...identifierMap,
56 }
57 }
58
59 const importSpecifiers = findImportSpecifiers(packageName)
60 const identifierMap = {}
61
62 for (const identifier of identifiers) {
63 identifierMap[identifier] = findImportIdentifierOf(
64 importSpecifiers,
65 identifier,
66 )
67 }
68
69 return {
70 namespace: null,
71 ...identifierMap,
72 }
73 }
74
75 const findAllMethodCalls = () =>
76 root

Callers

nothing calls this directly

Calls 3

findImportSpecifiersFunction · 0.85
findImportIdentifierOfFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…