MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / resolveContext

Function resolveContext

src/resolution/frameworks/react.ts:2486–2512  ·  view source on GitHub ↗

* Resolve a context reference using name-based lookup

(name: string, ref: UnresolvedRef, context: ResolutionContext)

Source from the content-addressed store, hash-verified

2484 * Resolve a context reference using name-based lookup
2485 */
2486function 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}

Callers 1

resolveFunction · 0.85

Calls 2

isEsmLanguageFunction · 0.85
getNodesByNameMethod · 0.65

Tested by

no test coverage detected