Skip to content

Commit 88d4f29

Browse files
committed
fix(extract): form Route nodes from NestJS @Controller/@get decorators (DeusData#1428)
NestJS controllers produced zero Route nodes. Two gaps in the decorator route walk (extract_route_from_decorators): - It only inspected decorator children of kind `call` (Python). A TypeScript decorator's call is a `call_expression`, so @get(':id') was never examined. - The path helpers only accept literals that start with "/". Nest writes segments without one (@controller('users'), @get(':id')), and the class-level prefix was composed only for Java/Kotlin (Spring/JAX-RS). TS/TSX class methods now read the Nest verb decorators (@get, @post, @put, @delete, @patch, @Head, @options, @ALL -> ANY) and compose them with the class's @controller / @jsoncontroller prefix. The prefix can be a string, an array (first entry), or the options object's `path`. Paths are rooted at "/". A verb decorator outside a controller class forms no route. A non-literal path argument (a constant) also forms none, so no route is invented. Route and HANDLES are reused as they are: no new label. Not covered: the app-level setGlobalPrefix() and enableVersioning() in main.ts are cross-file and are not applied. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 80eb92a commit 88d4f29

2 files changed

Lines changed: 196 additions & 0 deletions

File tree

‎internal/cbm/extract_defs.c‎

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,6 +1650,141 @@ static bool try_route_from_decorator_call(CBMArena *a, TSNode dchild, const char
16501650
return true;
16511651
}
16521652

1653+
/* NestJS (and routing-controllers) method decorators: the verb is the bare,
1654+
* capitalized decorator name — @Get, @Post, ... @All. Returns NULL otherwise. */
1655+
static const char *nest_decorator_method(const char *name) {
1656+
static const struct {
1657+
const char *decorator;
1658+
const char *method;
1659+
} verbs[] = {{"Get", "GET"}, {"Post", "POST"}, {"Put", "PUT"}, {"Delete", "DELETE"},
1660+
{"Patch", "PATCH"}, {"Head", "HEAD"}, {"Options", "OPTIONS"}, {"All", "ANY"}};
1661+
for (size_t i = 0; name && i < sizeof(verbs) / sizeof(verbs[0]); i++) {
1662+
if (strcmp(name, verbs[i].decorator) == 0) {
1663+
return verbs[i].method;
1664+
}
1665+
}
1666+
return NULL;
1667+
}
1668+
1669+
/* Unquote a TS `string` literal node and root it at "/": Nest writes route
1670+
* segments without a leading slash (@Get(':id'), @Controller('users')). */
1671+
static const char *nest_path_from_string(CBMArena *a, TSNode node, const char *source) {
1672+
if (ts_node_is_null(node) || strcmp(ts_node_type(node), "string") != 0) {
1673+
return NULL;
1674+
}
1675+
char *text = cbm_node_text(a, node, source);
1676+
size_t len = text ? strlen(text) : 0;
1677+
if (len < PAIR_CHARS) {
1678+
return NULL;
1679+
}
1680+
const char *inner = cbm_arena_strndup(a, text + SKIP_CHAR, len - PAIR_CHARS);
1681+
if (!inner) {
1682+
return NULL;
1683+
}
1684+
return inner[0] == '/' ? inner : cbm_arena_sprintf(a, "/%s", inner);
1685+
}
1686+
1687+
/* The route path a Nest decorator's argument list carries. Accepted shapes:
1688+
* () -> "/"
1689+
* ('users') / (['a', 'b']) -> the (first) string literal
1690+
* ({ path: 'users', ... }) -> the `path` property (Nest's options form)
1691+
* Any other first argument (a constant, a template) has no statically known
1692+
* path: NULL, so no Route is invented for it. */
1693+
static const char *nest_path_from_args(CBMArena *a, TSNode args, const char *source) {
1694+
if (ts_node_is_null(args) || ts_node_named_child_count(args) == 0) {
1695+
return "/";
1696+
}
1697+
TSNode arg = ts_node_named_child(args, 0);
1698+
const char *kind = ts_node_type(arg);
1699+
if (strcmp(kind, "array") == 0 && ts_node_named_child_count(arg) > 0) {
1700+
return nest_path_from_string(a, ts_node_named_child(arg, 0), source);
1701+
}
1702+
if (strcmp(kind, "object") != 0) {
1703+
return nest_path_from_string(a, arg, source);
1704+
}
1705+
uint32_t nc = ts_node_named_child_count(arg);
1706+
for (uint32_t i = 0; i < nc; i++) {
1707+
TSNode pair = ts_node_named_child(arg, i);
1708+
if (strcmp(ts_node_type(pair), "pair") != 0) {
1709+
continue;
1710+
}
1711+
TSNode key = ts_node_child_by_field_name(pair, TS_FIELD("key"));
1712+
char *key_text = ts_node_is_null(key) ? NULL : cbm_node_text(a, key, source);
1713+
if (key_text && strcmp(key_text, "path") == 0) {
1714+
return nest_path_from_string(a, ts_node_child_by_field_name(pair, TS_FIELD("value")),
1715+
source);
1716+
}
1717+
}
1718+
return "/"; /* options object without `path` (e.g. only `host`) */
1719+
}
1720+
1721+
/* A TS decorator's call_expression as (bare name, argument list). Returns the
1722+
* name, or NULL when the callee is not a plain identifier. */
1723+
static const char *ts_decorator_call_name(CBMArena *a, TSNode call, const char *source,
1724+
TSNode *out_args) {
1725+
if (ts_node_is_null(call) || strcmp(ts_node_type(call), "call_expression") != 0) {
1726+
return NULL;
1727+
}
1728+
TSNode fn = ts_node_child_by_field_name(call, TS_FIELD("function"));
1729+
if (ts_node_is_null(fn) || strcmp(ts_node_type(fn), "identifier") != 0) {
1730+
return NULL;
1731+
}
1732+
*out_args = ts_node_child_by_field_name(call, TS_FIELD("arguments"));
1733+
return cbm_node_text(a, fn, source);
1734+
}
1735+
1736+
/* NestJS method route: @Get(':id') on a TS class method. The class-level
1737+
* @Controller prefix is composed by the caller (nest_class_route_prefix). */
1738+
static bool try_route_from_ts_decorator_call(CBMArena *a, TSNode dchild, const char *source,
1739+
const char **out_path, const char **out_method) {
1740+
TSNode args = {0};
1741+
const char *method = nest_decorator_method(ts_decorator_call_name(a, dchild, source, &args));
1742+
if (!method) {
1743+
return false;
1744+
}
1745+
const char *path = nest_path_from_args(a, args, source);
1746+
if (!path) {
1747+
return false;
1748+
}
1749+
*out_path = path;
1750+
*out_method = method;
1751+
return true;
1752+
}
1753+
1754+
/* The @Controller('users') prefix of a NestJS controller, from `decorator`
1755+
* nodes that are children of the class node (`@X class C`) or its preceding
1756+
* siblings (`@X export class C`: the decorator belongs to export_statement).
1757+
* NULL when the class is not a controller. */
1758+
static const char *nest_prefix_from_decorator(CBMArena *a, TSNode dec, const char *source) {
1759+
if (strcmp(ts_node_type(dec), "decorator") != 0) {
1760+
return NULL;
1761+
}
1762+
TSNode args = {0};
1763+
const char *name = ts_decorator_call_name(a, ts_node_named_child(dec, 0), source, &args);
1764+
if (!name || (strcmp(name, "Controller") != 0 && strcmp(name, "JsonController") != 0)) {
1765+
return NULL;
1766+
}
1767+
return nest_path_from_args(a, args, source);
1768+
}
1769+
1770+
static const char *nest_class_route_prefix(CBMArena *a, TSNode class_node, const char *source) {
1771+
uint32_t cc = ts_node_named_child_count(class_node);
1772+
for (uint32_t i = 0; i < cc; i++) {
1773+
const char *p = nest_prefix_from_decorator(a, ts_node_named_child(class_node, i), source);
1774+
if (p) {
1775+
return p;
1776+
}
1777+
}
1778+
for (TSNode prev = ts_node_prev_named_sibling(class_node); !ts_node_is_null(prev);
1779+
prev = ts_node_prev_named_sibling(prev)) {
1780+
const char *p = nest_prefix_from_decorator(a, prev, source);
1781+
if (p) {
1782+
return p;
1783+
}
1784+
}
1785+
return NULL;
1786+
}
1787+
16531788
/* Resolve an annotation's name node across grammars. Java exposes a `name`
16541789
* field; tree-sitter-kotlin does not — its annotation name lives in a nested
16551790
* type_identifier:
@@ -1822,6 +1957,9 @@ static void extract_route_from_decorators(CBMArena *a, TSNode func_node, const c
18221957
uint32_t dc = ts_node_named_child_count(prev);
18231958
for (uint32_t di = 0; di < dc; di++) {
18241959
TSNode dchild = ts_node_named_child(prev, di);
1960+
if (try_route_from_ts_decorator_call(a, dchild, source, out_path, out_method)) {
1961+
return;
1962+
}
18251963
if (strcmp(ts_node_type(dchild), "call") != 0) {
18261964
continue;
18271965
}
@@ -5180,6 +5318,12 @@ static void push_method_def(CBMExtractCtx *ctx, TSNode child, TSNode class_node,
51805318
const char *prefix = spring_class_route_prefix(a, class_node, ctx->source, spec);
51815319
def.route_path = join_route_paths(a, prefix, def.route_path);
51825320
}
5321+
if (def.route_path && (ctx->language == CBM_LANG_TYPESCRIPT || ctx->language == CBM_LANG_TSX)) {
5322+
/* NestJS: a verb decorator only routes inside a @Controller class. */
5323+
const char *prefix = nest_class_route_prefix(a, class_node, ctx->source);
5324+
def.route_path = prefix ? join_route_paths(a, prefix, def.route_path) : NULL;
5325+
def.route_method = prefix ? def.route_method : NULL;
5326+
}
51835327
def.docstring = extract_docstring(a, child, ctx->source, ctx->language);
51845328

