Skip to content

Commit 3960516

Browse files
committed
fix(routes): address review feedback on string parsing, dynamic markers, track key, and test cases
1 parent 62bf2d5 commit 3960516

7 files changed

Lines changed: 93 additions & 39 deletions

File tree

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,12 @@ interface RouteInfo {
4040
</tr>
4141
</thead>
4242
<tbody>
43-
@for (route of filtered(); track route.path + route.file) {
43+
@for (route of filtered(); track $index) {
4444
<tr>
4545
<td class="path">/{{ route.path }}</td>
4646
<td>
4747
@if (route.redirectTo !== undefined) {
48-
<span class="redirect"
49-
>➜
50-
{{
51-
route.redirectTo
52-
? route.redirectTo.startsWith('/')
53-
? route.redirectTo
54-
: '/' + route.redirectTo
55-
: '/'
56-
}}</span
57-
>
48+
<span class="redirect">➜ {{ route.redirectTo }}</span>
5849
} @else {
5950
{{ route.component ?? '—' }}
6051
}

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

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,7 @@ describe('get-routes', () => {
2020
it('reads an eager component', async () => {
2121
const routes = await routesFor(`[{ path: 'about', component: AboutComponent }]`);
2222
expect(routes).toEqual([
23-
{
24-
path: 'about',
25-
component: 'AboutComponent',
26-
redirectTo: undefined,
27-
title: undefined,
28-
hasChildren: false,
29-
file: 'src/app.routes.ts',
30-
},
23+
{ path: 'about', component: 'AboutComponent', hasChildren: false, file: 'src/app.routes.ts' },
3124
]);
3225
});
3326

@@ -71,47 +64,100 @@ describe('get-routes', () => {
7164
expect(routes).toEqual([
7265
{
7366
path: '',
74-
component: undefined,
7567
redirectTo: 'home',
76-
title: undefined,
7768
hasChildren: false,
7869
file: 'src/app.routes.ts',
7970
},
8071
{
8172
path: 'home',
8273
component: 'HomeComponent',
83-
redirectTo: undefined,
8474
title: 'Home',
8575
hasChildren: false,
8676
file: 'src/app.routes.ts',
8777
},
8878
{
8979
path: 'dashboard',
9080
component: 'DashboardComponent',
91-
redirectTo: undefined,
9281
title: 'Dashboard',
9382
hasChildren: true,
9483
file: 'src/app.routes.ts',
9584
},
9685
{
9786
path: 'stats',
9887
component: 'StatsComponent',
99-
redirectTo: undefined,
100-
title: undefined,
10188
hasChildren: false,
10289
file: 'src/app.routes.ts',
10390
},
10491
]);
10592
});
10693

107-
it('handles function or expression title / redirectTo gracefully', async () => {
94+
it('marks function or expression title / redirectTo as (dynamic)', async () => {
10895
const routes = await routesFor(`[
10996
{ path: 'custom-title', component: HomeComponent, title: customTitleResolver },
11097
{ path: 'custom-redirect', redirectTo: () => '/fallback' },
98+
{ path: 'template-title', component: HomeComponent, title: \`Home \${id}\` },
11199
]`);
112100
expect(routes.map((r) => [r.path, r.title, r.redirectTo])).toEqual([
113-
['custom-title', undefined, undefined],
114-
['custom-redirect', undefined, undefined],
101+
['custom-title', '(dynamic)', undefined],
102+
['custom-redirect', undefined, '(dynamic)'],
103+
['template-title', '(dynamic)', undefined],
104+
]);
105+
});
106+
107+
it('handles string literal escapes, unescapes, and concatenated non-literals', async () => {
108+
const routes = await routesFor(`[
109+
{ path: 'about', title: 'It\\'s "special"', redirectTo: "my/\\"path\\"" },
110+
{ path: 'a' + 'b', component: Nope },
111+
]`);
112+
expect(routes).toEqual([
113+
{
114+
path: 'about',
115+
title: 'It\'s "special"',
116+
redirectTo: 'my/"path"',
117+
hasChildren: false,
118+
file: 'src/app.routes.ts',
119+
},
120+
]);
121+
});
122+
123+
it('reads redirectTo and title on nested child routes and wildcard routes', async () => {
124+
const routes = await routesFor(`[
125+
{
126+
path: 'admin',
127+
title: 'Admin Panel',
128+
children: [
129+
{ path: '', redirectTo: 'overview', pathMatch: 'full' },
130+
{ path: 'overview', component: AdminOverview, title: 'Overview' },
131+
],
132+
},
133+
{ path: '**', redirectTo: '' },
134+
]`);
135+
expect(routes).toEqual([
136+
{
137+
path: 'admin',
138+
title: 'Admin Panel',
139+
hasChildren: true,
140+
file: 'src/app.routes.ts',
141+
},
142+
{
143+
path: '',
144+
redirectTo: 'overview',
145+
hasChildren: false,
146+
file: 'src/app.routes.ts',
147+
},
148+
{
149+
path: 'overview',
150+
component: 'AdminOverview',
151+
title: 'Overview',
152+
hasChildren: false,
153+
file: 'src/app.routes.ts',
154+
},
155+
{
156+
path: '**',
157+
redirectTo: '',
158+
hasChildren: false,
159+
file: 'src/app.routes.ts',
160+
},
115161
]);
116162
});
117163

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ function walk(dir: string, cwd: string, out: ComponentInfo[]) {
8989

9090
out.push({
9191
selector: selectorMatch[1],
92-
file: relative(cwd, full).replaceAll('\\', '/'),
92+
file: relative(cwd, full),
9393
inputs,
9494
outputs,
9595
isStandalone,

‎packages/ng-devtools/src/rpc/get-ngrx-store.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ function walk(dir: string, cwd: string, out: NgrxStoreEntry[]) {
140140
continue;
141141
}
142142

143-
const relPath = relative(cwd, full).replaceAll('\\', '/');
143+
const relPath = relative(cwd, full);
144144

145145
for (const { pattern, kind } of NGRX_PATTERNS) {
146146
pattern.lastIndex = 0;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ function walk(dir: string, cwd: string, out: ProviderEntry[]) {
9999

100100
try {
101101
const content = readFileSync(full, 'utf-8');
102-
const relPath = relative(cwd, full).replaceAll('\\', '/');
102+
const relPath = relative(cwd, full);
103103

104104
// @Injectable({ providedIn: 'root' }) or @Service (with or without parens)
105105
for (const match of content.matchAll(

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ function findRouteFiles(dir: string, cwd: string, routes: ExtractedRoute[]) {
6969
routes.push({
7070
path,
7171
component: routeComponent(props),
72-
redirectTo: stringLiteral(props.get('redirectTo')),
73-
title: stringLiteral(props.get('title')),
72+
redirectTo: routeRedirectTo(props),
73+
title: routeTitle(props),
7474
hasChildren: props.has('children'),
7575
file: relPath,
7676
});
@@ -81,9 +81,28 @@ function findRouteFiles(dir: string, cwd: string, routes: ExtractedRoute[]) {
8181
}
8282
}
8383

84-
function stringLiteral(value?: string): string | undefined {
85-
const match = value?.match(/^(['"`])([\s\S]*)\1$/);
86-
return match?.[2];
84+
function stringLiteral(v?: string): string | undefined {
85+
if (!v) return undefined;
86+
const quote = v[0];
87+
if (quote !== "'" && quote !== '"' && quote !== '`') return undefined;
88+
if (skipString(v, 0) !== v.length - 1) return undefined;
89+
if (quote === '`' && v.includes('${')) return undefined;
90+
const raw = v.slice(1, -1);
91+
return raw.replace(/\\(.)/g, '$1');
92+
}
93+
94+
function routeRedirectTo(props: Map<string, string>): string | undefined {
95+
const val = props.get('redirectTo');
96+
if (val === undefined) return undefined;
97+
const literal = stringLiteral(val);
98+
return literal !== undefined ? literal : '(dynamic)';
99+
}
100+
101+
function routeTitle(props: Map<string, string>): string | undefined {
102+
const val = props.get('title');
103+
if (val === undefined) return undefined;
104+
const literal = stringLiteral(val);
105+
return literal !== undefined ? literal : '(dynamic)';
87106
}
88107

89108
function routeComponent(props: Map<string, string>): string | undefined {

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,7 @@ function walk(dir: string, cwd: string, out: SignalEntry[]) {
9999
if (!item.endsWith('.ts') || item.endsWith('.spec.ts') || item.endsWith('.d.ts')) continue;
100100

101101
try {
102-
out.push(
103-
...signalsIn(readFileSync(full, 'utf-8'), relative(cwd, full).replaceAll('\\', '/')),
104-
);
102+
out.push(...signalsIn(readFileSync(full, 'utf-8'), relative(cwd, full)));
105103
} catch {
106104
// skip
107105
}

0 commit comments

Comments
 (0)