Skip to content

Commit 3815385

Browse files
ngallodevclaude
andauthored
fix(typescript): preserve awaited generic call edges (#7)
## Summary - Unwrap awaited callees in TypeScript extraction and LSP resolution so awaited generic calls retain call edges. - Adapt upstream TypeScript await commit `e8a46c16`. ## Provenance and gates - Agent Run: `luna-cbm-ts-await-20260920`; source revision `a43b2201`; worker commit `c540426f`. - Agent-Workflow reports a valid completion receipt. Independent review, evaluation, acceptance, and integration are pending. - `git diff --check main...HEAD` passed. No tests were run for this PR request. ## Review follow-up Addressed in 9efedce. 1. **Incomplete coverage in ts_lsp.c**: `unwrap_await_callee` is now applied in the `ts_eval_expr_type` call_expression branch and in the contextual callback typing, as well as in `resolve_call_at`. There are no other reads of a call_expression's `function` field in the file. New tests: `tslsp_await_generic_call_type_issue2210` (`const v = await load<Foo>(); v.method()`) and `tslsp_await_generic_member_call_issue2210` (`await obj.run<Foo>()`). One caveat: for these shapes, `ts_signature_for_call` already falls back to evaluating the await node, so the two new unwrap sites are consistent with the rest of the file but no test fails without them. 2. **Applied to all languages**: the unwrap in extract_calls.c is now limited to the TypeScript, TSX and JavaScript grammars. 3. **Comment placement**: moved back above `extract_callee_from_fields`. 4. **Test gaps**: added a member-callee extraction test (`extract_ts_await_generic_member_call_issue2210`). Nested awaits are not covered. The parse shape is confirmed indirectly: removing the ts_lsp.c unwraps fails the PR's original LSP test and the member test. `scripts/test.sh --suites ts_lsp,extraction`: 638 passed. Lint passes. Separate gaps found along the way, not addressed here: `return obj.run()` inside an async function does not resolve, and `Promise<Foo>` return types are not unwrapped through `await`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8f35274 commit 3815385

4 files changed

Lines changed: 100 additions & 0 deletions

File tree

‎internal/cbm/extract_calls.c‎

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

261+
/* tree-sitter-typescript parses `await f<T>()` as a call_expression whose
262+
* function field is the await_expression; unwrap it to reach the callee.
263+
* Gated on the JS/TS grammars: other languages' await_expression nodes are
264+
* not call wrappers. */
265+
static TSNode unwrap_await_callee(TSNode node) {
266+
if (ts_node_is_null(node) || strcmp(ts_node_type(node), "await_expression") != 0 ||
267+
ts_node_named_child_count(node) == 0) {
268+
return node;
269+
}
270+
const TSLanguage *tl = ts_node_language(node);
271+
if (tl != cbm_ts_language(CBM_LANG_TYPESCRIPT) && tl != cbm_ts_language(CBM_LANG_TSX) &&
272+
tl != cbm_ts_language(CBM_LANG_JAVASCRIPT)) {
273+
return node;
274+
}
275+
return ts_node_named_child(node, 0);
276+
}
277+
261278
// Try common field-based callee resolution (function, name, method fields).
262279
static char *extract_callee_from_fields(CBMArena *a, TSNode node, const char *source) {
263280
// Try "function" field
264281
TSNode func_node = ts_node_child_by_field_name(node, TS_FIELD("function"));
282+
func_node = unwrap_await_callee(func_node);
265283
if (!ts_node_is_null(func_node)) {
266284
const char *fk = ts_node_type(func_node);
267285
if (strcmp(fk, "selector_expression") == 0) {
@@ -3435,6 +3453,7 @@ static CBMPrimaryCalleeSelection select_primary_callee(CBMExtractCtx *ctx, TSNod
34353453
}
34363454

34373455
selection.expr = language_specific_callee_expr(ctx->language, node);
3456+
selection.expr = unwrap_await_callee(selection.expr);
34383457
if (is_dynamic_callee_expr(ctx, selection.expr)) {
34393458
selection.expr = (TSNode){0};
34403459
return selection;

‎internal/cbm/lsp/ts_lsp.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,6 +1999,16 @@ static const CBMType *ts_method_signature_for_receiver(TSLSPContext *ctx, const
19991999
return substituted ? substituted : method->signature;
20002000
}
20012001

2002+
/* `await f<T>()` parses as a call_expression whose function field is the
2003+
* await_expression; unwrap it to reach the callee. */
2004+
static TSNode unwrap_await_callee(TSNode node) {
2005+
if (ts_node_is_null(node) || strcmp(ts_node_type(node), "await_expression") != 0 ||
2006+
ts_node_named_child_count(node) == 0) {
2007+
return node;
2008+
}
2009+
return ts_node_named_child(node, 0);
2010+
}
2011+
20022012
/* Return the function signature selected at this call site. This is the
20032013
* single boundary shared by chained return typing and contextual callback
20042014
* typing; both must observe the same overload as edge resolution. */
@@ -2187,6 +2197,7 @@ const CBMType *ts_eval_expr_type(TSLSPContext *ctx, TSNode node) {
21872197
}
21882198
} else if (strcmp(kind, "call_expression") == 0) {
21892199
TSNode fn = ts_node_child_by_field_name(node, "function", TS_LSP_FIELD_LEN("function"));
2200+
fn = unwrap_await_callee(fn);
21902201
if (!ts_node_is_null(fn)) {
21912202
TSNode call_args =
21922203
ts_node_child_by_field_name(node, "arguments", TS_LSP_FIELD_LEN("arguments"));
@@ -2802,6 +2813,7 @@ static void resolve_value_references_at(TSLSPContext *ctx, TSNode call_node) {
28022813

28032814
static void resolve_call_at(TSLSPContext *ctx, TSNode call_node) {
28042815
TSNode fn = ts_node_child_by_field_name(call_node, "function", TS_LSP_FIELD_LEN("function"));
2816+
fn = unwrap_await_callee(fn);
28052817
if (ts_node_is_null(fn))
28062818
return;
28072819

@@ -3302,6 +3314,7 @@ static void process_node(TSLSPContext *ctx, TSNode node) {
33023314
do {
33033315
TSNode fn_node =
33043316
ts_node_child_by_field_name(node, "function", TS_LSP_FIELD_LEN("function"));
3317+
fn_node = unwrap_await_callee(fn_node);
33053318
if (ts_node_is_null(fn_node))
33063319
break;
33073320
TSNode args =

‎tests/test_extraction.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4056,6 +4056,30 @@ TEST(extract_ts_url_builder_issue1009) {
40564056
PASS();
40574057
}
40584058

4059+
TEST(extract_ts_await_generic_call_issue2210) {
4060+
CBMFileResult *r = extract("function parseJsonBody<T>() { return {} as T; }\n"
4061+
"async function plain() { return await parseJsonBody(); }\n"
4062+
"async function generic() { return await parseJsonBody<string>(); }\n",
4063+
CBM_LANG_TYPESCRIPT, "t", "await.ts");
4064+
ASSERT_NOT_NULL(r);
4065+
ASSERT_FALSE(r->has_error);
4066+
ASSERT_EQ(count_calls_named(r, "parseJsonBody"), 2);
4067+
cbm_free_result(r);
4068+
PASS();
4069+
}
4070+
4071+
TEST(extract_ts_await_generic_member_call_issue2210) {
4072+
CBMFileResult *r =
4073+
extract("class Api { run<T>(): void {} }\n"
4074+
"async function go() { const obj = new Api(); await obj.run<string>(); }\n",
4075+
CBM_LANG_TYPESCRIPT, "t", "await_member.ts");
4076+
ASSERT_NOT_NULL(r);
4077+
ASSERT_FALSE(r->has_error);
4078+
ASSERT_EQ(count_calls_named(r, "obj.run"), 1);
4079+
cbm_free_result(r);
4080+
PASS();
4081+
}
4082+
40594083
/* Issue #1009 (composed builders): a builder whose template inlines an earlier
40604084
* builder's call plus a query string: `return \`${basePath(id)}?${params}\``.
40614085
* The known-substitution is inlined and the query string is truncated, so the
@@ -6903,6 +6927,8 @@ SUITE(extraction) {
69036927
RUN_TEST(extract_go_binary_concat_url_issue1249);
69046928
RUN_TEST(extract_go_binary_concat_url_no_literal_suffix_issue1249);
69056929
RUN_TEST(extract_ts_url_builder_issue1009);
6930+
RUN_TEST(extract_ts_await_generic_call_issue2210);
6931+
RUN_TEST(extract_ts_await_generic_member_call_issue2210);
69066932
RUN_TEST(extract_ts_url_builder_composed_issue1009);
69076933
RUN_TEST(extract_c_url_builder_gated_issue1009);
69086934
RUN_TEST(extract_ts_url_builder_mixed_returns_issue1009);

‎tests/test_ts_lsp.c‎

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

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

732+
TEST(tslsp_await_generic_call_issue2210) {
733+
CBMFileResult *r =
734+
extract_ts("function parseJsonBody<T>(): T { return null as T; }\n"
735+
"async function plain() { return await parseJsonBody(); }\n"
736+
"async function generic() { return await parseJsonBody<string>(); }\n");
737+
ASSERT_NOT_NULL(r);
738+
ASSERT_TRUE(find_resolved(r, "plain", "parseJsonBody") >= 0);
739+
ASSERT_TRUE(find_resolved(r, "generic", "parseJsonBody") >= 0);
740+
cbm_free_result(r);
741+
PASS();
742+
}
743+
744+
TEST(tslsp_await_generic_call_type_issue2210) {
745+
CBMFileResult *r =
746+
extract_ts("class Foo { method(): void {} }\n"
747+
"function load<T>(): Foo { return new Foo(); }\n"
748+
"async function go() { const v = await load<Foo>(); v.method(); }\n");
749+
ASSERT_NOT_NULL(r);
750+
ASSERT_GTE(require_resolved(r, ".go", "Foo.method"), 0);
751+
cbm_free_result(r);
752+
PASS();
753+
}
754+
755+
TEST(tslsp_await_generic_member_call_issue2210) {
756+
CBMFileResult *r = extract_ts("class Foo { method(): void {} }\n"
757+
"class Api { run<T>(): Foo { return new Foo(); } }\n"
758+
"async function go() {\n"
759+
" const obj = new Api();\n"
760+
" const v = await obj.run<Foo>();\n"
761+
" v.method();\n"
762+
"}\n");
763+
ASSERT_NOT_NULL(r);
764+
ASSERT_GTE(require_resolved(r, ".go", "Api.run"), 0);
765+
ASSERT_GTE(require_resolved(r, ".go", "Foo.method"), 0);
766+
cbm_free_result(r);
767+
PASS();
768+
}
769+
732770
TEST(tslsp_generic_identity_inference) {
733771
CBMFileResult *r = extract_ts("function identity<T>(x: T): T { return x; }\n"
734772
"class Box { use(): void {} }\n"
@@ -4316,7 +4354,11 @@ SUITE(ts_lsp) {
43164354
RUN_TEST(tslsp_nocrash_using_decl);
43174355
RUN_TEST(tslsp_nocrash_megasource);
43184356

4357+
RUN_TEST(tslsp_await_generic_call_issue2210);
4358+
43194359
/* Category 11: generics deeper */
4360+
RUN_TEST(tslsp_await_generic_call_type_issue2210);
4361+
RUN_TEST(tslsp_await_generic_member_call_issue2210);
43204362
RUN_TEST(tslsp_generic_identity_inference);
43214363
RUN_TEST(tslsp_generic_array_map_chain);
43224364
RUN_TEST(tslsp_generic_promise_resolve);

0 commit comments

Comments
 (0)