51855329
if (spec->branching_node_types && spec->branching_node_types[0]) {

‎tests/test_extraction.c‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4631,6 +4631,57 @@ TEST(extract_java_spring_relative_string_not_route_path) {
46314631
PASS();
46324632
}
46334633

4634+
/* Issue #1428: NestJS routes are TypeScript decorators — the class-level
4635+
* @Controller('users') prefix composes with the method-level @Get(':id')
4636+
* verb + path. Nest paths carry no leading slash, and the decorator's call is
4637+
* a `call_expression` (not Python's `call`), so no Route was ever formed. */
4638+
TEST(extract_ts_nestjs_controller_routes_issue1428) {
4639+
CBMFileResult *r =
4640+
extract("import { Controller, Get, Post, Patch, Delete, Param } from '@nestjs/common';\n"
4641+
"@Controller({ path: 'users', version: '1' })\n"
4642+
"export class UsersController {\n"
4643+
" @Get() findAll() { return []; }\n"
4644+
" @Get(':id') findOne(@Param('id') id: string) { return id; }\n"
4645+
" @Post() create() { return 1; }\n"
4646+
" @Patch(':id') update(@Param('id') id: string) { return id; }\n"
4647+
" @Delete('/:id') remove(@Param('id') id: string) { return id; }\n"
4648+
" helper() { return 0; }\n"
4649+
"}\n"
4650+
"@Controller('health')\n"
4651+
"export class HealthController {\n"
4652+
" @Get() check() { return { status: 'ok' }; }\n"
4653+
"}\n"
4654+
"@Controller()\n"
4655+
"export class RootController {\n"
4656+
" @Get('ping') ping() { return 'pong'; }\n"
4657+
"}\n",
4658+
CBM_LANG_TYPESCRIPT, "t", "src/users/users.controller.ts");
4659+
ASSERT_NOT_NULL(r);
4660+
ASSERT_FALSE(r->has_error);
4661+
static const struct {
4662+
const char *name;
4663+
const char *method;
4664+
const char *path;
4665+
} want[] = {
4666+
{"findAll", "GET", "/users"}, {"findOne", "GET", "/users/:id"},
4667+
{"create", "POST", "/users"}, {"update", "PATCH", "/users/:id"},
4668+
{"remove", "DELETE", "/users/:id"}, {"check", "GET", "/health"},
4669+
{"ping", "GET", "/ping"},
4670+
};
4671+
for (size_t i = 0; i < sizeof(want) / sizeof(want[0]); i++) {
4672+
const CBMDefinition *d = find_def_by_name(r, want[i].name);
4673+
ASSERT_NOT_NULL(d);
4674+
ASSERT_NOT_NULL(d->route_path);
4675+
ASSERT_STR_EQ(d->route_path, want[i].path);
4676+
ASSERT_STR_EQ(d->route_method, want[i].method);
4677+
}
4678+
const CBMDefinition *helper = find_def_by_name(r, "helper");
4679+
ASSERT_NOT_NULL(helper);
4680+
ASSERT_NULL(helper->route_path);
4681+
cbm_free_result(r);
4682+
PASS();
4683+
}
4684+
46344685
/* Return the file's Module definition (extraction pushes it first), or NULL. */
46354686
static const CBMDefinition *find_module_def(CBMFileResult *r) {
46364687
for (int i = 0; i < r->defs.count; i++) {
@@ -9043,6 +9094,7 @@ SUITE(extraction) {
90439094
RUN_TEST(extract_java_jaxrs_relative_path_templates);
90449095
RUN_TEST(extract_java_jaxrs_empty_path_means_class_path);
90459096
RUN_TEST(extract_java_spring_relative_string_not_route_path);
9097+
RUN_TEST(extract_ts_nestjs_controller_routes_issue1428);
90469098
RUN_TEST(extract_blazor_page_directive_routes_component);
90479099
RUN_TEST(extract_blazor_component_without_page_has_no_route);
90489100
RUN_TEST(extract_razor_page_directive_routes_cshtml_view);

0 commit comments

Comments
 (0)