Skip to content

Commit 1f6c910

Browse files
authored
feat(routes): parse and display redirectTo, title, and route metadata (#10)
* feat(routes): parse and display redirectTo, title, and route metadata (#9) * fix(routes): address review feedback on string parsing, dynamic markers, track key, and test cases * fix(routes): add JavaScript escape sequence decoder for route metadata string literals * fix(routes): preserve escaped template interpolation and explicitly type escape regex captures * style: enforce prettier formatting across route inspector and types * fix(routes): decode string and template literal escape sequences and handle escaped interpolations
1 parent 13608f3 commit 1f6c910

4 files changed

Lines changed: 262 additions & 25 deletions

File tree

‎app/src/pages/route-inspector.ts‎

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import { Component, input, signal, effect } from '@angular/core';
1+
import { Component, input, signal, effect, computed } from '@angular/core';
22
import type { DevframeRpcClient } from 'devframe/client';
33

44
interface RouteInfo {
55
path: string;
66
component?: string;
7+
redirectTo?: string;
8+
title?: string;
79
hasChildren: boolean;
810
file: string;
911
}
@@ -14,11 +16,12 @@ interface RouteInfo {
1416
<div class="toolbar">
1517
<input
1618
type="text"
19+
aria-label="Filter routes"
1720
placeholder="Filter routes…"
1821
[value]="filter()"
19-
(input)="filter.set($any($event.target).value)"
22+
(input)="onFilterInput($event)"
2023
/>
21-
<button (click)="refresh()">Refresh</button>
24+
<button type="button" (click)="refresh()">Refresh</button>
2225
</div>
2326
2427
@if (loading()) {
@@ -29,17 +32,25 @@ interface RouteInfo {
2932
<table role="table">
3033
<thead>
3134
<tr>
32-
<th>Path</th>
33-
<th>Component</th>
34-
<th>File</th>
35-
<th>Children</th>
35+
<th scope="col">Path</th>
36+
<th scope="col">Component / Target</th>
37+
<th scope="col">Title</th>
38+
<th scope="col">File</th>
39+
<th scope="col">Children</th>
3640
</tr>
3741
</thead>
3842
<tbody>
39-
@for (route of filtered(); track route.path + route.file) {
43+
@for (route of filtered(); track $index) {
4044
<tr>
4145
<td class="path">/{{ route.path }}</td>
42-
<td>{{ route.component ?? '—' }}</td>
46+
<td>
47+
@if (route.redirectTo !== undefined) {
48+
<span class="redirect">➜ {{ route.redirectTo }}</span>
49+
} @else {
50+
{{ route.component ?? '—' }}
51+
}
52+
</td>
53+
<td>{{ route.title ?? '—' }}</td>
4354
<td class="file">{{ route.file }}</td>
4455
<td>{{ route.hasChildren ? 'Yes' : '—' }}</td>
4556
</tr>
@@ -114,6 +125,10 @@ interface RouteInfo {
114125
color: var(--accent);
115126
font-weight: 500;
116127
}
128+
.redirect {
129+
font-family: monospace;
130+
color: #38bdf8;
131+
}
117132
.file {
118133
font-size: 12px;
119134
color: #71717a;
@@ -127,21 +142,32 @@ export class RouteInspector {
127142
filter = signal('');
128143
loading = signal(false);
129144

130-
filtered = signal<RouteInfo[]>([]);
145+
filtered = computed(() => {
146+
const q = this.filter().toLowerCase().trim();
147+
const all = this.routes();
148+
if (!q) return all;
149+
return all.filter(
150+
(r) =>
151+
r.path.toLowerCase().includes(q) ||
152+
(r.component && r.component.toLowerCase().includes(q)) ||
153+
(r.redirectTo && r.redirectTo.toLowerCase().includes(q)) ||
154+
(r.title && r.title.toLowerCase().includes(q)) ||
155+
r.file.toLowerCase().includes(q),
156+
);
157+
});
131158

132159
constructor() {
133-
effect(() => {
134-
const q = this.filter().toLowerCase();
135-
const all = this.routes();
136-
this.filtered.set(q ? all.filter((r) => r.path.includes(q) || r.file.includes(q)) : all);
137-
});
138-
139160
effect(() => {
140161
const client = this.rpc();
141162
if (client) this.refresh();
142163
});
143164
}
144165

166+
onFilterInput(event: Event) {
167+
const target = event.target as HTMLInputElement | null;
168+
this.filter.set(target?.value ?? '');
169+
}
170+
145171
async refresh() {
146172
const client = this.rpc();
147173
if (!client) return;

‎packages/ng-devtools/src/rpc/__tests__/get-routes.test.ts‎

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,151 @@ describe('get-routes', () => {
2020
]);
2121
});
2222

23+
it('reads redirectTo on redirection routes', async () => {
24+
const routes = await routesFor(`[
25+
{ path: '', redirectTo: 'dashboard', pathMatch: 'full' },
26+
{ path: 'home', redirectTo: '/dashboard' },
27+
{ path: 'login', redirectTo: \`auth/login\` },
28+
]`);
29+
expect(routes.map((r) => [r.path, r.redirectTo])).toEqual([
30+
['', 'dashboard'],
31+
['home', '/dashboard'],
32+
['login', 'auth/login'],
33+
]);
34+
});
35+
36+
it('reads title on route definitions', async () => {
37+
const routes = await routesFor(`[
38+
{ path: 'home', component: HomeComponent, title: 'Home Page' },
39+
{ path: 'settings', component: SettingsComponent, title: "Settings | App" },
40+
{ path: 'about', title: 'About {us}', component: AboutComponent },
41+
]`);
42+
expect(routes.map((r) => [r.path, r.title])).toEqual([
43+
['home', 'Home Page'],
44+
['settings', 'Settings | App'],
45+
['about', 'About {us}'],
46+
]);
47+
});
48+
49+
it('reads full route configuration with redirectTo and title', async () => {
50+
const routes = await routesFor(`[
51+
{ path: '', redirectTo: 'home', pathMatch: 'full' },
52+
{ path: 'home', component: HomeComponent, title: 'Home' },
53+
{
54+
path: 'dashboard',
55+
component: DashboardComponent,
56+
title: 'Dashboard',
57+
children: [{ path: 'stats', component: StatsComponent }],
58+
},
59+
]`);
60+
expect(routes).toEqual([
61+
{
62+
path: '',
63+
redirectTo: 'home',
64+
hasChildren: false,
65+
file: 'src/app.routes.ts',
66+
},
67+
{
68+
path: 'home',
69+
component: 'HomeComponent',
70+
title: 'Home',
71+
hasChildren: false,
72+
file: 'src/app.routes.ts',
73+
},
74+
{
75+
path: 'dashboard',
76+
component: 'DashboardComponent',
77+
title: 'Dashboard',
78+
hasChildren: true,
79+
file: 'src/app.routes.ts',
80+
},
81+
{
82+
path: 'stats',
83+
component: 'StatsComponent',
84+
hasChildren: false,
85+
file: 'src/app.routes.ts',
86+
},
87+
]);
88+
});
89+
90+
it('marks function or expression title / redirectTo as (dynamic)', async () => {
91+
const routes = await routesFor(`[
92+
{ path: 'custom-title', component: HomeComponent, title: customTitleResolver },
93+
{ path: 'custom-redirect', redirectTo: () => '/fallback' },
94+
{ path: 'template-title', component: HomeComponent, title: \`Home \${id}\` },
95+
]`);
96+
expect(routes.map((r) => [r.path, r.title, r.redirectTo])).toEqual([
97+
['custom-title', '(dynamic)', undefined],
98+
['custom-redirect', undefined, '(dynamic)'],
99+
['template-title', '(dynamic)', undefined],
100+
]);
101+
});
102+
103+
it('handles string literal escapes, unescapes, and concatenated non-literals', async () => {
104+
const routes = await routesFor(`[
105+
{ path: 'about', title: 'It\\'s "special"', redirectTo: "my/\\"path\\"" },
106+
{ path: '\\u0068ome', title: 'Line1\\nLine2', redirectTo: \`Price \\\${amount}\` },
107+
{ path: 'a' + 'b', component: Nope },
108+
]`);
109+
expect(routes).toEqual([
110+
{
111+
path: 'about',
112+
title: 'It\'s "special"',
113+
redirectTo: 'my/"path"',
114+
hasChildren: false,
115+
file: 'src/app.routes.ts',
116+
},
117+
{
118+
path: 'home',
119+
title: 'Line1\nLine2',
120+
redirectTo: 'Price ${amount}',
121+
hasChildren: false,
122+
file: 'src/app.routes.ts',
123+
},
124+
]);
125+
});
126+
127+
it('reads redirectTo and title on nested child routes and wildcard routes', async () => {
128+
const routes = await routesFor(`[
129+
{
130+
path: 'admin',
131+
title: 'Admin Panel',
132+
children: [
133+
{ path: '', redirectTo: 'overview', pathMatch: 'full' },
134+
{ path: 'overview', component: AdminOverview, title: 'Overview' },
135+
],
136+
},
137+
{ path: '**', redirectTo: '' },
138+
]`);
139+
expect(routes).toEqual([
140+
{
141+
path: 'admin',
142+
title: 'Admin Panel',
143+
hasChildren: true,
144+
file: 'src/app.routes.ts',
145+
},
146+
{
147+
path: '',
148+
redirectTo: 'overview',
149+
hasChildren: false,
150+
file: 'src/app.routes.ts',
151+
},
152+
{
153+
path: 'overview',
154+
component: 'AdminOverview',
155+
title: 'Overview',
156+
hasChildren: false,
157+
file: 'src/app.routes.ts',
158+
},
159+
{
160+
path: '**',
161+
redirectTo: '',
162+
hasChildren: false,
163+
file: 'src/app.routes.ts',
164+
},
165+
]);
166+
});
167+
23168
it('reads the export name of a lazy component', async () => {
24169
const routes = await routesFor(`[
25170
{ path: '', loadComponent: () => import('./home').then(m => m.Home) },

‎packages/ng-devtools/src/rpc/get-routes.ts‎

Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ import {
1515
const RouteSchema = v.object({
1616
path: v.string(),
1717
component: v.optional(v.string()),
18+
redirectTo: v.optional(v.string()),
19+
title: v.optional(v.string()),
1820
hasChildren: v.boolean(),
1921
file: v.string(),
2022
});
2123

24+
type ExtractedRoute = v.InferOutput<typeof RouteSchema>;
25+
2226
export const getRoutes = defineRpcFunction({
2327
name: 'get-routes',
2428
type: 'query',
@@ -35,17 +39,13 @@ export const getRoutes = defineRpcFunction({
3539
}),
3640
});
3741

38-
function extractRoutes(cwd: string) {
39-
const routes: { path: string; component?: string; hasChildren: boolean; file: string }[] = [];
42+
function extractRoutes(cwd: string): ExtractedRoute[] {
43+
const routes: ExtractedRoute[] = [];
4044
for (const root of sourceRoots(cwd)) findRouteFiles(root, cwd, routes);
4145
return routes;
4246
}
4347

44-
function findRouteFiles(
45-
dir: string,
46-
cwd: string,
47-
routes: { path: string; component?: string; hasChildren: boolean; file: string }[],
48-
) {
48+
function findRouteFiles(dir: string, cwd: string, routes: ExtractedRoute[]) {
4949
let entries: string[];
5050
try {
5151
entries = readdirSync(dir);
@@ -71,15 +71,17 @@ function findRouteFiles(
7171

7272
try {
7373
const content = readFileSync(full, 'utf-8');
74-
const relPath = relative(cwd, full);
74+
const relPath = relative(cwd, full).replaceAll('\\', '/');
7575

7676
for (const body of objectLiterals(stripComments(content))) {
7777
const props = topLevelProps(body);
78-
const path = props.get('path')?.match(/^['"`]([^'"`]*)['"`]$/)?.[1];
78+
const path = stringLiteral(props.get('path'));
7979
if (path === undefined) continue;
8080
routes.push({
8181
path,
8282
component: routeComponent(props),
83+
redirectTo: routeRedirectTo(props),
84+
title: routeTitle(props),
8385
// `loadChildren` has children too, it just loads them lazily.
8486
hasChildren: props.has('children') || props.has('loadChildren'),
8587
file: relPath,
@@ -91,6 +93,68 @@ function findRouteFiles(
9193
}
9294
}
9395

96+
function decodeEscapes(s: string): string {
97+
return s.replace(
98+
/\\(?:(u[0-9a-fA-F]{4}|x[0-9a-fA-F]{2})|([nrtbfv0\\])|(\r\n|[\r\n\u2028\u2029])|(.))/g,
99+
(
100+
_: string,
101+
hex: string | undefined,
102+
char: string | undefined,
103+
lineCont: string | undefined,
104+
anyChar: string | undefined,
105+
) => {
106+
if (hex) return String.fromCharCode(parseInt(hex.slice(1), 16));
107+
if (char) {
108+
switch (char) {
109+
case 'n':
110+
return '\n';
111+
case 'r':
112+
return '\r';
113+
case 't':
114+
return '\t';
115+
case 'b':
116+
return '\b';
117+
case 'f':
118+
return '\f';
119+
case 'v':
120+
return '\v';
121+
case '0':
122+
return '\0';
123+
case '\\':
124+
return '\\';
125+
default:
126+
return char;
127+
}
128+
}
129+
if (lineCont) return '';
130+
return anyChar ?? '';
131+
},
132+
);
133+
}
134+
135+
function stringLiteral(v?: string): string | undefined {
136+
if (!v) return undefined;
137+
const quote = v[0];
138+
if (quote !== "'" && quote !== '"' && quote !== '`') return undefined;
139+
if (skipString(v, 0) !== v.length - 1) return undefined;
140+
if (quote === '`' && /(^|[^\\])(?:\\\\)*\$\{/.test(v)) return undefined;
141+
return decodeEscapes(v.slice(1, -1));
142+
}
143+
144+
function routeRedirectTo(props: Map<string, string>): string | undefined {
145+
const val = props.get('redirectTo');
146+
if (val === undefined) return undefined;
147+
const literal = stringLiteral(val);
148+
return literal !== undefined ? literal : '(dynamic)';
149+
}
150+
151+
function routeTitle(props: Map<string, string>): string | undefined {
152+
const val = props.get('title');
153+
if (val === undefined) return undefined;
154+
const literal = stringLiteral(val);
155+
return literal !== undefined ? literal : '(dynamic)';
156+
}
157+
94158
function routeComponent(props: Map<string, string>): string | undefined {
95159
const eager = props.get('component')?.match(/^(\w+)/)?.[1];
96160
if (eager) return eager;

‎packages/ng-devtools/src/types.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export interface ComponentNode {
1010
export interface RouteInfo {
1111
path: string;
1212
component?: string;
13+
redirectTo?: string;
14+
title?: string;
1315
hasChildren: boolean;
1416
guards?: string[];
1517
}

0 commit comments

Comments
 (0)