@@ -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