Skip to content

Commit 952b573

Browse files
committed
feat: discover route registrations
1 parent b9e89ff commit 952b573

1 file changed

Lines changed: 83 additions & 0 deletions

File tree

src/routes.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { childForField, parseCpp, walkNamed } from './cpp-ast.js';
2+
import { readTextFile } from './fs.js';
3+
import { normalizeNewlines } from './text.js';
4+
import { type CppHttpMethod, type RouteRegistration, HttpMethodMap } from './types.js';
5+
6+
export async function discoverRoutes(routeFilePath: string): Promise<readonly RouteRegistration[]> {
7+
const sourceText = normalizeNewlines(await readTextFile(routeFilePath));
8+
const rootNode = parseCpp(sourceText);
9+
const routes: RouteRegistration[] = [];
10+
11+
walkNamed(rootNode, (node) => {
12+
if (node.type !== 'call_expression') {
13+
return;
14+
}
15+
16+
const functionNode = childForField(node, 'function');
17+
const functionParts = functionNode === undefined ? undefined : parseFieldExpression(functionNode);
18+
if (functionParts === undefined) {
19+
return;
20+
}
21+
22+
if (functionParts.operator !== '->' || functionParts.baseName !== 'server' || !isHttpMethodToken(functionParts.fieldName)) {
23+
return;
24+
}
25+
26+
const argumentListNode = node.namedChildren.find((child) => child.type === 'argument_list');
27+
const pathNode = argumentListNode?.namedChildren[0];
28+
const handlerNode = argumentListNode?.namedChildren[1];
29+
const pathTemplate = pathNode === undefined ? undefined : extractStringLiteral(pathNode.text);
30+
const handler = handlerNode?.type === 'identifier' ? handlerNode.text : undefined;
31+
if (pathTemplate === undefined || handler === undefined) {
32+
return;
33+
}
34+
35+
routes.push({
36+
method: HttpMethodMap[functionParts.fieldName],
37+
pathTemplate,
38+
handler,
39+
source: {
40+
filePath: routeFilePath,
41+
line: node.startPosition.row + 1,
42+
column: node.startPosition.column + 1,
43+
},
44+
});
45+
});
46+
47+
return routes;
48+
}
49+
50+
function parseFieldExpression(node: { readonly type: string; readonly text: string; childForFieldName(fieldName: string): unknown }): {
51+
readonly baseName: string;
52+
readonly fieldName: string;
53+
readonly operator: string;
54+
} | undefined {
55+
if (node.type !== 'field_expression') {
56+
return undefined;
57+
}
58+
59+
const argumentNode = childForField(node as never, 'argument');
60+
const fieldNode = childForField(node as never, 'field');
61+
const operatorNode = childForField(node as never, 'operator');
62+
if (argumentNode === undefined || fieldNode === undefined || operatorNode === undefined) {
63+
return undefined;
64+
}
65+
66+
if (argumentNode.type !== 'identifier') {
67+
return undefined;
68+
}
69+
70+
return {
71+
baseName: argumentNode.text,
72+
fieldName: fieldNode.text,
73+
operator: operatorNode.text,
74+
};
75+
}
76+
77+
function isHttpMethodToken(token: string): token is CppHttpMethod {
78+
return token in HttpMethodMap;
79+
}
80+
81+
function extractStringLiteral(value: string): string | undefined {
82+
return value.startsWith('"') && value.endsWith('"') ? value.slice(1, value.length - 1) : undefined;
83+
}

0 commit comments

Comments
 (0)