Skip to content

Commit 3984795

Browse files
committed
fix(ts-lsp): type imported class receivers for cross-file method calls (#1354)
A method call on an instance of a class defined in another file produced no CALLS edge, while the identical code in the class's own file resolved via lsp_ts_method. The type-aware tier typed the receiver wrongly in two places, so the method lookup in the shared TS registry missed and the call fell to method_not_in_registry: 1. `new C()` always qualified a bare constructor name against the CURRENT module. `const t = new ImportedClass(); t.m()` and `new ImportedClass().m()` were typed as a phantom `<this module>.ImportedClass`. The constructor type now follows TS scoping: a class declared in this module, else the class an unambiguous import binding names, else the old spelling. 2. ts_import_symbol_qn (and the copy of its heuristic in resolve_type_with_imports) treated an import value ending in ".<name>" as an already-qualified symbol QN. For the dominant TS convention of a file named after its class (`Foo.ts` exports `class Foo`, module `x.Foo`, class `x.Foo.Foo`), the receiver was typed as the MODULE, for `new`, annotations and parameters alike. The registry now decides the ambiguous case: a registered `module.name` wins, else the value is the symbol QN as before. Both are resolved through the existing shared cross-file registry; no name-based fallback is reintroduced. Cost is one or two hashed lookups on the finalized overlay / sealed shared registry, per `new` expression or per ambiguous import spelling during the walk; nothing is looked up during registration and nothing is written to the shared registry. typeorm (3,593 .ts files, shallow f279fd13): lsp_ts_method 2,551 -> 3,130, all 579 new edges cross-file (cross-file lsp_ts_method 696 -> 1,275), 0 edges lost, other strategies unchanged, index time unchanged. 20 sampled new edges checked against source: 20 true positives. Signed-off-by: Martin Vogel <[email protected]>
1 parent 0f52d30 commit 3984795

3 files changed

Lines changed: 285 additions & 16 deletions

File tree

‎internal/cbm/lsp/ts_lsp.c‎

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,17 +1406,30 @@ static bool ts_scope_has_binding(const CBMScope *scope, const char *name) {
14061406
return ts_scope_binding_owner(scope, name) != NULL;
14071407
}
14081408

1409+
/* Full QN of an imported symbol. An import value is normally the module QN
1410+
* (symbol = module + "." + name), but may already be the symbol QN. When the
1411+
* module QN ends in ".name" the spelling is ambiguous: `Foo.ts` exporting
1412+
* `Foo` (module `x.Foo`, symbol `x.Foo.Foo`) looks exactly like an
1413+
* already-qualified value. Treating it as the symbol typed every imported
1414+
* same-name class as its module (#1354), so the registry decides: a
1415+
* registered `module.name` wins, else the value is the symbol. Two hashed
1416+
* lookups on a finalized/sealed registry, only in the ambiguous case. */
14091417
static const char *ts_import_symbol_qn(TSLSPContext *ctx, const char *module_qn, const char *name) {
14101418
if (!ctx || !module_qn || !name) {
14111419
return NULL;
14121420
}
1421+
const char *appended = cbm_arena_sprintf(ctx->arena, "%s.%s", module_qn, name);
14131422
size_t module_len = strlen(module_qn);
14141423
size_t name_len = strlen(name);
14151424
if (module_len > name_len + 1 && module_qn[module_len - name_len - 1] == '.' &&
14161425
strcmp(module_qn + module_len - name_len, name) == 0) {
1426+
if (appended && (cbm_registry_lookup_type(ctx->registry, appended) ||
1427+
cbm_registry_lookup_func(ctx->registry, appended))) {
1428+
return appended;
1429+
}
14171430
return module_qn;
14181431
}
1419-
return cbm_arena_sprintf(ctx->arena, "%s.%s", module_qn, name);
1432+
return appended;
14201433
}
14211434

14221435
/* Same-file function values use only the exact current-module QN. No short-name,
@@ -1965,6 +1978,26 @@ static const char *ts_import_module_for_local(const TSLSPContext *ctx, const cha
19651978
return matched;
19661979
}
19671980

1981+
/* Type of `new C()` for a bare class name, in TS scoping order: a class
1982+
* declared in this module, else the class an unambiguous import binding
1983+
* names, else the historical current-module spelling. Without the import
1984+
* step (#1354) `new ImportedClass()` was typed as a phantom current-module
1985+
* class, so every method call on the instance missed the registry and the
1986+
* cross-file edge was lost while the same code in the class's own file
1987+
* resolved. One hashed type lookup; the registry is only read. */
1988+
static const CBMType *ts_new_bare_class_type(TSLSPContext *ctx, const char *cname) {
1989+
if (!ctx->module_qn)
1990+
return cbm_type_named(ctx->arena, cname);
1991+
const char *local_qn = cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->module_qn, cname);
1992+
if (cbm_registry_lookup_type(ctx->registry, local_qn))
1993+
return cbm_type_named(ctx->arena, local_qn);
1994+
bool import_matched = false;
1995+
const char *import_module = ts_import_module_for_local(ctx, cname, &import_matched);
1996+
if (import_module)
1997+
return cbm_type_named(ctx->arena, ts_import_symbol_qn(ctx, import_module, cname));
1998+
return cbm_type_named(ctx->arena, local_qn);
1999+
}
2000+
19682001
static const CBMRegisteredFunc *ts_lookup_namespace_call(TSLSPContext *ctx, const char *object_name,
19692002
const char *method_name, TSNode args,
19702003
bool *out_is_import) {
@@ -2195,13 +2228,8 @@ const CBMType *ts_eval_expr_type(TSLSPContext *ctx, TSNode node) {
21952228
if (!ts_node_is_null(ctor)) {
21962229
char *cname = node_text(ctx, ctor);
21972230
if (cname) {
2198-
// Bare class name → qualify against module.
2199-
if (strchr(cname, '.') == NULL && ctx->module_qn) {
2200-
const char *qn = cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->module_qn, cname);
2201-
result = cbm_type_named(ctx->arena, qn);
2202-
} else {
2203-
result = cbm_type_named(ctx->arena, cname);
2204-
}
2231+
result = strchr(cname, '.') == NULL ? ts_new_bare_class_type(ctx, cname)
2232+
: cbm_type_named(ctx->arena, cname);
22052233
}
22062234
}
22072235
} else if (strcmp(kind, "call_expression") == 0) {
@@ -2435,14 +2463,7 @@ static const CBMType *resolve_type_with_imports(TSLSPContext *ctx, const CBMType
24352463
continue;
24362464
if (strcmp(lname, bare) != 0)
24372465
continue;
2438-
// Heuristic: if mqn already ends in ".bare" use as-is; otherwise append.
2439-
size_t mqn_len = strlen(mqn);
2440-
size_t bare_len = strlen(bare);
2441-
if (mqn_len > bare_len + 1 && mqn[mqn_len - bare_len - 1] == '.' &&
2442-
strcmp(mqn + mqn_len - bare_len, bare) == 0) {
2443-
return cbm_type_named(ctx->arena, mqn);
2444-
}
2445-
return cbm_type_named(ctx->arena, cbm_arena_sprintf(ctx->arena, "%s.%s", mqn, bare));
2466+
return cbm_type_named(ctx->arena, ts_import_symbol_qn(ctx, mqn, bare));
24462467
}
24472468
return t;
24482469
}

