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

Function tableRoutes

src/resolution/frameworks/react.ts:1985–2054  ·  view source on GitHub ↗

* Routes written in a table that another file hands to the router. * * The ASP.NET Core React template keeps them in `AppRoutes.js` — * `[{ index: true, element: }, { path: '/counter', element: * }, …]` — and renders them from `App.js` with * `AppRoutes.map(({ element, ...r

(context: ResolutionContext)

Source from the content-addressed store, hash-verified

1983 * new here.
1984 */
1985function tableRoutes(context: ResolutionContext): FrameworkExtractionResult {
1986 const nodes: Node[] = [];
1987 const references: UnresolvedRef[] = [];
1988 const consumers = new Set<string>();
1989 for (const pkg of ROUTER_PACKAGES) {
1990 for (const n of context.getNodesByName(pkg)) {
1991 if (n.kind === 'import' && !isTestPath(n.filePath)) consumers.add(n.filePath);
1992 }
1993 }
1994 if (consumers.size === 0) return { nodes, references };
1995 const queue: Array<{ file: string; at: number; prefix: string[]; layouts: string[] }> = [];
1996 const queued = new Set<string>();
1997 // A table renders inside the layouts around the place that names it. A
1998 // route's `layout:` reference is resolved in the route's own file, so they
1999 // go along only when that place is in the table's file.
2000 const enqueue = (table: { file: string; at: number } | null, prefix: string[], from: string, layouts: string[]): void => {
2001 if (!table || queued.size >= MAX_TABLES) return;
2002 const key = `${table.file}\0${table.at}\0${prefix.join('\0')}`;
2003 if (queued.has(key)) return;
2004 queued.add(key);
2005 queue.push({ ...table, prefix, layouts: table.file === from ? layouts : [] });
2006 };
2007 for (const file of [...consumers].sort()) {
2008 const source = context.readFile(file);
2009 // Most of the files that import React Router only link or navigate.
2010 if (!source || !CONSUMER_HINT.test(source)) continue;
2011 const scan = scanRoutes(source, allowsJsx(file), { objects: ROUTER_CALL.test(source), uses: true });
2012 for (const use of scan.uses) {
2013 const tables = use.literal !== undefined ? [{ file, at: use.literal }] : tableNamed(use.name!, file, context);
2014 const prefix = pathValues(use.prefix, file, context);
2015 for (const table of tables) enqueue(table, prefix, file, use.layouts);
2016 }
2017 }
2018 const made = new Set<string>();
2019 const now = Date.now();
2020 for (let next = queue.shift(); next; next = queue.shift()) {
2021 const { file, at, prefix, layouts } = next;
2022 const source = context.readFile(file);
2023 if (!source) continue;
2024 const scan = scanRoutes(source, allowsJsx(file), { table: { at, prefix: prefix.map((lit) => ({ lit })), layouts } });
2025 for (const named of scan.names) {
2026 const prefix = pathValues(named.prefix, file, context);
2027 for (const table of tableNamed(named, file, context)) enqueue(table, prefix, file, named.layouts);
2028 }
2029 if (ROUTER_CALL.test(source)) continue;
2030 const lineAt = makeLineAt(source, 1);
2031 for (const route of scan.routes) {
2032 const line = lineAt(route.at);
2033 const id = `route:${file}:${line}:table:${route.path}`;
2034 if (made.has(id)) continue;
2035 made.add(id);
2036 const values = route.parts.map((p) => p.lit ?? (p.expr ? constantPathValue(p.expr, file, context) : null));
2037 nodes.push({
2038 id,
2039 kind: 'route',
2040 name: values.every((v) => v !== null) ? composeRoutePath(values as string[]) : route.path,
2041 qualifiedName: `${file}::route:${route.path}`,
2042 filePath: file,

Callers 1

crossFileNodesFunction · 0.85

Calls 15

isTestPathFunction · 0.90
makeLineAtFunction · 0.90
scanRoutesFunction · 0.85
allowsJsxFunction · 0.85
tableNamedFunction · 0.85
pathValuesFunction · 0.85
enqueueFunction · 0.85
constantPathValueFunction · 0.85
composeRoutePathFunction · 0.85
routeReferencesFunction · 0.85
mapMethod · 0.80
hasMethod · 0.80

Tested by

no test coverage detected