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