Skip to content

Commit f4a1332

Browse files
committed
added first attempt to extract routes from ast
1 parent b1f60b5 commit f4a1332

File tree

2 files changed

+62
-8
lines changed

2 files changed

+62
-8
lines changed

scripts/route.helper.js

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,16 @@ export const extractRoutesFromTS = (routesString, rootName = ROOT_COMPONENT) =>
109109

110110
// Extract and transform the routes
111111
const routes = routesArrayNode.expression.elements.map(e => {
112+
let routesStringRange;
112113
try {
113-
const x = routesString.substring(...e.range);
114-
/* ToDo: build the expressions from ast
115-
if (x.includes('layout/layout.module')) {
116-
console.log(x, e.properties.find(n => n.key?.name === 'children').value.elements[0].properties)
117-
}*/
118-
return JSON.parse(cleanUpRouteDeclarations(x, rootName));
114+
// ToDo: build the expressions from ast
115+
// console.log(handleChildren([e]))
116+
117+
routesStringRange = routesString.substring(...e.range);
118+
routesStringRange = cleanUpRouteDeclarations(routesStringRange, rootName);
119+
return JSON.parse(routesStringRange);
119120
} catch (error) {
120-
console.error('Error parsing route configuration:', cleanUpRouteDeclarations(routesString.substring(...e.range)), e, error);
121+
console.error('Error parsing route configuration:', cleanUpRouteDeclarations(routesStringRange), e, error);
121122
}
122123
}).filter(Boolean);
123124

@@ -128,6 +129,59 @@ export const extractRoutesFromTS = (routesString, rootName = ROOT_COMPONENT) =>
128129
}
129130
};
130131

132+
const handleChildren = (elements, rootName) => {
133+
const tempRoutes = [];
134+
elements.forEach(e => {
135+
// e.type should always be 'ObjectExpression'
136+
if (e.properties.some(n => n.key?.name === 'component')) {
137+
tempRoutes.push(extractComponents(e.properties, rootName));
138+
}
139+
140+
if (e.properties.some(n => n.key?.name === 'loadComponent')) {
141+
tempRoutes.push(extractLoadComponents(e.properties, rootName));
142+
}
143+
144+
if (e.properties.some(n => n.key?.name === 'loadChildren')) {
145+
tempRoutes.push(extractLoadChildren(e.properties, rootName));
146+
}
147+
148+
const children = e.properties.find(n => n.key?.name === 'children')?.value?.elements;
149+
if (children?.length > 0) {
150+
handleChildren(children, rootName).forEach(element => {
151+
tempRoutes.push(element)
152+
});
153+
}
154+
})
155+
156+
return tempRoutes;
157+
};
158+
159+
const extractComponents = (properties, parent) => {
160+
return {
161+
path: properties.find(n => n.key?.name === 'path')?.value?.value,
162+
component: properties.find(n => n.key?.name === 'component')?.value?.name,
163+
parent
164+
}
165+
};
166+
167+
const extractLoadComponents = (properties, parent) => {
168+
return {
169+
path: properties.find(n => n.key?.name === 'path')?.value?.value,
170+
loadComponent: properties.find(n => n.key?.name === 'loadComponent')?.value?.body?.source?.value,
171+
parent
172+
}
173+
};
174+
175+
const extractLoadChildren = (properties, parent) => {
176+
const loadComponent = properties.find(n => n.key?.name === 'loadChildren')?.value?.body?.source?.value
177+
return {
178+
path: properties.find(n => n.key?.name === 'path')?.value?.value,
179+
loadComponent,
180+
componentName: loadComponent,
181+
parent
182+
}
183+
}
184+
131185
const cleanUpRouteDeclarations = (route, rootName) => {
132186
return route
133187
// 1.1 Remove Guards:

test-data/route-definitions/real-world/app.routes.satisfies.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const routes = [
2525
},
2626
{
2727
path: "settings",
28-
loadComponent: () => import("./features/settings/settings.component"),
28+
loadComponent: () => import("./features/settings/settings.component").then(c => c.SettingsComponent),
2929
canActivate: [() => inject(UserService).isAuthenticated],
3030
},
3131
{

0 commit comments

Comments
 (0)