Skip to content

Commit 475a698

Browse files
authored
Merge pull request DeusData#1097 from JhohanBustamante/fix/decorator-comment-drop
fix(extract): keep decorators separated by a comment
2 parents f18ed73 + 012be57 commit 475a698

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

‎internal/cbm/extract_defs.c‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,6 +1886,18 @@ static int collect_modifier_decorators(CBMArena *a, TSNode modifiers, const char
18861886
return idx;
18871887
}
18881888

1889+
/* Comments are NAMED nodes in tree-sitter, so a comment interleaved in a
1890+
* decorator run would end the walk and silently drop every decorator above it:
1891+
*
1892+
* @Post('login') <-- lost
1893+
* @HttpCode(HttpStatus.OK) <-- lost
1894+
* // why this route is throttled <-- walk stopped here
1895+
* @Throttle({ ... }) <-- kept
1896+
* async login(...)
1897+
*
1898+
* Documenting a decorator must not make it disappear from the graph, so treat
1899+
* comments as transparent — like the anonymous tokens already skipped below.
1900+
* (is_comment_node() is defined above, near the docstring helpers.) */
18891901
static const char **extract_decorators(CBMArena *a, TSNode node, const char *source,
18901902
CBMLanguage lang, const CBMLangSpec *spec) {
18911903
if (!spec->decorator_node_types || !spec->decorator_node_types[0]) {
@@ -1897,10 +1909,11 @@ static const char **extract_decorators(CBMArena *a, TSNode node, const char *sou
18971909
while (!ts_node_is_null(prev)) {
18981910
if (cbm_kind_in_set(prev, spec->decorator_node_types)) {
18991911
count++;
1900-
} else if (ts_node_is_named(prev)) {
1912+
} else if (ts_node_is_named(prev) && !is_comment_node(ts_node_type(prev))) {
19011913
/* A real preceding construct ends the decorator run. Anonymous
19021914
* tokens (e.g. TS `export` between `@Decorator` and the
1903-
* `class_declaration`) are skipped so the decorator is still seen. */
1915+
* `class_declaration`) and comments are skipped so the decorator
1916+
* is still seen. */
19041917
break;
19051918
}
19061919
prev = ts_node_prev_sibling(prev);
@@ -1937,7 +1950,7 @@ static const char **extract_decorators(CBMArena *a, TSNode node, const char *sou
19371950
while (!ts_node_is_null(prev) && idx < count) {
19381951
if (cbm_kind_in_set(prev, spec->decorator_node_types)) {
19391952
result[idx++] = cbm_node_text(a, prev, source);
1940-
} else if (ts_node_is_named(prev)) {
1953+
} else if (ts_node_is_named(prev) && !is_comment_node(ts_node_type(prev))) {
19411954
break;
19421955
}
19431956
prev = ts_node_prev_sibling(prev);

‎tests/test_extraction.c‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2930,6 +2930,30 @@ TEST(extract_java_jaxrs_path_composition_issue1005) {
29302930
PASS();
29312931
}
29322932

2933+
/* A comment between decorators must not drop the decorators above it.
2934+
* Comments are NAMED tree-sitter nodes, so the prev-sibling walk used to stop
2935+
* at one — a documented route (@Post + @HttpCode above an explanatory comment)
2936+
* silently lost those decorators and disappeared from route/authz queries. */
2937+
TEST(extract_ts_decorators_survive_interleaved_comment) {
2938+
CBMFileResult *r = extract("class AuthController {\n"
2939+
" @Post('login')\n"
2940+
" @HttpCode(HttpStatus.OK)\n"
2941+
" // throttled per IP and per account\n"
2942+
" @Throttle({ default: { ttl: 900_000, limit: 5 } })\n"
2943+
" async login(dto: LoginDto) { return 1; }\n"
2944+
"}\n",
2945+
CBM_LANG_TYPESCRIPT, "t", "auth.controller.ts");
2946+
ASSERT_NOT_NULL(r);
2947+
ASSERT_FALSE(r->has_error);
2948+
const CBMDefinition *m = find_def_by_name(r, "login");
2949+
ASSERT_NOT_NULL(m);
2950+
ASSERT(decorators_contain(m, "Throttle")); /* below the comment — always worked */
2951+
ASSERT(decorators_contain(m, "HttpCode")); /* above the comment — was dropped */
2952+
ASSERT(decorators_contain(m, "Post")); /* above the comment — was dropped */
2953+
cbm_free_result(r);
2954+
PASS();
2955+
}
2956+
29332957
/* Find an in-body call by its raw callee text; returns the call or NULL. */
29342958
static const CBMCall *find_call_by_callee(CBMFileResult *r, const char *callee) {
29352959
for (int i = 0; i < r->calls.count; i++) {
@@ -5089,6 +5113,7 @@ SUITE(extraction) {
50895113
RUN_TEST(walk_defs_no_truncation_over_4096_issue668);
50905114
RUN_TEST(extract_rust_test_attr_marks_is_test_issue855);
50915115
RUN_TEST(docstring_utf8_truncation_boundary_issue1017);
5116+
RUN_TEST(extract_ts_decorators_survive_interleaved_comment);
50925117

50935118
cbm_shutdown();
50945119
}

0 commit comments

Comments
 (0)