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

Function angularRouteTable

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

Source from the content-addressed store, hash-verified

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

Tested by

no test coverage detected