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

Function exportedComponent

src/resolution/frameworks/react.ts:538–604  ·  view source on GitHub ↗
(
  file: string,
  name: string,
  context: ResolutionContext,
  visited: ReadonlySet<string> = new Set(),
  reads?: Set<string>
)

Source from the content-addressed store, hash-verified

536 * too (`jsxChild`).
537 */
538export function exportedComponent(
539 file: string,
540 name: string,
541 context: ResolutionContext,
542 visited: ReadonlySet<string> = new Set(),
543 reads?: Set<string>
544): string | null {
545 if (visited.has(file) || visited.size > MAX_REEXPORT_HOPS) return null;
546 reads?.add(file);
547 const language = scriptLanguage(file);
548 const through = new Set(visited).add(file);
549 const forwarded = (source: string, exported: string): string | null => {
550 const target = resolveImportPath(source, file, language, context);
551 return target ? exportedComponent(target, exported, context, through, reads) : null;
552 };
553 const code = stripCommentsForRegex(context.readFile(file) ?? '', 'typescript');
554 const local = ownExport(code, name);
555 if (local) {
556 const nodes = context.getNodesInFile(file);
557 const declaredAs = (binding: string): Node | undefined => {
558 const named = nodes.filter((n) => n.name === binding && EXPORTED_COMPONENT_KINDS.has(n.kind));
559 return named.find((n) => n.qualifiedName === binding) ?? named[0];
560 };
561 const exported = declaredAs(local);
562 // A constant that holds another binding is that binding: outline's
563 // `Avatar/index.ts` is `const Avatar = AvatarWithHoverCard; export { Avatar }`.
564 // (A `let` may be assigned another component before it is used.)
565 let binding = local;
566 let declared = exported;
567 for (let hop = 0; declared && hop < MAX_REEXPORT_HOPS; hop++) {
568 const held = declared.kind === 'constant' ? heldBinding(declared, context) : null;
569 if (!held) break;
570 binding = held;
571 declared = declaredAs(held);
572 }
573 // A value is what it wraps: `export default Page` of `const Page = observer(function Page_() {…})`.
574 // So is a component node minted for a wrapper around another component
575 // (`const MemoizedBlurhash = memo(Blurhash)`), which has no body to render from.
576 if (declared) {
577 const wrapped = declared.kind === 'constant' || declared.kind === 'variable' ? wrappedComponent(declared, context)
578 : declared.kind === 'component' ? handedComponent(declared, context) : null;
579 return (wrapped ?? declared).id;
580 }
581 // A name the module imports and exports again — `import { Page } from
582 // './page'; export { Page }`, `import Page from './Page'; export default
583 // Page` — is the page that import names. `export default withAuth(Page)`
584 // hands over no binding of its own.
585 if (binding === local && name === 'default' && !exportsBinding(code, local)) return null;
586 const imported = context.getImportMappings(file, language).find((m) => m.localName === binding);
587 const id = imported && !imported.isNamespace ? forwarded(imported.source, imported.isDefault ? 'default' : imported.exportedName) : null;
588 // A held binding that leads nowhere leaves the value itself the export.
589 return id ?? exported?.id ?? null;
590 }
591 const reExports = context.getReExports?.(file, language) ?? [];
592 for (const re of reExports) {
593 if (re.kind === 'named' && re.exportedName === name) return forwarded(re.source, re.originalName);
594 }
595 if (name === 'default') return null;

Callers 5

importedTagModulesFunction · 0.90
exportOfFunction · 0.90
lazyRouteComponentFunction · 0.85
pickedExportFunction · 0.85
forwardedFunction · 0.85

Calls 15

stripCommentsForRegexFunction · 0.90
scriptLanguageFunction · 0.85
ownExportFunction · 0.85
declaredAsFunction · 0.85
heldBindingFunction · 0.85
wrappedComponentFunction · 0.85
handedComponentFunction · 0.85
exportsBindingFunction · 0.85
forwardedFunction · 0.85
hasMethod · 0.80
getImportMappingsMethod · 0.80
getReExportsMethod · 0.80

Tested by

no test coverage detected