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

Function parseTanstackRoutes

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

Source from the content-addressed store, hash-verified

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

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
optionsBraceFunction · 0.85
composePathFunction · 0.85
pushMethod · 0.80
mapMethod · 0.80
hasMethod · 0.80

Tested by

no test coverage detected