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

Function angularRouteTable

src/resolution/frameworks/angular-router.ts:709–772  ·  view source on GitHub ↗
(context: ResolutionContext)

Source from the content-addressed store, hash-verified

707const tables = new WeakMap<ResolutionContext, AngularRouteTable>();
708
709export function angularRouteTable(context: ResolutionContext): AngularRouteTable {
710 const all = context.getNodesByKind('route');
711 const cached = tables.get(context);
712 if (cached && cached.source === all) return cached;
713 const byRoot = new Map<string, RouteTable>();
714 const byFile = new Map<string, Node>();
715 for (const node of all) {
716 if (!inAngularTable(node)) continue;
717 const root = angularAppRoot(node.filePath, context);
718 let t = byRoot.get(root);
719 if (!t) byRoot.set(root, (t = { source: all, exact: new Map(), dynamic: [] }));
720 addRouteTo(t, node.name, node);
721 if (!byFile.has(node.filePath)) byFile.set(node.filePath, node);
722 }
723 // Redirects: arriving at `/` is arriving at wherever `redirectTo` sends
724 // the user. A file's mount prefix is what its routes' names add to their
725 // in-file paths. A chain (`/` → `/pages` → `/pages/dashboard`) settles in
726 // a few passes.
727 const aliases: Array<{ table: RouteTable; from: string; to: string }> = [];
728 const mounts = byFile.size > 0 ? angularMounts(context, all.filter(isAngularRoute)) : null;
729 const redirectFiles = new Map<string, string[]>();
730 for (const [file, sample] of byFile) {
731 const own = resolvedSegments(inFilePath(sample), file, context);
732 const full = sample.name.split('/').filter((seg) => seg.length > 0);
733 redirectFiles.set(file, full.slice(0, Math.max(0, full.length - own.length)));
734 }
735 // A file that only mounts others can still redirect: its prefix is where it is mounted.
736 for (const file of mounts?.mountFiles ?? []) {
737 if (!redirectFiles.has(file)) redirectFiles.set(file, mounts!.prefixOf(file));
738 }
739 for (const [file, prefix] of redirectFiles) {
740 const content = context.readFile(file);
741 if (!content || !content.includes('redirectTo')) continue;
742 const table = byRoot.get(angularAppRoot(file, context)) ?? (byRoot.size === 1 ? [...byRoot.values()][0]! : undefined);
743 if (!table) continue;
744 for (const r of parseAngularRoutes(content).redirects) {
745 const from = joinPath([...prefix, ...resolvedSegments(r.from, file, context)]);
746 const to = joinPath([...(r.absolute ? [] : prefix), ...resolvedSegments(r.to, file, context)]);
747 aliases.push({ table, from, to });
748 }
749 }
750 for (let pass = 0; pass < 4; pass++) {
751 let added = false;
752 for (const a of aliases) {
753 const target = a.table.exact.get(a.to);
754 if (target && !a.table.exact.has(a.from)) {
755 a.table.exact.set(a.from, target);
756 added = true;
757 }
758 }
759 if (!added) break;
760 }
761 // `/` that no route or redirect serves falls through to the app's root
762 // `**` redirect. Only `/`: letting the wildcard answer any path would draw
763 // every destination this reading could not name as a trip home.
764 for (const a of aliases) {
765 if (a.from !== '/**') continue;
766 const target = a.table.exact.get(a.to);

Callers 2

angularTemplateEdgesFunction · 0.90
resolveFunction · 0.85

Calls 15

addRouteToFunction · 0.90
inAngularTableFunction · 0.85
angularAppRootFunction · 0.85
angularMountsFunction · 0.85
resolvedSegmentsFunction · 0.85
inFilePathFunction · 0.85
parseAngularRoutesFunction · 0.85
hasMethod · 0.80
prefixOfMethod · 0.80
pushMethod · 0.80
joinPathFunction · 0.70
getNodesByKindMethod · 0.65

Tested by

no test coverage detected