Skip to content

Commit ef55146

Browse files
committed
fix(ts-lsp): resolve JSX calls from top-level arrow components
Signed-off-by: lorenzozanee <wyz0707@proton.me>
1 parent ec4fbd3 commit ef55146

2 files changed

Lines changed: 123 additions & 1 deletion

File tree

‎internal/cbm/lsp/ts_lsp.c‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3760,6 +3760,31 @@ static void process_function_body(TSLSPContext *ctx, TSNode func_node, const cha
37603760
ctx->current_scope = saved_scope;
37613761
}
37623762

3763+
static void process_top_level_variable_arrows(TSLSPContext *ctx, TSNode declaration) {
3764+
if (!ctx || ts_node_is_null(declaration) || !ctx->module_qn)
3765+
return;
3766+
3767+
uint32_t count = ts_node_named_child_count(declaration);
3768+
for (uint32_t i = 0; i < count; i++) {
3769+
TSNode declarator = ts_node_named_child(declaration, i);
3770+
if (!node_kind_is(declarator, "variable_declarator"))
3771+
continue;
3772+
3773+
TSNode name = ts_node_child_by_field_name(declarator, "name", TS_LSP_FIELD_LEN("name"));
3774+
TSNode value =
3775+
ts_node_child_by_field_name(declarator, "value", TS_LSP_FIELD_LEN("value"));
3776+
if (!node_kind_is(name, "identifier") || !node_kind_is(value, "arrow_function"))
3777+
continue;
3778+
3779+
char *function_name = node_text(ctx, name);
3780+
if (!function_name)
3781+
continue;
3782+
const char *function_qn =
3783+
cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->module_qn, function_name);
3784+
process_function_body(ctx, value, function_qn, NULL);
3785+
}
3786+
}
3787+
37633788
void ts_lsp_process_file(TSLSPContext *ctx, TSNode root) {
37643789
if (!ctx || ts_node_is_null(root))
37653790
return;
@@ -3777,6 +3802,14 @@ void ts_lsp_process_file(TSLSPContext *ctx, TSNode root) {
37773802
const char *kind = ts_node_type(child);
37783803
if (strcmp(kind, "lexical_declaration") == 0 || strcmp(kind, "variable_declaration") == 0) {
37793804
ts_process_statement(ctx, child);
3805+
} else if (strcmp(kind, "export_statement") == 0) {
3806+
uint32_t count = ts_node_named_child_count(child);
3807+
for (uint32_t j = 0; j < count; j++) {
3808+
TSNode inner = ts_node_named_child(child, j);
3809+
if (node_kind_is(inner, "lexical_declaration") ||
3810+
node_kind_is(inner, "variable_declaration"))
3811+
ts_process_statement(ctx, inner);
3812+
}
37803813
}
37813814
}
37823815

@@ -3785,7 +3818,9 @@ void ts_lsp_process_file(TSLSPContext *ctx, TSNode root) {
37853818
TSNode child = kids[i];
37863819
const char *kind = ts_node_type(child);
37873820

3788-
if (strcmp(kind, "function_declaration") == 0) {
3821+
if (strcmp(kind, "lexical_declaration") == 0 || strcmp(kind, "variable_declaration") == 0) {
3822+
process_top_level_variable_arrows(ctx, child);
3823+
} else if (strcmp(kind, "function_declaration") == 0) {
37893824
TSNode name = ts_node_child_by_field_name(child, "name", TS_LSP_FIELD_LEN("name"));
37903825
if (ts_node_is_null(name) || !ctx->module_qn)
37913826
continue;
@@ -3824,6 +3859,9 @@ void ts_lsp_process_file(TSLSPContext *ctx, TSNode root) {
38243859
continue;
38253860
const char *fqn = cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->module_qn, fn);
38263861
process_function_body(ctx, inner, fqn, NULL);
3862+
} else if (strcmp(ik, "lexical_declaration") == 0 ||
3863+
strcmp(ik, "variable_declaration") == 0) {
3864+
process_top_level_variable_arrows(ctx, inner);
38273865
} else if (strcmp(ik, "class_declaration") == 0 ||
38283866
strcmp(ik, "internal_module") == 0) {
38293867
process_node(ctx, inner);

‎tests/test_ts_lsp.c‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,6 +1157,89 @@ TEST(tslsp_jsx_component_resolutions_join_exact_tag_sites) {
11571157
PASS();
11581158
}
11591159

1160+
TEST(tslsp_jsx_top_level_async_arrow_callers_keep_exact_identity) {
1161+
const char *source = "function Card(): JSX.Element { return <span/>; }\n"
1162+
"const Direct = async () => <Card/>;\n"
1163+
"export const Exported = async () => <Card/>;\n"
1164+
"function Declared(): JSX.Element { return <Card/>; }\n";
1165+
const char *const declarations[] = {"const Direct", "export const Exported",
1166+
"function Declared"};
1167+
const char *const callers[] = {"test.main.Direct", "test.main.Exported", "test.main.Declared"};
1168+
CBMFileResult *r = extract_tsx(source);
1169+
const bool result_created = r != NULL;
1170+
bool exact_carrier[3] = {false};
1171+
bool exact_resolution[3] = {false};
1172+
bool resolved_invocation[3] = {false};
1173+
bool resolved_card_callee[3] = {false};
1174+
bool resolved_jsx_strategy[3] = {false};
1175+
int carrier_count[3] = {0};
1176+
int failed_cases = result_created ? 0 : 1;
1177+
1178+
if (!result_created)
1179+
printf(" JSX caller diagnostic: extraction failed\n");
1180+
if (r) {
1181+
for (int i = 0; i < 3; i++) {
1182+
const char *declaration = strstr(source, declarations[i]);
1183+
const char *site = declaration ? strstr(declaration, "<Card/>") : NULL;
1184+
if (!site) {
1185+
printf(" JSX caller diagnostic: source site missing for %s\n", callers[i]);
1186+
failed_cases++;
1187+
continue;
1188+
}
1189+
const uint32_t start = (uint32_t)(site - source);
1190+
const uint32_t end = start + (uint32_t)strlen("<Card/>");
1191+
const CBMCall *carrier = NULL;
1192+
for (int j = 0; j < r->calls.count; j++) {
1193+
const CBMCall *call = &r->calls.items[j];
1194+
if (call->callee_name && strcmp(call->callee_name, "Card") == 0 &&
1195+
call->site_start_byte == start && call->site_end_byte == end) {
1196+
carrier = call;
1197+
carrier_count[i]++;
1198+
}
1199+
}
1200+
1201+
const CBMResolvedCall *resolved = carrier ? cbm_pipeline_find_lsp_resolution(
1202+
&r->resolved_calls, carrier, false)
1203+
: NULL;
1204+
exact_carrier[i] = carrier && carrier->enclosing_func_qn &&
1205+
strcmp(carrier->enclosing_func_qn, callers[i]) == 0 &&
1206+
carrier->site_start_byte == start && carrier->site_end_byte == end &&
1207+
carrier->requires_lsp_resolution;
1208+
exact_resolution[i] = resolved && resolved->caller_qn &&
1209+
strcmp(resolved->caller_qn, callers[i]) == 0 &&
1210+
resolved->site_start_byte == start && resolved->site_end_byte == end;
1211+
resolved_invocation[i] = resolved && resolved->kind == CBM_RESOLVED_INVOCATION;
1212+
if (resolved && resolved->callee_qn) {
1213+
const size_t callee_len = strlen(resolved->callee_qn);
1214+
resolved_card_callee[i] = callee_len >= strlen(".Card") &&
1215+
strcmp(resolved->callee_qn + callee_len - strlen(".Card"),
1216+
".Card") == 0;
1217+
}
1218+
resolved_jsx_strategy[i] =
1219+
resolved && resolved->strategy && strcmp(resolved->strategy, "lsp_ts_jsx") == 0;
1220+
if (carrier_count[i] != 1 || !exact_carrier[i] || !exact_resolution[i] ||
1221+
!resolved_invocation[i] || !resolved_card_callee[i] || !resolved_jsx_strategy[i]) {
1222+
printf(" JSX caller diagnostic: expected=%s span=%u:%u carriers=%d caller=%s "
1223+
"requires_lsp=%d resolution=%s resolution_span=%u:%u kind=%d callee=%s "
1224+
"strategy=%s\n",
1225+
callers[i], start, end, carrier_count[i],
1226+
carrier && carrier->enclosing_func_qn ? carrier->enclosing_func_qn : "(null)",
1227+
carrier ? carrier->requires_lsp_resolution : 0,
1228+
resolved && resolved->caller_qn ? resolved->caller_qn : "(null)",
1229+
resolved ? resolved->site_start_byte : 0, resolved ? resolved->site_end_byte : 0,
1230+
resolved ? resolved->kind : -1,
1231+
resolved && resolved->callee_qn ? resolved->callee_qn : "(null)",
1232+
resolved && resolved->strategy ? resolved->strategy : "(null)");
1233+
failed_cases++;
1234+
}
1235+
}
1236+
}
1237+
1238+
cbm_free_result(r);
1239+
ASSERT_EQ(failed_cases, 0);
1240+
PASS();
1241+
}
1242+
11601243
/* A callable value named like a module component owns the JSX tag. The LSP
11611244
* pass must not confidently redirect it to the module function, and the
11621245
* carrier must require semantic resolution so graph construction cannot do
@@ -4867,6 +4950,7 @@ SUITE(ts_lsp) {
48674950
RUN_TEST(tslsp_jsx_component_with_children);
48684951
RUN_TEST(tslsp_jsx_import_requires_registered_component);
48694952
RUN_TEST(tslsp_jsx_component_resolutions_join_exact_tag_sites);
4953+
RUN_TEST(tslsp_jsx_top_level_async_arrow_callers_keep_exact_identity);
48704954
RUN_TEST(tslsp_jsx_local_tag_shadow_blocks_module_component);
48714955
RUN_TEST(tslsp_jsx_intrinsic_skipped);
48724956
RUN_TEST(tslsp_jsx_nested_component);

0 commit comments

Comments
 (0)