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

Function parseTanstackRoutes

src/resolution/frameworks/tanstack-router.ts:167–237  ·  view source on GitHub ↗
(content: string)

Source from the content-addressed store, hash-verified

165 * address belongs to the index route under it.
166 */
167export function parseTanstackRoutes(content: string): TanstackRouteEntry[] {
168 if (!ROUTE_FACTORY.test(content)) return [];
169 const safe = stripCommentsForRegex(content, 'typescript');
170 const out: TanstackRouteEntry[] = [];
171 const lineOf = (index: number): number => safe.slice(0, index).split('\n').length;
172
173 // ---- file-based: the path is the first argument, the options follow ----
174 const fileRoutes = /\bcreate(?:Lazy)?FileRoute\s*\(/g;
175 let f: RegExpExecArray | null;
176 while ((f = fileRoutes.exec(safe)) !== null) {
177 const open = f.index + f[0].length - 1;
178 const close = matchBracket(safe, open);
179 if (close < 0) continue;
180 const raw = readStringAt(safe.slice(open + 1, close).trimStart(), 0);
181 if (raw === null) continue;
182 const path = tanstackPath(raw);
183 if (path === null || isPathlessLayout(raw)) continue;
184 out.push({
185 path,
186 component: componentIn(chainAfter(safe, close + 1)),
187 // `createFileRoute('/dashboard/')` is the index page AT `/dashboard`;
188 // `createFileRoute('/dashboard')` is the layout around it.
189 index: raw.length > 1 && raw.endsWith('/'),
190 fileBased: true,
191 line: lineOf(f.index),
192 });
193 }
194
195 // ---- code-based: a fragment per route, composed through its parent ----
196 const decls = new Map<string, { path: string | null; parent: string | null; component: string | null; root: boolean; index: number }>();
197 const named = /\b(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*(?::[^=]*?)?=\s*create(Root)?Route\s*\(\s*\{/g;
198 let d: RegExpExecArray | null;
199 while ((d = named.exec(safe)) !== null) {
200 const brace = safe.indexOf('{', d.index + d[0].length - 1);
201 const end = matchBracket(safe, brace);
202 if (end < 0) continue;
203 const fields = readFields(safe, brace, end);
204 const pathField = fields.get('path');
205 const path = d[2] ? '/' : pathField ? readStringAt(pathField.text.trimStart(), 0) : null;
206 const parentField = fields.get('getParentRoute');
207 const parent = parentField ? (/=>\s*([A-Za-z_$][\w$]*)/.exec(parentField.text)?.[1] ?? null) : null;
208 const componentField = fields.get('component');
209 decls.set(d[1]!, {
210 path,
211 parent,
212 component: componentField ? componentIn(componentField.text) : null,
213 root: d[2] !== undefined,
214 index: d.index,
215 });
216 }
217 // A route with an index child is the LAYOUT around that address; the child
218 // with `path: '/'` is what renders there. A parent with no index child still
219 // is the page at its own address — its outlet is simply empty.
220 const wrapsAnIndex = new Set(
221 [...decls.values()].filter((r) => r.path === '/' && r.parent !== null).map((r) => r.parent!)
222 );
223 for (const [name, decl] of decls) {
224 if (decl.path === null) continue; // a pathless layout contributes no address

Callers 2

extractFunction · 0.85

Calls 15

stripCommentsForRegexFunction · 0.90
matchBracketFunction · 0.90
readStringAtFunction · 0.90
readFieldsFunction · 0.90
tanstackPathFunction · 0.85
isPathlessLayoutFunction · 0.85
chainAfterFunction · 0.85
composePathFunction · 0.85
pushMethod · 0.80
mapMethod · 0.80
hasMethod · 0.80
componentInFunction · 0.70

Tested by

no test coverage detected