Skip to content

Commit 2a9796e

Browse files
committed
fix(lsp): preserve overloads through imported TypeScript aliases
Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent fd9f1d8 commit 2a9796e

4 files changed

Lines changed: 171 additions & 19 deletions

File tree

‎internal/cbm/lsp/ts_lsp.c‎

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2231,7 +2231,8 @@ static const CBMType *ts_signature_for_call(TSLSPContext *ctx, TSNode function,
22312231
if (!registered) {
22322232
const char *module_qn = ts_import_module_for_local(ctx, name, NULL);
22332233
if (module_qn)
2234-
registered = ts_lookup_symbol_for_call(ctx, module_qn, name, args);
2234+
registered = ts_lookup_symbol_for_call(ctx, module_qn,
2235+
ts_import_export_name(ctx, name), args);
22352236
}
22362237
if (registered)
22372238
return registered->signature;
@@ -4048,22 +4049,27 @@ void ts_lsp_add_import(TSLSPContext *ctx, const char *local_name, const char *mo
40484049
}
40494050

40504051
static void ts_lsp_add_import_alias(TSLSPContext *ctx, const char *local, const char *exported) {
4051-
int new_count = ctx->import_alias_count + 1;
4052-
const char **locals =
4053-
(const char **)cbm_arena_alloc(ctx->arena, (size_t)new_count * sizeof(const char *));
4054-
const char **exports =
4055-
(const char **)cbm_arena_alloc(ctx->arena, (size_t)new_count * sizeof(const char *));
4056-
if (!locals || !exports)
4057-
return;
4058-
for (int i = 0; i < ctx->import_alias_count; i++) {
4059-
locals[i] = ctx->import_alias_locals[i];
4060-
exports[i] = ctx->import_alias_exports[i];
4061-
}
4062-
locals[ctx->import_alias_count] = local;
4063-
exports[ctx->import_alias_count] = exported;
4064-
ctx->import_alias_locals = locals;
4065-
ctx->import_alias_exports = exports;
4066-
ctx->import_alias_count = new_count;
4052+
if ((size_t)ctx->import_alias_count == ctx->import_alias_capacity) {
4053+
/* The arena retains old arrays: grow geometrically so collecting A
4054+
* aliases retains O(A) pointers instead of O(A squared). */
4055+
size_t capacity = ctx->import_alias_capacity ? ctx->import_alias_capacity * 2 : 8;
4056+
const char **locals =
4057+
(const char **)cbm_arena_alloc(ctx->arena, capacity * sizeof(const char *));
4058+
const char **exports =
4059+
(const char **)cbm_arena_alloc(ctx->arena, capacity * sizeof(const char *));
4060+
if (!locals || !exports)
4061+
return;
4062+
for (int i = 0; i < ctx->import_alias_count; i++) {
4063+
locals[i] = ctx->import_alias_locals[i];
4064+
exports[i] = ctx->import_alias_exports[i];
4065+
}
4066+
ctx->import_alias_locals = locals;
4067+
ctx->import_alias_exports = exports;
4068+
ctx->import_alias_capacity = capacity;
4069+
}
4070+
ctx->import_alias_locals[ctx->import_alias_count] = local;
4071+
ctx->import_alias_exports[ctx->import_alias_count] = exported;
4072+
ctx->import_alias_count++;
40674073
}
40684074

40694075
/* Record each `import { foo as bar }` binding of the file (#2297). The import

‎internal/cbm/lsp/ts_lsp.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ typedef struct {
3939
const char **import_alias_locals;
4040
const char **import_alias_exports;
4141
int import_alias_count;
42+
size_t import_alias_capacity;
4243

4344
// File / surrounding context.
4445
const char *module_qn; // QN of this file's module

‎tests/test_pipeline.c‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9341,8 +9341,30 @@ TEST(pipeline_ts_renamed_import_call_issue2297) {
93419341
cross_file_call_has_strategy(s, project, "callsPlain", "fooUniqueQrs", "lsp_ts_import");
93429342
bool renamed = cross_file_call_has_strategy(s, project, "callsRenamed", "fooUniqueQrs",
93439343
"lsp_import_alias");
9344-
bool wrapper = cross_file_call_has_strategy(s, project, "buildHealthUniqueQrs",
9345-
"buildHealthUniqueQrs", "lsp_import_alias");
9344+
char wrapper_qn[512];
9345+
char exported_qn[512];
9346+
snprintf(wrapper_qn, sizeof(wrapper_qn), "%s.src.wrapper.buildHealthUniqueQrs", project);
9347+
snprintf(exported_qn, sizeof(exported_qn), "%s.src.lib.buildHealthUniqueQrs", project);
9348+
cbm_node_t wrapper_node = {0};
9349+
cbm_node_t exported_node = {0};
9350+
bool wrapper = false;
9351+
bool recursive = false;
9352+
if (cbm_store_find_node_by_qn(s, project, wrapper_qn, &wrapper_node) == CBM_STORE_OK &&
9353+
cbm_store_find_node_by_qn(s, project, exported_qn, &exported_node) == CBM_STORE_OK) {
9354+
cbm_edge_t *edges = NULL;
9355+
int edge_count = 0;
9356+
cbm_store_find_edges_by_source_type(s, wrapper_node.id, "CALLS", &edges, &edge_count);
9357+
for (int i = 0; i < edge_count; i++) {
9358+
if (edges[i].target_id == exported_node.id && edges[i].properties_json &&
9359+
strstr(edges[i].properties_json, "lsp_import_alias"))
9360+
wrapper = true;
9361+
if (edges[i].target_id == wrapper_node.id)
9362+
recursive = true;
9363+
}
9364+
cbm_store_free_edges(edges, edge_count);
9365+
}
9366+
cbm_node_free_fields(&wrapper_node);
9367+
cbm_node_free_fields(&exported_node);
93469368
bool renamed_class = cross_file_call_has_strategy(s, project, "viaRenamedClass",
93479369
"pingUniqueQrs", "lsp_ts_method");
93489370

@@ -9353,6 +9375,7 @@ TEST(pipeline_ts_renamed_import_call_issue2297) {
93539375
ASSERT_TRUE(plain);
93549376
ASSERT_TRUE(renamed);
93559377
ASSERT_TRUE(wrapper);
9378+
ASSERT_FALSE(recursive);
93569379
ASSERT_TRUE(renamed_class);
93579380
PASS();
93589381
}

‎tests/test_ts_lsp.c‎

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2771,6 +2771,125 @@ TEST(tslsp_crossfile_chain_through_return) {
27712771
PASS();
27722772
}
27732773

2774+
/* The call target and its return signature must use the same exported name,
2775+
* even when the module also exports a function named like the local alias. */
2776+
static int ts_renamed_import_return_chain(bool shared_registry) {
2777+
const char *source = "import { make as alias } from './lib';\n"
2778+
"function go() { alias().read(); }\n";
2779+
CBMLSPDef defs[] = {
2780+
{.qualified_name = "test.main.go",
2781+
.short_name = "go",
2782+
.label = "Function",
2783+
.lang = CBM_LANG_TYPESCRIPT,
2784+
.def_module_qn = "test.main"},
2785+
{.qualified_name = "test.lib.Good",
2786+
.short_name = "Good",
2787+
.label = "Class",
2788+
.lang = CBM_LANG_TYPESCRIPT,
2789+
.def_module_qn = "test.lib",
2790+
.method_names_str = "read"},
2791+
{.qualified_name = "test.lib.Good.read",
2792+
.short_name = "read",
2793+
.label = "Method",
2794+
.lang = CBM_LANG_TYPESCRIPT,
2795+
.def_module_qn = "test.lib",
2796+
.receiver_type = "test.lib.Good"},
2797+
{.qualified_name = "test.lib.Bad",
2798+
.short_name = "Bad",
2799+
.label = "Class",
2800+
.lang = CBM_LANG_TYPESCRIPT,
2801+
.def_module_qn = "test.lib",
2802+
.method_names_str = "read"},
2803+
{.qualified_name = "test.lib.Bad.read",
2804+
.short_name = "read",
2805+
.label = "Method",
2806+
.lang = CBM_LANG_TYPESCRIPT,
2807+
.def_module_qn = "test.lib",
2808+
.receiver_type = "test.lib.Bad"},
2809+
{.qualified_name = "test.lib.make",
2810+
.short_name = "make",
2811+
.label = "Function",
2812+
.lang = CBM_LANG_TYPESCRIPT,
2813+
.def_module_qn = "test.lib",
2814+
.return_types = "test.lib.Good"},
2815+
{.qualified_name = "test.lib.alias",
2816+
.short_name = "alias",
2817+
.label = "Function",
2818+
.lang = CBM_LANG_TYPESCRIPT,
2819+
.def_module_qn = "test.lib",
2820+
.return_types = "test.lib.Bad"},
2821+
};
2822+
enum { NDEFS = (int)(sizeof(defs) / sizeof(defs[0])) };
2823+
const char *imp_names[] = {"alias"};
2824+
const char *imp_qns[] = {"test.lib"};
2825+
CBMArena arena;
2826+
cbm_arena_init(&arena);
2827+
CBMResolvedCallArray out = {0};
2828+
if (shared_registry) {
2829+
CBMTypeRegistry *reg = cbm_ts_build_cross_registry(&arena, defs, NDEFS);
2830+
ASSERT_NOT_NULL(reg);
2831+
cbm_run_ts_lsp_cross_with_registry(&arena, source, (int)strlen(source), "test.main", false,
2832+
false, false, reg, defs, NDEFS, imp_names, imp_qns, 1,
2833+
NULL, &out);
2834+
} else {
2835+
cbm_run_ts_lsp_cross(&arena, source, (int)strlen(source), "test.main", false, false, false,
2836+
defs, NDEFS, imp_names, imp_qns, 1, NULL, &out);
2837+
}
2838+
bool direct = has_exact_resolved(&out, "test.main.go", "test.lib.make", "lsp_import_alias");
2839+
bool good = has_exact_resolved(&out, "test.main.go", "test.lib.Good.read", "lsp_ts_method");
2840+
bool bad = has_exact_resolved(&out, "test.main.go", "test.lib.Bad.read", "lsp_ts_method");
2841+
cbm_arena_destroy(&arena);
2842+
ASSERT_TRUE(direct);
2843+
ASSERT_FALSE(bad);
2844+
ASSERT_TRUE(good);
2845+
PASS();
2846+
}
2847+
2848+
TEST(tslsp_crossfile_renamed_import_return_chain) {
2849+
return ts_renamed_import_return_chain(false);
2850+
}
2851+
2852+
TEST(tslsp_crossfile_renamed_import_return_chain_shared) {
2853+
return ts_renamed_import_return_chain(true);
2854+
}
2855+
2856+
/* Keep the supplied import map at one entry: this measures the AST alias
2857+
* collector independently of the older import-map insertion path. */
2858+
static size_t ts_import_alias_arena_bytes(int count) {
2859+
size_t capacity = (size_t)count * 48 + 64;
2860+
char *source = malloc(capacity);
2861+
if (!source)
2862+
return 0;
2863+
size_t len = (size_t)snprintf(source, capacity, "import { ");
2864+
for (int i = 0; i < count; i++) {
2865+
len += (size_t)snprintf(source + len, capacity - len, "%sf%d as a%d", i ? ", " : "", i, i);
2866+
}
2867+
len += (size_t)snprintf(source + len, capacity - len, " } from './lib';\n");
2868+
const char *imp_names[] = {"a0"};
2869+
const char *imp_qns[] = {"test.lib"};
2870+
CBMArena arena;
2871+
cbm_arena_init(&arena);
2872+
CBMResolvedCallArray out = {0};
2873+
cbm_run_ts_lsp_cross(&arena, source, (int)len, "test.main", false, false, false, NULL, 0,
2874+
imp_names, imp_qns, 1, NULL, &out);
2875+
size_t bytes = cbm_arena_total(&arena);
2876+
cbm_arena_destroy(&arena);
2877+
free(source);
2878+
return bytes;
2879+
}
2880+
2881+
TEST(tslsp_import_alias_arena_growth) {
2882+
size_t small = ts_import_alias_arena_bytes(1024);
2883+
size_t large = ts_import_alias_arena_bytes(2048);
2884+
ASSERT_GT(small, 0);
2885+
ASSERT_GT(large, small);
2886+
printf(" aliases arena bytes: 1024=%zu 2048=%zu ", small, large);
2887+
/* Linear source/AST work plus geometric storage stays below 3x; retaining
2888+
* two complete arrays at every insertion grows approximately 4x. */
2889+
ASSERT(large < small * 3);
2890+
PASS();
2891+
}
2892+
27742893
TEST(tslsp_crossfile_no_def_match) {
27752894
/* Function that imports a symbol not in the def list — should resolve safely
27762895
* (emit unresolved, no crash). */
@@ -5236,6 +5355,9 @@ SUITE(ts_lsp) {
52365355
RUN_TEST(tslsp_scale_many_defs_no_crash_issue344);
52375356
RUN_TEST(tslsp_crossfile_function_call);
52385357
RUN_TEST(tslsp_crossfile_chain_through_return);
5358+
RUN_TEST(tslsp_crossfile_renamed_import_return_chain);
5359+
RUN_TEST(tslsp_crossfile_renamed_import_return_chain_shared);
5360+
RUN_TEST(tslsp_import_alias_arena_growth);
52395361
RUN_TEST(tslsp_crossfile_no_def_match);
52405362
RUN_TEST(tslsp_crossfile_renamed_import_issue2297);
52415363
RUN_TEST(tslsp_crossfile_renamed_import_missing_export_issue2297);

0 commit comments

Comments
 (0)