@@ -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 ]) {
0 commit comments