MCPcopy Create free account
hub / github.com/TanStack/devtools / findDevtoolsComponentName

Function findDevtoolsComponentName

packages/devtools-vite/src/inject-plugin.ts:42–79  ·  view source on GitHub ↗
(code: string)

Source from the content-addressed store, hash-verified

40 * Handles renamed imports and namespace imports
41 */
42export const findDevtoolsComponentName = (code: string): string | null => {
43 try {
44 const result = parseSync('input.tsx', code, {
45 sourceType: 'module',
46 lang: 'tsx',
47 })
48 if (result.errors.length > 0) return null
49
50 let componentName: string | null = null
51
52 walk(result.program, (node) => {
53 if (componentName) return
54 if (node.type !== 'ImportDeclaration') return
55 if (!isTanStackDevtoolsImport(node.source.value)) return
56
57 for (const spec of node.specifiers) {
58 // import { TanStackDevtools } or import { TanStackDevtools as X }
59 if (
60 spec.type === 'ImportSpecifier' &&
61 spec.imported.type === 'Identifier' &&
62 spec.imported.name === 'TanStackDevtools'
63 ) {
64 componentName = spec.local.name
65 return
66 }
67 // import * as X from '...'
68 if (spec.type === 'ImportNamespaceSpecifier') {
69 componentName = `${spec.local.name}.TanStackDevtools`
70 return
71 }
72 }
73 })
74
75 return componentName
76 } catch (e) {
77 return null
78 }
79}
80
81/**
82 * Check if a plugin already exists in the array expression

Callers 2

testTransformFunction · 0.90
injectPluginIntoFileFunction · 0.85

Calls 2

walkFunction · 0.90
isTanStackDevtoolsImportFunction · 0.90

Tested by 1

testTransformFunction · 0.72