* Resolve a context reference using name-based lookup
(name: string, ref: UnresolvedRef, context: ResolutionContext)
| 2484 | * Resolve a context reference using name-based lookup |
| 2485 | */ |
| 2486 | function resolveContext(name: string, ref: UnresolvedRef, context: ResolutionContext): string | null { |
| 2487 | // In a JS/TS module only the file's own context is in reach by name; another |
| 2488 | // file's comes through an import (trpc's adapters' `createContext?.(…)` is an |
| 2489 | // option, not an example app's `createContext`). |
| 2490 | if (isEsmLanguage(ref.language)) { |
| 2491 | return context.getNodesByName(name).find((n) => n.filePath === ref.filePath)?.id ?? null; |
| 2492 | } |
| 2493 | const candidates = context.getNodesByName(name); |
| 2494 | if (candidates.length === 0) { |
| 2495 | // Try without Context/Provider suffix |
| 2496 | const baseName = name.replace(/Context$|Provider$/, ''); |
| 2497 | if (baseName !== name) { |
| 2498 | const baseCandidates = context.getNodesByName(baseName); |
| 2499 | if (baseCandidates.length > 0) return baseCandidates[0]!.id; |
| 2500 | } |
| 2501 | return null; |
| 2502 | } |
| 2503 | |
| 2504 | // Prefer context directories |
| 2505 | const CONTEXT_DIRS = ['/context/', '/contexts/', '/src/context/', '/src/contexts/', '/providers/', '/src/providers/']; |
| 2506 | const preferred = candidates.filter((n) => |
| 2507 | CONTEXT_DIRS.some((d) => n.filePath.includes(d)) |
| 2508 | ); |
| 2509 | if (preferred.length > 0) return preferred[0]!.id; |
| 2510 | |
| 2511 | return candidates[0]!.id; |
| 2512 | } |
no test coverage detected