Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(lsp): preserve overloads through imported TypeScript aliases

Martin Vogel2a9796e0167b
diff --git a/internal/cbm/lsp/ts_lsp.c b/internal/cbm/lsp/ts_lsp.c
index 27cf50ff2..25961d06f 100644
--- a/internal/cbm/lsp/ts_lsp.c
+++ b/internal/cbm/lsp/ts_lsp.c
@@ -2231,7 +2231,8 @@ static const CBMType *ts_signature_for_call(TSLSPContext *ctx, TSNode function,
if (!registered) {
const char *module_qn = ts_import_module_for_local(ctx, name, NULL);
if (module_qn)
- registered = ts_lookup_symbol_for_call(ctx, module_qn, name, args);
+ registered = ts_lookup_symbol_for_call(ctx, module_qn,
+ ts_import_export_name(ctx, name), args);
}
if (registered)
return registered->signature;
@@ -4048,22 +4049,27 @@ void ts_lsp_add_import(TSLSPContext *ctx, const char *local_name, const char *mo
}
static void ts_lsp_add_import_alias(TSLSPContext *ctx, const char *local, const char *exported) {
- int new_count = ctx->import_alias_count + 1;
- const char **locals =
- (const char **)cbm_arena_alloc(ctx->arena, (size_t)new_count * sizeof(const char *));
- const char **exports =
- (const char **)cbm_arena_alloc(ctx->arena, (size_t)new_count * sizeof(const char *));
- if (!locals || !exports)
- return;
- for (int i = 0; i < ctx->import_alias_count; i++) {
- locals[i] = ctx->import_alias_locals[i];
- exports[i] = ctx->import_alias_exports[i];
- }
- locals[ctx->import_alias_count] = local;
- exports[ctx->import_alias_count] = exported;
- ctx->import_alias_locals = locals;
- ctx->import_alias_exports = exports;
- ctx->import_alias_count = new_count;
+ if ((size_t)ctx->import_alias_count == ctx->import_alias_capacity) {
+ /* The arena retains old arrays: grow geometrically so collecting A
+ * aliases retains O(A) pointers instead of O(A squared). */
+ size_t capacity = ctx->import_alias_capacity ? ctx->import_alias_capacity * 2 : 8;
+ const char **locals =
+ (const char **)cbm_arena_alloc(ctx->arena, capacity * sizeof(const char *));
+ const char **exports =
+ (const char **)cbm_arena_alloc(ctx->arena, capacity * sizeof(const char *));
+ if (!locals || !exports)
+ return;
+ for (int i = 0; i < ctx->import_alias_count; i++) {
+ locals[i] = ctx->import_alias_locals[i];
+ exports[i] = ctx->import_alias_exports[i];
+ }
+ ctx->import_alias_locals = locals;
+ ctx->import_alias_exports = exports;
+ ctx->import_alias_capacity = capacity;
+ }
+ ctx->import_alias_locals[ctx->import_alias_count] = local;
+ ctx->import_alias_exports[ctx->import_alias_count] = exported;
+ ctx->import_alias_count++;
}
/* Record each `import { foo as bar }` binding of the file (#2297). The import
diff --git a/internal/cbm/lsp/ts_lsp.h b/internal/cbm/lsp/ts_lsp.h
index 312ae985e..fab7fa578 100644
--- a/internal/cbm/lsp/ts_lsp.h
+++ b/internal/cbm/lsp/ts_lsp.h
@@ -39,6 +39,7 @@ typedef struct {
const char **import_alias_locals;
const char **import_alias_exports;
int import_alias_count;
+ size_t import_alias_capacity;
// File / surrounding context.
const char *module_qn; // QN of this file's module
diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c
index 2a5923236..043c38eaa 100644
--- a/tests/test_pipeline.c
+++ b/tests/test_pipeline.c
@@ -9341,8 +9341,30 @@ TEST(pipeline_ts_renamed_import_call_issue2297) {
cross_file_call_has_strategy(s, project, "callsPlain", "fooUniqueQrs", "lsp_ts_import");
bool renamed = cross_file_call_has_strategy(s, project, "callsRenamed", "fooUniqueQrs",
"lsp_import_alias");
- bool wrapper = cross_file_call_has_strategy(s, project, "buildHealthUniqueQrs",
- "buildHealthUniqueQrs", "lsp_import_alias");
+ char wrapper_qn[512];
+ char exported_qn[512];
+ snprintf(wrapper_qn, sizeof(wrapper_qn), "%s.src.wrapper.buildHealthUniqueQrs", project);
+ snprintf(exported_qn, sizeof(exported_qn), "%s.src.lib.buildHealthUniqueQrs", project);
+ cbm_node_t wrapper_node = {0};
+ cbm_node_t exported_node = {0};
+ bool wrapper = false;
+ bool recursive = false;
+ if (cbm_store_find_node_by_qn(s, project, wrapper_qn, &wrapper_node) == CBM_STORE_OK &&
+ cbm_store_find_node_by_qn(s, project, exported_qn, &exported_node) == CBM_STORE_OK) {
+ cbm_edge_t *edges = NULL;
+ int edge_count = 0;
+ cbm_store_find_edges_by_source_type(s, wrapper_node.id, "CALLS", &edges, &edge_count);
+ for (int i = 0; i < edge_count; i++) {
+ if (edges[i].target_id == exported_node.id && edges[i].properties_json &&
+ strstr(edges[i].properties_json, "lsp_import_alias"))
+ wrapper = true;
+ if (edges[i].target_id == wrapper_node.id)
+ recursive = true;
+ }
+ cbm_store_free_edges(edges, edge_count);
+ }
+ cbm_node_free_fields(&wrapper_node);
+ cbm_node_free_fields(&exported_node);
bool renamed_class = cross_file_call_has_strategy(s, project, "viaRenamedClass",
"pingUniqueQrs", "lsp_ts_method");
@@ -9353,6 +9375,7 @@ TEST(pipeline_ts_renamed_import_call_issue2297) {
ASSERT_TRUE(plain);
ASSERT_TRUE(renamed);
ASSERT_TRUE(wrapper);
+ ASSERT_FALSE(recursive);
ASSERT_TRUE(renamed_class);
PASS();
}
diff --git a/tests/test_ts_lsp.c b/tests/test_ts_lsp.c
index a5531452d..5a78fa79c 100644
--- a/tests/test_ts_lsp.c
+++ b/tests/test_ts_lsp.c
@@ -2771,6 +2771,125 @@ TEST(tslsp_crossfile_chain_through_return) {
PASS();
}
+/* The call target and its return signature must use the same exported name,
+ * even when the module also exports a function named like the local alias. */
+static int ts_renamed_import_return_chain(bool shared_registry) {
+ const char *source = "import { make as alias } from './lib';\n"
+ "function go() { alias().read(); }\n";
+ CBMLSPDef defs[] = {
+ {.qualified_name = "test.main.go",
+ .short_name = "go",
+ .label = "Function",
+ .lang = CBM_LANG_TYPESCRIPT,
+ .def_module_qn = "test.main"},
+ {.qualified_name = "test.lib.Good",
+ .short_name = "Good",
+ .label = "Class",
+ .lang = CBM_LANG_TYPESCRIPT,
+ .def_module_qn = "test.lib",
+ .method_names_str = "read"},
+ {.qualified_name = "test.lib.Good.read",
+ .short_name = "read",
+ .label = "Method",
+ .lang = CBM_LANG_TYPESCRIPT,
+ .def_module_qn = "test.lib",
+ .receiver_type = "test.lib.Good"},
+ {.qualified_name = "test.lib.Bad",
+ .short_name = "Bad",
+ .label = "Class",
+ .lang = CBM_LANG_TYPESCRIPT,
+ .def_module_qn = "test.lib",
+ .method_names_str = "read"},
+ {.qualified_name = "test.lib.Bad.read",
+ .short_name = "read",
+ .label = "Method",
+ .lang = CBM_LANG_TYPESCRIPT,
+ .def_module_qn = "test.lib",
+ .receiver_type = "test.lib.Bad"},
+ {.qualified_name = "test.lib.make",
+ .short_name = "make",
+ .label = "Function",
+ .lang = CBM_LANG_TYPESCRIPT,
+ .def_module_qn = "test.lib",
+ .return_types = "test.lib.Good"},
+ {.qualified_name = "test.lib.alias",
+ .short_name = "alias",
+ .label = "Function",
+ .lang = CBM_LANG_TYPESCRIPT,
+ .def_module_qn = "test.lib",
+ .return_types = "test.lib.Bad"},
+ };
+ enum { NDEFS = (int)(sizeof(defs) / sizeof(defs[0])) };
+ const char *imp_names[] = {"alias"};
+ const char *imp_qns[] = {"test.lib"};
+ CBMArena arena;
+ cbm_arena_init(&arena);
+ CBMResolvedCallArray out = {0};
+ if (shared_registry) {
+ CBMTypeRegistry *reg = cbm_ts_build_cross_registry(&arena, defs, NDEFS);
+ ASSERT_NOT_NULL(reg);
+ cbm_run_ts_lsp_cross_with_registry(&arena, source, (int)strlen(source), "test.main", false,
+ false, false, reg, defs, NDEFS, imp_names, imp_qns, 1,
+ NULL, &out);
+ } else {
+ cbm_run_ts_lsp_cross(&arena, source, (int)strlen(source), "test.main", false, false, false,
+ defs, NDEFS, imp_names, imp_qns, 1, NULL, &out);
+ }
+ bool direct = has_exact_resolved(&out, "test.main.go", "test.lib.make", "lsp_import_alias");
+ bool good = has_exact_resolved(&out, "test.main.go", "test.lib.Good.read", "lsp_ts_method");
+ bool bad = has_exact_resolved(&out, "test.main.go", "test.lib.Bad.read", "lsp_ts_method");
+ cbm_arena_destroy(&arena);
+ ASSERT_TRUE(direct);
+ ASSERT_FALSE(bad);
+ ASSERT_TRUE(good);
+ PASS();
+}
+
+TEST(tslsp_crossfile_renamed_import_return_chain) {
+ return ts_renamed_import_return_chain(false);
+}
+
+TEST(tslsp_crossfile_renamed_import_return_chain_shared) {
+ return ts_renamed_import_return_chain(true);
+}
+
+/* Keep the supplied import map at one entry: this measures the AST alias
+ * collector independently of the older import-map insertion path. */
+static size_t ts_import_alias_arena_bytes(int count) {
+ size_t capacity = (size_t)count * 48 + 64;
+ char *source = malloc(capacity);
+ if (!source)
+ return 0;
+ size_t len = (size_t)snprintf(source, capacity, "import { ");
+ for (int i = 0; i < count; i++) {
+ len += (size_t)snprintf(source + len, capacity - len, "%sf%d as a%d", i ? ", " : "", i, i);
+ }
+ len += (size_t)snprintf(source + len, capacity - len, " } from './lib';\n");
+ const char *imp_names[] = {"a0"};
+ const char *imp_qns[] = {"test.lib"};
+ CBMArena arena;
+ cbm_arena_init(&arena);
+ CBMResolvedCallArray out = {0};
+ cbm_run_ts_lsp_cross(&arena, source, (int)len, "test.main", false, false, false, NULL, 0,
+ imp_names, imp_qns, 1, NULL, &out);
+ size_t bytes = cbm_arena_total(&arena);
+ cbm_arena_destroy(&arena);
+ free(source);
+ return bytes;
+}
+
+TEST(tslsp_import_alias_arena_growth) {
+ size_t small = ts_import_alias_arena_bytes(1024);
+ size_t large = ts_import_alias_arena_bytes(2048);
+ ASSERT_GT(small, 0);
+ ASSERT_GT(large, small);
+ printf(" aliases arena bytes: 1024=%zu 2048=%zu ", small, large);
+ /* Linear source/AST work plus geometric storage stays below 3x; retaining
+ * two complete arrays at every insertion grows approximately 4x. */
+ ASSERT(large < small * 3);
+ PASS();
+}
+
TEST(tslsp_crossfile_no_def_match) {
/* Function that imports a symbol not in the def list — should resolve safely
* (emit unresolved, no crash). */
@@ -5236,6 +5355,9 @@ SUITE(ts_lsp) {
RUN_TEST(tslsp_scale_many_defs_no_crash_issue344);
RUN_TEST(tslsp_crossfile_function_call);
RUN_TEST(tslsp_crossfile_chain_through_return);
+ RUN_TEST(tslsp_crossfile_renamed_import_return_chain);
+ RUN_TEST(tslsp_crossfile_renamed_import_return_chain_shared);
+ RUN_TEST(tslsp_import_alias_arena_growth);
RUN_TEST(tslsp_crossfile_no_def_match);
RUN_TEST(tslsp_crossfile_renamed_import_issue2297);
RUN_TEST(tslsp_crossfile_renamed_import_missing_export_issue2297);