Skip to content

Commit 60c307e

Browse files
authored
Merge pull request DeusData#2242 from lorenzozanee/fix/typescript-awaited-generic-calls
fix(typescript): preserve awaited generic call edges
2 parents c58eb61 + e8a46c1 commit 60c307e

4 files changed

Lines changed: 45 additions & 0 deletions

File tree

‎internal/cbm/extract_calls.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,18 @@ static char *extract_constructor_callee(CBMArena *a, TSNode node, const char *so
258258
}
259259

260260
// Try common field-based callee resolution (function, name, method fields).
261+
static TSNode unwrap_await_callee(TSNode node) {
262+
if (ts_node_is_null(node) || strcmp(ts_node_type(node), "await_expression") != 0 ||
263+
ts_node_named_child_count(node) == 0) {
264+
return node;
265+
}
266+
return ts_node_named_child(node, 0);
267+
}
268+
261269
static char *extract_callee_from_fields(CBMArena *a, TSNode node, const char *source) {
262270
// Try "function" field
263271
TSNode func_node = ts_node_child_by_field_name(node, TS_FIELD("function"));
272+
func_node = unwrap_await_callee(func_node);
264273
if (!ts_node_is_null(func_node)) {
265274
const char *fk = ts_node_type(func_node);
266275
if (strcmp(fk, "selector_expression") == 0) {
@@ -3521,6 +3530,7 @@ static CBMPrimaryCalleeSelection select_primary_callee(CBMExtractCtx *ctx, TSNod
35213530
}
35223531

35233532
selection.expr = language_specific_callee_expr(ctx->language, node);
3533+
selection.expr = unwrap_await_callee(selection.expr);
35243534
if (is_dynamic_callee_expr(ctx, selection.expr)) {
35253535
selection.expr = (TSNode){0};
35263536
return selection;

‎internal/cbm/lsp/ts_lsp.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2800,8 +2800,17 @@ static void resolve_value_references_at(TSLSPContext *ctx, TSNode call_node) {
28002800
}
28012801
}
28022802

2803+
static TSNode unwrap_await_callee(TSNode node) {
2804+
if (ts_node_is_null(node) || strcmp(ts_node_type(node), "await_expression") != 0 ||
2805+
ts_node_named_child_count(node) == 0) {
2806+
return node;
2807+
}
2808+
return ts_node_named_child(node, 0);
2809+
}
2810+
28032811
static void resolve_call_at(TSLSPContext *ctx, TSNode call_node) {
28042812
TSNode fn = ts_node_child_by_field_name(call_node, "function", TS_LSP_FIELD_LEN("function"));
2813+
fn = unwrap_await_callee(fn);
28052814
if (ts_node_is_null(fn))
28062815
return;
28072816

‎tests/test_extraction.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4362,6 +4362,18 @@ TEST(swift_labeled_call_string_arg_issue1892) {
43624362
* literal, consumed as client(buildPath(id)). The builder's URL is recorded in
43634363
* the per-file constant map and resolved at the call site, for both return
43644364
* statements and arrow expression bodies. */
4365+
TEST(extract_ts_await_generic_call_issue2210) {
4366+
CBMFileResult *r = extract("function parseJsonBody<T>() { return {} as T; }\n"
4367+
"async function plain() { return await parseJsonBody(); }\n"
4368+
"async function generic() { return await parseJsonBody<string>(); }\n",
4369+
CBM_LANG_TYPESCRIPT, "t", "await.ts");
4370+
ASSERT_NOT_NULL(r);
4371+
ASSERT_FALSE(r->has_error);
4372+
ASSERT_EQ(count_calls_named(r, "parseJsonBody"), 2);
4373+
cbm_free_result(r);
4374+
PASS();
4375+
}
4376+
43654377
TEST(extract_ts_url_builder_issue1009) {
43664378
CBMFileResult *r = extract("function thingDetail(id: string): string {\n"
43674379
" return `/api/v1/things/${id}/detail`;\n"
@@ -7930,6 +7942,7 @@ SUITE(extraction) {
79307942
RUN_TEST(extract_go_binary_concat_url_issue1249);
79317943
RUN_TEST(extract_go_binary_concat_url_no_literal_suffix_issue1249);
79327944
RUN_TEST(extract_ts_url_builder_issue1009);
7945+
RUN_TEST(extract_ts_await_generic_call_issue2210);
79337946
RUN_TEST(extract_ts_route_handler_after_named_middleware);
79347947
RUN_TEST(extract_ts_route_handler_after_inline_middleware);
79357948
RUN_TEST(extract_ts_url_builder_composed_issue1009);

‎tests/test_ts_lsp.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,17 @@ TEST(tslsp_nocrash_megasource) {
729729

730730
/* ── Category 11: generics (deeper) ─────────────────────────────────────────── */
731731

732+
TEST(tslsp_await_generic_call_issue2210) {
733+
CBMFileResult *r = extract_ts("function parseJsonBody<T>(): T { return null as T; }\n"
734+
"async function plain() { return await parseJsonBody(); }\n"
735+
"async function generic() { return await parseJsonBody<string>(); }\n");
736+
ASSERT_NOT_NULL(r);
737+
ASSERT_TRUE(find_resolved(r, "plain", "parseJsonBody") >= 0);
738+
ASSERT_TRUE(find_resolved(r, "generic", "parseJsonBody") >= 0);
739+
cbm_free_result(r);
740+
PASS();
741+
}
742+
732743
TEST(tslsp_generic_identity_inference) {
733744
CBMFileResult *r = extract_ts("function identity<T>(x: T): T { return x; }\n"
734745
"class Box { use(): void {} }\n"
@@ -4316,6 +4327,8 @@ SUITE(ts_lsp) {
43164327
RUN_TEST(tslsp_nocrash_using_decl);
43174328
RUN_TEST(tslsp_nocrash_megasource);
43184329

4330+
RUN_TEST(tslsp_await_generic_call_issue2210);
4331+
43194332
/* Category 11: generics deeper */
43204333
RUN_TEST(tslsp_generic_identity_inference);
43214334
RUN_TEST(tslsp_generic_array_map_chain);

0 commit comments

Comments
 (0)