‎tests/test_pipeline.c‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6118,6 +6118,93 @@ TEST(pipeline_axios_wrapper_baseurl_composes_http_calls_issue1916) {
61186118
PASS();
61196119
}
61206120

6121+
/* Issue #1354: a method call on an instance built with `new ImportedClass()`
6122+
* produced no CALLS edge (only the constructor edge), while the identical
6123+
* shape in the class's own file resolved via lsp_ts_method. End to end:
6124+
* both cross-file shapes must now carry the type-aware lsp_ts_method edge,
6125+
* and the same-file control must keep it. */
6126+
TEST(pipeline_ts_crossfile_new_instance_method_call_issue1354) {
6127+
char tmp[256];
6128+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_ts_1354_XXXXXX");
6129+
if (!cbm_mkdtemp(tmp)) {
6130+
FAIL("tmpdir");
6131+
}
6132+
6133+
/* write_temp_file creates one directory level only. */
6134+
write_temp_file(tmp, "lib/toast.service.ts",
6135+
"export class ToastService {\n"
6136+
" openSuccessUniqueXyz(msg: string): void {\n"
6137+
" console.log(msg);\n"
6138+
" }\n"
6139+
"}\n"
6140+
"\n"
6141+
"export function sameFileControl(): void {\n"
6142+
" const t = new ToastService();\n"
6143+
" t.openSuccessUniqueXyz('same-file control');\n"
6144+
"}\n");
6145+
write_temp_file(tmp, "app/variants.ts",
6146+
"import { ToastService } from '../lib/toast.service';\n"
6147+
"\n"
6148+
"export function crossNewLocal(): void {\n"
6149+
" const t = new ToastService();\n"
6150+
" t.openSuccessUniqueXyz('cross-file');\n"
6151+
"}\n"
6152+
"\n"
6153+
"export function crossNewChain(): void {\n"
6154+
" new ToastService().openSuccessUniqueXyz('chained');\n"
6155+
"}\n");
6156+
/* File named after its class (`NotifierService.ts` exports
6157+
* `NotifierService`): the imported module QN already ends in the class
6158+
* name, which must not be mistaken for the class QN. */
6159+
write_temp_file(tmp, "lib/NotifierService.ts",
6160+
"export class NotifierService {\n"
6161+
" notifyUniqueAbc(): void {}\n"
6162+
"}\n");
6163+
write_temp_file(tmp, "app/notify.ts",
6164+
"import { NotifierService } from '../lib/NotifierService';\n"
6165+
"\n"
6166+
"export function crossSameNameNew(): void {\n"
6167+
" const n = new NotifierService();\n"
6168+
" n.notifyUniqueAbc();\n"
6169+
"}\n"
6170+
"\n"
6171+
"export function crossSameNameTyped(n: NotifierService): void {\n"
6172+
" n.notifyUniqueAbc();\n"
6173+
"}\n");
6174+
6175+
char db_path[512];
6176+
snprintf(db_path, sizeof(db_path), "%s/ts_1354.db", tmp);
6177+
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
6178+
ASSERT_NOT_NULL(p);
6179+
ASSERT_EQ(cbm_pipeline_run(p), 0);
6180+
const char *project = cbm_pipeline_project_name(p);
6181+
6182+
cbm_store_t *s = cbm_store_open_path(db_path);
6183+
ASSERT_NOT_NULL(s);
6184+
6185+
bool same_file = cross_file_call_has_strategy(s, project, "sameFileControl",
6186+
"openSuccessUniqueXyz", "lsp_ts_method");
6187+
bool cross_local = cross_file_call_has_strategy(s, project, "crossNewLocal",
6188+
"openSuccessUniqueXyz", "lsp_ts_method");
6189+
bool cross_chain = cross_file_call_has_strategy(s, project, "crossNewChain",
6190+
"openSuccessUniqueXyz", "lsp_ts_method");
6191+
bool same_name_new = cross_file_call_has_strategy(s, project, "crossSameNameNew",
6192+
"notifyUniqueAbc", "lsp_ts_method");
6193+
bool same_name_typed = cross_file_call_has_strategy(s, project, "crossSameNameTyped",
6194+
"notifyUniqueAbc", "lsp_ts_method");
6195+
6196+
cbm_store_close(s);
6197+
cbm_pipeline_free(p);
6198+
th_rmtree(tmp);
6199+
6200+
ASSERT_TRUE(same_file);
6201+
ASSERT_TRUE(cross_local);
6202+
ASSERT_TRUE(cross_chain);
6203+
ASSERT_TRUE(same_name_new);
6204+
ASSERT_TRUE(same_name_typed);
6205+
PASS();
6206+
}
6207+
61216208
TEST(pipeline_tsjs_receiver_suppresses_weak_method_edge) {
61226209
char tmp[256];
61236210
snprintf(tmp, sizeof(tmp), "/tmp/cbm_tsjs_recv_XXXXXX");
@@ -16255,6 +16342,7 @@ SUITE(pipeline) {
1625516342
#endif
1625616343
RUN_TEST(pipeline_tsjs_receiver_suppresses_weak_method_edge);
1625716344
RUN_TEST(pipeline_axios_wrapper_baseurl_composes_http_calls_issue1916);
16345+
RUN_TEST(pipeline_ts_crossfile_new_instance_method_call_issue1354);
1625816346
RUN_TEST(pipeline_python_receiver_suppresses_weak_method_edge);
1625916347
RUN_TEST(pipeline_python_receiver_keeps_specific_unique_name_member_call);
1626016348
RUN_TEST(pipeline_html_embedded_member_call_stays_unbound);

‎tests/test_ts_lsp.c‎

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,164 @@ TEST(tslsp_crossfile_method_dispatch) {
20882088
PASS();
20892089
}
20902090

2091+
/* Exact resolved-call probe: caller QN, callee QN and strategy all match. */
2092+
static bool has_exact_resolved(const CBMResolvedCallArray *arr, const char *caller_qn,
2093+
const char *callee_qn, const char *strategy) {
2094+
for (int i = 0; i < arr->count; i++) {
2095+
const CBMResolvedCall *rc = &arr->items[i];
2096+
if (rc->confidence > 0 && rc->caller_qn && rc->callee_qn && rc->strategy &&
2097+
strcmp(rc->caller_qn, caller_qn) == 0 && strcmp(rc->callee_qn, callee_qn) == 0 &&
2098+
strcmp(rc->strategy, strategy) == 0)
2099+
return true;
2100+
}
2101+
return false;
2102+
}
2103+
2104+
/* Issue #1354: `new ImportedClass()` typed the instance as a phantom
2105+
* CURRENT-module class (`test.main.Conn`), so every method call on it missed
2106+
* the shared registry and fell to method_not_in_registry. Driven through the
2107+
* production Tier-2 path (shared sealed registry + per-file overlay). A local
2108+
* class with the imported name must still shadow the import. */
2109+
TEST(tslsp_crossfile_new_expression_receiver_issue1354) {
2110+
CBMLSPDef defs[] = {
2111+
{.qualified_name = "test.conn.Conn",
2112+
.short_name = "Conn",
2113+
.label = "Class",
2114+
.lang = CBM_LANG_TYPESCRIPT,
2115+
.def_module_qn = "test.conn",
2116+
.method_names_str = "ping"},
2117+
{.qualified_name = "test.conn.Conn.ping",
2118+
.short_name = "ping",
2119+
.label = "Method",
2120+
.lang = CBM_LANG_TYPESCRIPT,
2121+
.def_module_qn = "test.conn",
2122+
.receiver_type = "test.conn.Conn"},
2123+
{.qualified_name = "test.main.viaLocal",
2124+
.short_name = "viaLocal",
2125+
.label = "Function",
2126+
.lang = CBM_LANG_TYPESCRIPT,
2127+
.def_module_qn = "test.main"},
2128+
{.qualified_name = "test.main.viaChain",
2129+
.short_name = "viaChain",
2130+
.label = "Function",
2131+
.lang = CBM_LANG_TYPESCRIPT,
2132+
.def_module_qn = "test.main"},
2133+
{.qualified_name = "test.shadow.Conn",
2134+
.short_name = "Conn",
2135+
.label = "Class",
2136+
.lang = CBM_LANG_TYPESCRIPT,
2137+
.def_module_qn = "test.shadow",
2138+
.method_names_str = "ping"},
2139+
{.qualified_name = "test.shadow.Conn.ping",
2140+
.short_name = "ping",
2141+
.label = "Method",
2142+
.lang = CBM_LANG_TYPESCRIPT,
2143+
.def_module_qn = "test.shadow",
2144+
.receiver_type = "test.shadow.Conn"},
2145+
{.qualified_name = "test.shadow.viaShadow",
2146+
.short_name = "viaShadow",
2147+
.label = "Function",
2148+
.lang = CBM_LANG_TYPESCRIPT,
2149+
.def_module_qn = "test.shadow"},
2150+
};
2151+
enum { NDEFS = (int)(sizeof(defs) / sizeof(defs[0])) };
2152+
const char *imp_names[] = {"Conn"};
2153+
const char *imp_qns[] = {"test.conn"};
2154+
2155+
CBMArena arena;
2156+
cbm_arena_init(&arena);
2157+
CBMTypeRegistry *reg = cbm_ts_build_cross_registry(&arena, defs, NDEFS);
2158+
ASSERT_NOT_NULL(reg);
2159+
2160+
const char *main_src = "import { Conn } from './conn';\n"
2161+
"export function viaLocal() { const c = new Conn(); c.ping(); }\n"
2162+
"export function viaChain() { new Conn().ping(); }\n";
2163+
CBMResolvedCallArray out = {0};
2164+
cbm_run_ts_lsp_cross_with_registry(&arena, main_src, (int)strlen(main_src), "test.main", false,
2165+
false, false, reg, defs, NDEFS, imp_names, imp_qns, 1, NULL,
2166+
&out);
2167+
ASSERT_TRUE(
2168+
has_exact_resolved(&out, "test.main.viaLocal", "test.conn.Conn.ping", "lsp_ts_method"));
2169+
ASSERT_TRUE(
2170+
has_exact_resolved(&out, "test.main.viaChain", "test.conn.Conn.ping", "lsp_ts_method"));
2171+
2172+
/* Shadow control: the file declares its own Conn and imports nothing. */
2173+
const char *shadow_src = "export class Conn { ping() {} }\n"
2174+
"export function viaShadow() { const c = new Conn(); c.ping(); }\n";
2175+
CBMResolvedCallArray out2 = {0};
2176+
cbm_run_ts_lsp_cross_with_registry(&arena, shadow_src, (int)strlen(shadow_src), "test.shadow",
2177+
false, false, false, reg, defs, NDEFS, NULL, NULL, 0, NULL,
2178+
&out2);
2179+
ASSERT_TRUE(has_exact_resolved(&out2, "test.shadow.viaShadow", "test.shadow.Conn.ping",
2180+
"lsp_ts_method"));
2181+
ASSERT_FALSE(
2182+
has_exact_resolved(&out2, "test.shadow.viaShadow", "test.conn.Conn.ping", "lsp_ts_method"));
2183+
2184+
cbm_arena_destroy(&arena);
2185+
PASS();
2186+
}
2187+
2188+
/* Issue #1354, file named after its class: `Conn.ts` exports `class Conn`, so
2189+
* the imported module QN `test.Conn` already ends in `.Conn`. The spelling was
2190+
* read as an already-qualified symbol QN and the receiver typed as the MODULE,
2191+
* losing every method call on imported same-name classes (the dominant TS file
2192+
* convention). The registry must decide; an import value that really is the
2193+
* symbol QN (`test.conn.Conn`, no `test.conn.Conn.Conn`) keeps resolving. */
2194+
TEST(tslsp_crossfile_same_name_file_receiver_issue1354) {
2195+
CBMLSPDef defs[] = {
2196+
{.qualified_name = "test.Conn.Conn",
2197+
.short_name = "Conn",
2198+
.label = "Class",
2199+
.lang = CBM_LANG_TYPESCRIPT,
2200+
.def_module_qn = "test.Conn",
2201+
.method_names_str = "ping"},
2202+
{.qualified_name = "test.Conn.Conn.ping",
2203+
.short_name = "ping",
2204+
.label = "Method",
2205+
.lang = CBM_LANG_TYPESCRIPT,
2206+
.def_module_qn = "test.Conn",
2207+
.receiver_type = "test.Conn.Conn"},
2208+
{.qualified_name = "test.conn.Link",
2209+
.short_name = "Link",
2210+
.label = "Class",
2211+
.lang = CBM_LANG_TYPESCRIPT,
2212+
.def_module_qn = "test.conn",
2213+
.method_names_str = "open"},
2214+
{.qualified_name = "test.conn.Link.open",
2215+
.short_name = "open",
2216+
.label = "Method",
2217+
.lang = CBM_LANG_TYPESCRIPT,
2218+
.def_module_qn = "test.conn",
2219+
.receiver_type = "test.conn.Link"},
2220+
};
2221+
enum { NDEFS = (int)(sizeof(defs) / sizeof(defs[0])) };
2222+
const char *imp_names[] = {"Conn", "Link"};
2223+
const char *imp_qns[] = {"test.Conn", "test.conn.Link"};
2224+
2225+
CBMArena arena;
2226+
cbm_arena_init(&arena);
2227+
CBMTypeRegistry *reg = cbm_ts_build_cross_registry(&arena, defs, NDEFS);
2228+
ASSERT_NOT_NULL(reg);
2229+
2230+
const char *src = "import { Conn } from './Conn';\n"
2231+
"import { Link } from './conn';\n"
2232+
"export function viaNew() { const c = new Conn(); c.ping(); }\n"
2233+
"export function viaTyped(c: Conn) { c.ping(); }\n"
2234+
"export function viaQualified(l: Link) { l.open(); }\n";
2235+
CBMResolvedCallArray out = {0};
2236+
cbm_run_ts_lsp_cross_with_registry(&arena, src, (int)strlen(src), "test.main", false, false,
2237+
false, reg, defs, NDEFS, imp_names, imp_qns, 2, NULL, &out);
2238+
ASSERT_TRUE(
2239+
has_exact_resolved(&out, "test.main.viaNew", "test.Conn.Conn.ping", "lsp_ts_method"));
2240+
ASSERT_TRUE(
2241+
has_exact_resolved(&out, "test.main.viaTyped", "test.Conn.Conn.ping", "lsp_ts_method"));
2242+
ASSERT_TRUE(
2243+
has_exact_resolved(&out, "test.main.viaQualified", "test.conn.Link.open", "lsp_ts_method"));
2244+
2245+
cbm_arena_destroy(&arena);
2246+
PASS();
2247+
}
2248+
20912249
/* Issue #344 / #340: SIGSEGV in the TS cross-file LSP pass when the collected
20922250
* def array exceeded ~1189 entries — scale-dependent, not file-specific. Drive
20932251
* the resolver with a def array far past that threshold; the v0.7.0 rewrite
@@ -4519,6 +4677,8 @@ SUITE(ts_lsp) {
45194677

45204678
/* Cross-file resolution */
45214679
RUN_TEST(tslsp_crossfile_method_dispatch);
4680+
RUN_TEST(tslsp_crossfile_new_expression_receiver_issue1354);
4681+
RUN_TEST(tslsp_crossfile_same_name_file_receiver_issue1354);
45224682
RUN_TEST(tslsp_scale_many_defs_no_crash_issue344);
45234683
RUN_TEST(tslsp_crossfile_function_call);
45244684
RUN_TEST(tslsp_crossfile_chain_through_return);

0 commit comments

Comments
 (0)