Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
fix(extract): form Route nodes from NestJS @Controller/@Get decorators (#1428)
Martin Vogel88d4f29d18d6
diff --git a/internal/cbm/extract_defs.c b/internal/cbm/extract_defs.cindex 1e63d26fc..34a574759 100644--- a/internal/cbm/extract_defs.c+++ b/internal/cbm/extract_defs.c@@ -1650,6 +1650,141 @@ static bool try_route_from_decorator_call(CBMArena *a, TSNode dchild, const charreturn true;}+/* NestJS (and routing-controllers) method decorators: the verb is the bare,+ * capitalized decorator name — @Get, @Post, ... @All. Returns NULL otherwise. */+static const char *nest_decorator_method(const char *name) {+ static const struct {+ const char *decorator;+ const char *method;+ } verbs[] = {{"Get", "GET"}, {"Post", "POST"}, {"Put", "PUT"}, {"Delete", "DELETE"},+ {"Patch", "PATCH"}, {"Head", "HEAD"}, {"Options", "OPTIONS"}, {"All", "ANY"}};+ for (size_t i = 0; name && i < sizeof(verbs) / sizeof(verbs[0]); i++) {+ if (strcmp(name, verbs[i].decorator) == 0) {+ return verbs[i].method;+ }+ }+ return NULL;+}++/* Unquote a TS `string` literal node and root it at "/": Nest writes route+ * segments without a leading slash (@Get(':id'), @Controller('users')). */+static const char *nest_path_from_string(CBMArena *a, TSNode node, const char *source) {+ if (ts_node_is_null(node) || strcmp(ts_node_type(node), "string") != 0) {+ return NULL;+ }+ char *text = cbm_node_text(a, node, source);+ size_t len = text ? strlen(text) : 0;+ if (len < PAIR_CHARS) {+ return NULL;+ }+ const char *inner = cbm_arena_strndup(a, text + SKIP_CHAR, len - PAIR_CHARS);+ if (!inner) {+ return NULL;+ }+ return inner[0] == '/' ? inner : cbm_arena_sprintf(a, "/%s", inner);+}++/* The route path a Nest decorator's argument list carries. Accepted shapes:+ * () -> "/"+ * ('users') / (['a', 'b']) -> the (first) string literal+ * ({ path: 'users', ... }) -> the `path` property (Nest's options form)+ * Any other first argument (a constant, a template) has no statically known+ * path: NULL, so no Route is invented for it. */+static const char *nest_path_from_args(CBMArena *a, TSNode args, const char *source) {+ if (ts_node_is_null(args) || ts_node_named_child_count(args) == 0) {+ return "/";+ }+ TSNode arg = ts_node_named_child(args, 0);+ const char *kind = ts_node_type(arg);+ if (strcmp(kind, "array") == 0 && ts_node_named_child_count(arg) > 0) {+ return nest_path_from_string(a, ts_node_named_child(arg, 0), source);+ }+ if (strcmp(kind, "object") != 0) {+ return nest_path_from_string(a, arg, source);+ }+ uint32_t nc = ts_node_named_child_count(arg);+ for (uint32_t i = 0; i < nc; i++) {+ TSNode pair = ts_node_named_child(arg, i);+ if (strcmp(ts_node_type(pair), "pair") != 0) {+ continue;+ }+ TSNode key = ts_node_child_by_field_name(pair, TS_FIELD("key"));+ char *key_text = ts_node_is_null(key) ? NULL : cbm_node_text(a, key, source);+ if (key_text && strcmp(key_text, "path") == 0) {+ return nest_path_from_string(a, ts_node_child_by_field_name(pair, TS_FIELD("value")),+ source);+ }+ }+ return "/"; /* options object without `path` (e.g. only `host`) */+}++/* A TS decorator's call_expression as (bare name, argument list). Returns the+ * name, or NULL when the callee is not a plain identifier. */+static const char *ts_decorator_call_name(CBMArena *a, TSNode call, const char *source,+ TSNode *out_args) {+ if (ts_node_is_null(call) || strcmp(ts_node_type(call), "call_expression") != 0) {+ return NULL;+ }+ TSNode fn = ts_node_child_by_field_name(call, TS_FIELD("function"));+ if (ts_node_is_null(fn) || strcmp(ts_node_type(fn), "identifier") != 0) {+ return NULL;+ }+ *out_args = ts_node_child_by_field_name(call, TS_FIELD("arguments"));+ return cbm_node_text(a, fn, source);+}++/* NestJS method route: @Get(':id') on a TS class method. The class-level+ * @Controller prefix is composed by the caller (nest_class_route_prefix). */+static bool try_route_from_ts_decorator_call(CBMArena *a, TSNode dchild, const char *source,+ const char **out_path, const char **out_method) {+ TSNode args = {0};+ const char *method = nest_decorator_method(ts_decorator_call_name(a, dchild, source, &args));+ if (!method) {+ return false;+ }+ const char *path = nest_path_from_args(a, args, source);+ if (!path) {+ return false;+ }+ *out_path = path;+ *out_method = method;+ return true;+}++/* The @Controller('users') prefix of a NestJS controller, from `decorator`+ * nodes that are children of the class node (`@X class C`) or its preceding+ * siblings (`@X export class C`: the decorator belongs to export_statement).+ * NULL when the class is not a controller. */+static const char *nest_prefix_from_decorator(CBMArena *a, TSNode dec, const char *source) {+ if (strcmp(ts_node_type(dec), "decorator") != 0) {+ return NULL;+ }+ TSNode args = {0};+ const char *name = ts_decorator_call_name(a, ts_node_named_child(dec, 0), source, &args);+ if (!name || (strcmp(name, "Controller") != 0 && strcmp(name, "JsonController") != 0)) {+ return NULL;+ }+ return nest_path_from_args(a, args, source);+}++static const char *nest_class_route_prefix(CBMArena *a, TSNode class_node, const char *source) {+ uint32_t cc = ts_node_named_child_count(class_node);+ for (uint32_t i = 0; i < cc; i++) {+ const char *p = nest_prefix_from_decorator(a, ts_node_named_child(class_node, i), source);+ if (p) {+ return p;+ }+ }+ for (TSNode prev = ts_node_prev_named_sibling(class_node); !ts_node_is_null(prev);+ prev = ts_node_prev_named_sibling(prev)) {+ const char *p = nest_prefix_from_decorator(a, prev, source);+ if (p) {+ return p;+ }+ }+ return NULL;+}+/* Resolve an annotation's name node across grammars. Java exposes a `name`* field; tree-sitter-kotlin does not — its annotation name lives in a nested* type_identifier:@@ -1822,6 +1957,9 @@ static void extract_route_from_decorators(CBMArena *a, TSNode func_node, const cuint32_t dc = ts_node_named_child_count(prev);for (uint32_t di = 0; di < dc; di++) {TSNode dchild = ts_node_named_child(prev, di);+ if (try_route_from_ts_decorator_call(a, dchild, source, out_path, out_method)) {+ return;+ }if (strcmp(ts_node_type(dchild), "call") != 0) {continue;}@@ -5180,6 +5318,12 @@ static void push_method_def(CBMExtractCtx *ctx, TSNode child, TSNode class_node,const char *prefix = spring_class_route_prefix(a, class_node, ctx->source, spec);def.route_path = join_route_paths(a, prefix, def.route_path);}+ if (def.route_path && (ctx->language == CBM_LANG_TYPESCRIPT || ctx->language == CBM_LANG_TSX)) {+ /* NestJS: a verb decorator only routes inside a @Controller class. */+ const char *prefix = nest_class_route_prefix(a, class_node, ctx->source);+ def.route_path = prefix ? join_route_paths(a, prefix, def.route_path) : NULL;+ def.route_method = prefix ? def.route_method : NULL;+ }def.docstring = extract_docstring(a, child, ctx->source, ctx->language);if (spec->branching_node_types && spec->branching_node_types[0]) {diff --git a/tests/test_extraction.c b/tests/test_extraction.cindex 82744f592..5502d8b73 100644--- a/tests/test_extraction.c+++ b/tests/test_extraction.c@@ -4631,6 +4631,57 @@ TEST(extract_java_spring_relative_string_not_route_path) {PASS();}+/* Issue #1428: NestJS routes are TypeScript decorators — the class-level+ * @Controller('users') prefix composes with the method-level @Get(':id')+ * verb + path. Nest paths carry no leading slash, and the decorator's call is+ * a `call_expression` (not Python's `call`), so no Route was ever formed. */+TEST(extract_ts_nestjs_controller_routes_issue1428) {+ CBMFileResult *r =+ extract("import { Controller, Get, Post, Patch, Delete, Param } from '@nestjs/common';\n"+ "@Controller({ path: 'users', version: '1' })\n"+ "export class UsersController {\n"+ " @Get() findAll() { return []; }\n"+ " @Get(':id') findOne(@Param('id') id: string) { return id; }\n"+ " @Post() create() { return 1; }\n"+ " @Patch(':id') update(@Param('id') id: string) { return id; }\n"+ " @Delete('/:id') remove(@Param('id') id: string) { return id; }\n"+ " helper() { return 0; }\n"+ "}\n"+ "@Controller('health')\n"+ "export class HealthController {\n"+ " @Get() check() { return { status: 'ok' }; }\n"+ "}\n"+ "@Controller()\n"+ "export class RootController {\n"+ " @Get('ping') ping() { return 'pong'; }\n"+ "}\n",+ CBM_LANG_TYPESCRIPT, "t", "src/users/users.controller.ts");+ ASSERT_NOT_NULL(r);+ ASSERT_FALSE(r->has_error);+ static const struct {+ const char *name;+ const char *method;+ const char *path;+ } want[] = {+ {"findAll", "GET", "/users"}, {"findOne", "GET", "/users/:id"},+ {"create", "POST", "/users"}, {"update", "PATCH", "/users/:id"},+ {"remove", "DELETE", "/users/:id"}, {"check", "GET", "/health"},+ {"ping", "GET", "/ping"},+ };+ for (size_t i = 0; i < sizeof(want) / sizeof(want[0]); i++) {+ const CBMDefinition *d = find_def_by_name(r, want[i].name);+ ASSERT_NOT_NULL(d);+ ASSERT_NOT_NULL(d->route_path);+ ASSERT_STR_EQ(d->route_path, want[i].path);+ ASSERT_STR_EQ(d->route_method, want[i].method);+ }+ const CBMDefinition *helper = find_def_by_name(r, "helper");+ ASSERT_NOT_NULL(helper);+ ASSERT_NULL(helper->route_path);+ cbm_free_result(r);+ PASS();+}+/* Return the file's Module definition (extraction pushes it first), or NULL. */static const CBMDefinition *find_module_def(CBMFileResult *r) {for (int i = 0; i < r->defs.count; i++) {@@ -9043,6 +9094,7 @@ SUITE(extraction) {RUN_TEST(extract_java_jaxrs_relative_path_templates);RUN_TEST(extract_java_jaxrs_empty_path_means_class_path);RUN_TEST(extract_java_spring_relative_string_not_route_path);+ RUN_TEST(extract_ts_nestjs_controller_routes_issue1428);RUN_TEST(extract_blazor_page_directive_routes_component);RUN_TEST(extract_blazor_component_without_page_has_no_route);RUN_TEST(extract_razor_page_directive_routes_cshtml_view);