diff --git a/app/src/pages/route-inspector.ts b/app/src/pages/route-inspector.ts index e0949459..26af6baa 100644 --- a/app/src/pages/route-inspector.ts +++ b/app/src/pages/route-inspector.ts @@ -1,9 +1,11 @@ -import { Component, input, signal, effect } from '@angular/core'; +import { Component, input, signal, effect, computed } from '@angular/core'; import type { DevframeRpcClient } from 'devframe/client'; interface RouteInfo { path: string; component?: string; + redirectTo?: string; + title?: string; hasChildren: boolean; file: string; } @@ -14,11 +16,12 @@ interface RouteInfo {
- +
@if (loading()) { @@ -29,17 +32,25 @@ interface RouteInfo { - - - - + + + + + - @for (route of filtered(); track route.path + route.file) { + @for (route of filtered(); track $index) { - + + @@ -114,6 +125,10 @@ interface RouteInfo { color: var(--accent); font-weight: 500; } + .redirect { + font-family: monospace; + color: #38bdf8; + } .file { font-size: 12px; color: #71717a; @@ -127,21 +142,32 @@ export class RouteInspector { filter = signal(''); loading = signal(false); - filtered = signal([]); + filtered = computed(() => { + const q = this.filter().toLowerCase().trim(); + const all = this.routes(); + if (!q) return all; + return all.filter( + (r) => + r.path.toLowerCase().includes(q) || + (r.component && r.component.toLowerCase().includes(q)) || + (r.redirectTo && r.redirectTo.toLowerCase().includes(q)) || + (r.title && r.title.toLowerCase().includes(q)) || + r.file.toLowerCase().includes(q), + ); + }); constructor() { - effect(() => { - const q = this.filter().toLowerCase(); - const all = this.routes(); - this.filtered.set(q ? all.filter((r) => r.path.includes(q) || r.file.includes(q)) : all); - }); - effect(() => { const client = this.rpc(); if (client) this.refresh(); }); } + onFilterInput(event: Event) { + const target = event.target as HTMLInputElement | null; + this.filter.set(target?.value ?? ''); + } + async refresh() { const client = this.rpc(); if (!client) return; diff --git a/packages/ng-devtools/src/rpc/__tests__/get-routes.test.ts b/packages/ng-devtools/src/rpc/__tests__/get-routes.test.ts index a0af98ca..bb92f392 100644 --- a/packages/ng-devtools/src/rpc/__tests__/get-routes.test.ts +++ b/packages/ng-devtools/src/rpc/__tests__/get-routes.test.ts @@ -20,6 +20,151 @@ describe('get-routes', () => { ]); }); + it('reads redirectTo on redirection routes', async () => { + const routes = await routesFor(`[ + { path: '', redirectTo: 'dashboard', pathMatch: 'full' }, + { path: 'home', redirectTo: '/dashboard' }, + { path: 'login', redirectTo: \`auth/login\` }, + ]`); + expect(routes.map((r) => [r.path, r.redirectTo])).toEqual([ + ['', 'dashboard'], + ['home', '/dashboard'], + ['login', 'auth/login'], + ]); + }); + + it('reads title on route definitions', async () => { + const routes = await routesFor(`[ + { path: 'home', component: HomeComponent, title: 'Home Page' }, + { path: 'settings', component: SettingsComponent, title: "Settings | App" }, + { path: 'about', title: 'About {us}', component: AboutComponent }, + ]`); + expect(routes.map((r) => [r.path, r.title])).toEqual([ + ['home', 'Home Page'], + ['settings', 'Settings | App'], + ['about', 'About {us}'], + ]); + }); + + it('reads full route configuration with redirectTo and title', async () => { + const routes = await routesFor(`[ + { path: '', redirectTo: 'home', pathMatch: 'full' }, + { path: 'home', component: HomeComponent, title: 'Home' }, + { + path: 'dashboard', + component: DashboardComponent, + title: 'Dashboard', + children: [{ path: 'stats', component: StatsComponent }], + }, + ]`); + expect(routes).toEqual([ + { + path: '', + redirectTo: 'home', + hasChildren: false, + file: 'src/app.routes.ts', + }, + { + path: 'home', + component: 'HomeComponent', + title: 'Home', + hasChildren: false, + file: 'src/app.routes.ts', + }, + { + path: 'dashboard', + component: 'DashboardComponent', + title: 'Dashboard', + hasChildren: true, + file: 'src/app.routes.ts', + }, + { + path: 'stats', + component: 'StatsComponent', + hasChildren: false, + file: 'src/app.routes.ts', + }, + ]); + }); + + it('marks function or expression title / redirectTo as (dynamic)', async () => { + const routes = await routesFor(`[ + { path: 'custom-title', component: HomeComponent, title: customTitleResolver }, + { path: 'custom-redirect', redirectTo: () => '/fallback' }, + { path: 'template-title', component: HomeComponent, title: \`Home \${id}\` }, + ]`); + expect(routes.map((r) => [r.path, r.title, r.redirectTo])).toEqual([ + ['custom-title', '(dynamic)', undefined], + ['custom-redirect', undefined, '(dynamic)'], + ['template-title', '(dynamic)', undefined], + ]); + }); + + it('handles string literal escapes, unescapes, and concatenated non-literals', async () => { + const routes = await routesFor(`[ + { path: 'about', title: 'It\\'s "special"', redirectTo: "my/\\"path\\"" }, + { path: '\\u0068ome', title: 'Line1\\nLine2', redirectTo: \`Price \\\${amount}\` }, + { path: 'a' + 'b', component: Nope }, + ]`); + expect(routes).toEqual([ + { + path: 'about', + title: 'It\'s "special"', + redirectTo: 'my/"path"', + hasChildren: false, + file: 'src/app.routes.ts', + }, + { + path: 'home', + title: 'Line1\nLine2', + redirectTo: 'Price ${amount}', + hasChildren: false, + file: 'src/app.routes.ts', + }, + ]); + }); + + it('reads redirectTo and title on nested child routes and wildcard routes', async () => { + const routes = await routesFor(`[ + { + path: 'admin', + title: 'Admin Panel', + children: [ + { path: '', redirectTo: 'overview', pathMatch: 'full' }, + { path: 'overview', component: AdminOverview, title: 'Overview' }, + ], + }, + { path: '**', redirectTo: '' }, + ]`); + expect(routes).toEqual([ + { + path: 'admin', + title: 'Admin Panel', + hasChildren: true, + file: 'src/app.routes.ts', + }, + { + path: '', + redirectTo: 'overview', + hasChildren: false, + file: 'src/app.routes.ts', + }, + { + path: 'overview', + component: 'AdminOverview', + title: 'Overview', + hasChildren: false, + file: 'src/app.routes.ts', + }, + { + path: '**', + redirectTo: '', + hasChildren: false, + file: 'src/app.routes.ts', + }, + ]); + }); + it('reads the export name of a lazy component', async () => { const routes = await routesFor(`[ { path: '', loadComponent: () => import('./home').then(m => m.Home) }, diff --git a/packages/ng-devtools/src/rpc/get-routes.ts b/packages/ng-devtools/src/rpc/get-routes.ts index 6403abb7..fdd617a3 100644 --- a/packages/ng-devtools/src/rpc/get-routes.ts +++ b/packages/ng-devtools/src/rpc/get-routes.ts @@ -15,10 +15,14 @@ import { const RouteSchema = v.object({ path: v.string(), component: v.optional(v.string()), + redirectTo: v.optional(v.string()), + title: v.optional(v.string()), hasChildren: v.boolean(), file: v.string(), }); +type ExtractedRoute = v.InferOutput; + export const getRoutes = defineRpcFunction({ name: 'get-routes', type: 'query', @@ -35,17 +39,13 @@ export const getRoutes = defineRpcFunction({ }), }); -function extractRoutes(cwd: string) { - const routes: { path: string; component?: string; hasChildren: boolean; file: string }[] = []; +function extractRoutes(cwd: string): ExtractedRoute[] { + const routes: ExtractedRoute[] = []; for (const root of sourceRoots(cwd)) findRouteFiles(root, cwd, routes); return routes; } -function findRouteFiles( - dir: string, - cwd: string, - routes: { path: string; component?: string; hasChildren: boolean; file: string }[], -) { +function findRouteFiles(dir: string, cwd: string, routes: ExtractedRoute[]) { let entries: string[]; try { entries = readdirSync(dir); @@ -71,15 +71,17 @@ function findRouteFiles( try { const content = readFileSync(full, 'utf-8'); - const relPath = relative(cwd, full); + const relPath = relative(cwd, full).replaceAll('\\', '/'); for (const body of objectLiterals(stripComments(content))) { const props = topLevelProps(body); - const path = props.get('path')?.match(/^['"`]([^'"`]*)['"`]$/)?.[1]; + const path = stringLiteral(props.get('path')); if (path === undefined) continue; routes.push({ path, component: routeComponent(props), + redirectTo: routeRedirectTo(props), + title: routeTitle(props), // `loadChildren` has children too, it just loads them lazily. hasChildren: props.has('children') || props.has('loadChildren'), file: relPath, @@ -91,6 +93,68 @@ function findRouteFiles( } } +function decodeEscapes(s: string): string { + return s.replace( + /\\(?:(u[0-9a-fA-F]{4}|x[0-9a-fA-F]{2})|([nrtbfv0\\])|(\r\n|[\r\n\u2028\u2029])|(.))/g, + ( + _: string, + hex: string | undefined, + char: string | undefined, + lineCont: string | undefined, + anyChar: string | undefined, + ) => { + if (hex) return String.fromCharCode(parseInt(hex.slice(1), 16)); + if (char) { + switch (char) { + case 'n': + return '\n'; + case 'r': + return '\r'; + case 't': + return '\t'; + case 'b': + return '\b'; + case 'f': + return '\f'; + case 'v': + return '\v'; + case '0': + return '\0'; + case '\\': + return '\\'; + default: + return char; + } + } + if (lineCont) return ''; + return anyChar ?? ''; + }, + ); +} + +function stringLiteral(v?: string): string | undefined { + if (!v) return undefined; + const quote = v[0]; + if (quote !== "'" && quote !== '"' && quote !== '`') return undefined; + if (skipString(v, 0) !== v.length - 1) return undefined; + if (quote === '`' && /(^|[^\\])(?:\\\\)*\$\{/.test(v)) return undefined; + return decodeEscapes(v.slice(1, -1)); +} + +function routeRedirectTo(props: Map): string | undefined { + const val = props.get('redirectTo'); + if (val === undefined) return undefined; + const literal = stringLiteral(val); + return literal !== undefined ? literal : '(dynamic)'; +} + +function routeTitle(props: Map): string | undefined { + const val = props.get('title'); + if (val === undefined) return undefined; + const literal = stringLiteral(val); + return literal !== undefined ? literal : '(dynamic)'; +} + function routeComponent(props: Map): string | undefined { const eager = props.get('component')?.match(/^(\w+)/)?.[1]; if (eager) return eager; diff --git a/packages/ng-devtools/src/types.ts b/packages/ng-devtools/src/types.ts index 275a57f5..b3fc4b7c 100644 --- a/packages/ng-devtools/src/types.ts +++ b/packages/ng-devtools/src/types.ts @@ -10,6 +10,8 @@ export interface ComponentNode { export interface RouteInfo { path: string; component?: string; + redirectTo?: string; + title?: string; hasChildren: boolean; guards?: string[]; }
PathComponentFileChildrenPathComponent / TargetTitleFileChildren
/{{ route.path }}{{ route.component ?? '—' }} + @if (route.redirectTo !== undefined) { + ➜ {{ route.redirectTo }} + } @else { + {{ route.component ?? '—' }} + } + {{ route.title ?? '—' }} {{ route.file }} {{ route.hasChildren ? 'Yes' : '—' }}