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

Function parseAngularRoutes

src/resolution/frameworks/angular-router.ts:257–346  ·  view source on GitHub ↗
(content: string)

Source from the content-addressed store, hash-verified

255 * text: `path` may be written after `component`.
256 */
257export function parseAngularRoutes(content: string): { routes: AngularRoute[]; mounts: AngularMount[]; redirects: AngularRedirect[] } {
258 const routes: AngularRoute[] = [];
259 const mounts: AngularMount[] = [];
260 const redirects: AngularRedirect[] = [];
261 if (!ROUTER_IMPORT.test(content)) return { routes, mounts, redirects };
262 const safe = stripCommentsForRegex(content, 'typescript');
263 const lineOf = (at: number) => lineAt(safe, at);
264 const seenPaths = new Set<string>();
265 const walked = new Set<number>();
266
267 const walk = (open: number, close: number, prefix: readonly string[], depth: number, layouts: readonly AngularComponentRef[]): void => {
268 if (walked.has(open) || depth > 12) return;
269 walked.add(open);
270 for (const obj of topLevelObjects(safe, open + 1, close)) {
271 const fields = readFields(safe, obj.start, obj.end);
272 if (!ROUTE_KEYS.some((k) => fields.has(k))) continue;
273 // A custom `matcher` decides the address at run time — no path to name.
274 if (fields.has('matcher')) continue;
275 const pathField = fields.get('path');
276 // `{ path: '**', redirectTo: 'home' }`: where an address nothing else matches lands.
277 if (pathField && fields.has('redirectTo') && staticString(pathField.text) === '**') {
278 const target = staticString(fields.get('redirectTo')!.text);
279 if (target !== null && !target.includes('**')) {
280 const absolute = target.startsWith('/');
281 redirects.push({ from: joinPath([...prefix, '**']), to: joinPath([...(absolute ? [] : prefix), ...target.split('/').filter((t) => t.length > 0)]), absolute });
282 }
283 continue;
284 }
285 const own = pathField ? pathSegments(pathField.text) : [];
286 if (own === null) continue; // a computed path, or a `**` catch-all
287 const segs = [...prefix, ...own];
288 const line = lineOf(pathField ? pathField.at : obj.start);
289
290 const redirectTo = fields.get('redirectTo');
291 if (redirectTo) {
292 // `{ path: '', redirectTo: 'home' }`: arriving at `/` is arriving at
293 // `/home`. A relative target is a sibling, under the same parent.
294 const target = staticString(redirectTo.text);
295 if (target !== null && !target.includes('**')) {
296 const absolute = target.startsWith('/');
297 const to = joinPath([...(absolute ? [] : prefix), ...target.split('/').filter((t) => t.length > 0)]);
298 redirects.push({ from: joinPath(segs), to, absolute });
299 }
300 continue;
301 }
302
303 const loadChildren = fields.get('loadChildren');
304 if (loadChildren) {
305 const lazy = lazyImport(loadChildren.text);
306 if (lazy) mounts.push({ prefix: joinPath(segs), spec: lazy.spec, line });
307 continue;
308 }
309 const componentField = fields.get('component') ? 'component' : fields.get('loadComponent') ? 'loadComponent' : null;
310 const component = componentField ? componentRef(componentField, fields.get(componentField)!.text) : null;
311
312 const children = fields.get('children');
313 let childClaimsAddress = false;
314 if (children) {

Callers 4

angularMountsFunction · 0.85
angularRouteTableFunction · 0.85
extractFunction · 0.85

Calls 4

stripCommentsForRegexFunction · 0.90
matchBracketFunction · 0.90
walkFunction · 0.70
execMethod · 0.65

Tested by

no test coverage detected