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

Function resolve

src/resolution/frameworks/react.ts:46–160  ·  view source on GitHub ↗
(ref: UnresolvedRef, context: ResolutionContext)

Source from the content-addressed store, hash-verified

44 },
45
46 resolve(ref: UnresolvedRef, context: ResolutionContext): ResolvedRef | null {
47 // Components, hooks and contexts are a script's: halo's Java
48 // `import org.springframework…SecurityContext` is no React context.
49 if (!REACT_SCRIPT_LANGUAGES.has(ref.language)) return null;
50 if (ref.referenceName.startsWith(LAZY_ROUTE_PREFIX)) {
51 const target = lazyRouteComponent(ref.referenceName.slice(LAZY_ROUTE_PREFIX.length), ref.filePath, context);
52 return target ? { original: ref, targetNodeId: target, confidence: 0.9, resolvedBy: 'framework' } : null;
53 }
54 // The export a lazy route's loader picks. Vue Router and Angular name a
55 // lazily loaded component the same way, and their routes' are theirs.
56 const picked = PICKED_EXPORT.exec(ref.referenceName);
57 if (picked) {
58 const target = isReactRouteRef(ref) ? pickedExport(picked[1]!, picked[2]!, ref.filePath, context) : null;
59 return target ? { original: ref, targetNodeId: target, confidence: 0.9, resolvedBy: 'framework' } : null;
60 }
61 // The layout a route renders inside: what happens in it — its header's
62 // links, its logout — happens on the route's screen too.
63 if (ref.referenceName.startsWith(LAYOUT_PREFIX)) {
64 const target = layoutComponent(ref.referenceName.slice(LAYOUT_PREFIX.length), ref, context);
65 return target ? { original: ref, targetNodeId: target, confidence: 0.9, resolvedBy: 'framework', metadata: { layout: true } } : null;
66 }
67 // A component, hook or context the file IMPORTS is the import's: the
68 // package's (`useQuery` from `@tanstack/react-query`, `<Button>` from a UI
69 // kit), or the module the import names, which import resolution finds.
70 // Framework resolution runs first, and a name lookup here bound trpc's
71 // tests' `useQuery` to a hook nested in one of trpc's own factories.
72 if (context.getImportMappings?.(ref.filePath, ref.language)?.some((m) => m.localName === ref.referenceName)) {
73 return null;
74 }
75
76 // Pattern 1: Component references (PascalCase). Only from JSX-capable
77 // files — a component is USED in markup, which only parses in .tsx/.jsx.
78 // Without this gate, every PascalCase TYPE reference in plain .ts files
79 // went through component resolution: in a monorepo with same-named
80 // classes per package (#764, amplication), a `.ts` GraphQL-types file's
81 // own `Account` type alias lost to an arbitrary `Account` CLASS in
82 // another package (the framework's 0.8 outranked the name-matcher's
83 // proximity-correct 0.7).
84 if (
85 (ref.language === 'tsx' || ref.language === 'jsx') &&
86 isPascalCase(ref.referenceName) &&
87 !isBuiltInType(ref.referenceName)
88 ) {
89 // A type annotation is name matching's, which scopes a type as
90 // TypeScript does — the file's own, an import's or a global one — and
91 // never a component elsewhere: component lookup handed the `Option`
92 // annotations of outline's InputSelect.tsx, which declares `type
93 // Option`, to the `Option` component of Export/ExportDialog.tsx, and
94 // mastodon's `<Item>` type parameters to the `Item` class of
95 // media_gallery.jsx. Only a type alias or interface the file declares
96 // beside a value or a component of its name is decided here, where both
97 // are the file's own: the annotation is the type, as bigbluebutton's
98 // `type ModalRegistration` is beside its render-prop component `const
99 // ModalRegistration = (…) =>`. (A class is a type and a value at once.)
100 const own = ownDeclarations(ref.referenceName, ref.filePath, context);
101 if (namesType(ref)) {
102 const type = own.find((n) => n.kind === 'type_alias' || n.kind === 'interface');
103 return type && own.some((n) => n.kind !== 'type_alias' && n.kind !== 'interface' && n.kind !== 'class')

Callers

nothing calls this directly

Calls 15

lazyRouteComponentFunction · 0.85
isReactRouteRefFunction · 0.85
pickedExportFunction · 0.85
layoutComponentFunction · 0.85
isBuiltInTypeFunction · 0.85
ownDeclarationsFunction · 0.85
namesTypeFunction · 0.85
declaredComponentFunction · 0.85
resolveHookFunction · 0.85
resolveContextFunction · 0.85
hasMethod · 0.80
getImportMappingsMethod · 0.80

Tested by

no test coverage detected