| 40 | * Handles renamed imports and namespace imports |
| 41 | */ |
| 42 | export 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 |