@@ -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+
27742893TEST (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