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

Function layoutComponent

src/resolution/frameworks/react.ts:915–935  ·  view source on GitHub ↗

* The component a route's `layout:` reference names, found the way the * route's own component is: through the route file's import of it, else * declared in reach — or a lazily loaded layout module's component, or the * export its loader picks.

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

Source from the content-addressed store, hash-verified

913 * export its loader picks.
914 */
915function layoutComponent(spec: string, ref: UnresolvedRef, context: ResolutionContext): string | null {
916 if (spec.startsWith(LAZY_ROUTE_PREFIX)) return lazyRouteComponent(spec.slice(LAZY_ROUTE_PREFIX.length), ref.filePath, context);
917 const picked = isReactRouteRef(ref) ? PICKED_EXPORT.exec(spec) : null;
918 if (picked) return pickedExport(picked[1]!, picked[2]!, ref.filePath, context);
919 const mapping = context.getImportMappings(ref.filePath, ref.language).find((m) => m.localName === spec);
920 if (!mapping) {
921 // A layout the route file declares itself (`const MainLayout =
922 // Loadable(lazy(…))`): no name match follows a `layout:` reference, so
923 // the declaration binds here when there is no component to find.
924 const own = declaredComponent(spec, ref.filePath, context);
925 if (own) return (own.component ?? own.declaration).id;
926 return resolveComponent(spec, ref.filePath, context);
927 }
928 const viaImport = context.resolveImport?.({ ...ref, referenceName: spec })?.targetNodeId;
929 if (viaImport) return viaImport;
930 // `import { default as AppRoot } from './routes/app/root'`: read the module the
931 // import names — never a same-named component somewhere else.
932 if (mapping.isDefault || mapping.exportedName === 'default') return lazyRouteComponent(mapping.source, ref.filePath, context);
933 const file = resolveImportPath(mapping.source, ref.filePath, ref.language, context);
934 return file ? context.getNodesInFile(file).find((n) => n.name === mapping.exportedName && COMPONENT_KINDS.has(n.kind))?.id ?? null : null;
935}
936
937interface RouteDeclaration {
938 /** The route's full path as far as the file tells: nesting composed, a constant part shown as `{expr}`. */

Callers 1

resolveFunction · 0.85

Calls 11

resolveImportPathFunction · 0.90
lazyRouteComponentFunction · 0.85
isReactRouteRefFunction · 0.85
pickedExportFunction · 0.85
declaredComponentFunction · 0.85
getImportMappingsMethod · 0.80
resolveImportMethod · 0.80
hasMethod · 0.80
resolveComponentFunction · 0.70
execMethod · 0.65
getNodesInFileMethod · 0.65

Tested by

no test coverage detected