@@ -261,16 +261,20 @@ static TSNode find_first_kind(TSNode node, const char *kind) {
261261 return null_node ;
262262}
263263
264- static void ts_emit_resolved_call_at (TSLSPContext * ctx , const char * callee_qn , const char * strategy ,
265- float confidence , TSNode site ) {
264+ /* `reason` carries the name written at the site when it differs from the
265+ * callee's leaf (`bar()` through `import { foo as bar }`): the pipeline joins an
266+ * alias strategy by that name instead of by the callee QN's last segment. */
267+ static void ts_emit_resolved_call_reason_at (TSLSPContext * ctx , const char * callee_qn ,
268+ const char * strategy , float confidence ,
269+ const char * reason , TSNode site ) {
266270 if (!ctx || !ctx -> resolved_calls || !callee_qn || !ctx -> enclosing_func_qn )
267271 return ;
268272 CBMResolvedCall rc = {0 };
269273 rc .caller_qn = ctx -> enclosing_func_qn ;
270274 rc .callee_qn = callee_qn ;
271275 rc .strategy = strategy ? strategy : "lsp_ts" ;
272276 rc .confidence = confidence ;
273- rc .reason = NULL ;
277+ rc .reason = reason ;
274278 rc .kind = CBM_RESOLVED_INVOCATION ;
275279 if (!ts_node_is_null (site )) {
276280 rc .site_start_byte = ts_node_start_byte (site );
@@ -279,8 +283,14 @@ static void ts_emit_resolved_call_at(TSLSPContext *ctx, const char *callee_qn, c
279283 cbm_resolvedcall_push (ctx -> resolved_calls , ctx -> arena , rc );
280284}
281285
282- static void ts_emit_resolved_reference (TSLSPContext * ctx , const char * callee_qn ,
283- const char * strategy , float confidence , TSNode site ) {
286+ static void ts_emit_resolved_call_at (TSLSPContext * ctx , const char * callee_qn , const char * strategy ,
287+ float confidence , TSNode site ) {
288+ ts_emit_resolved_call_reason_at (ctx , callee_qn , strategy , confidence , NULL , site );
289+ }
290+
291+ static void ts_emit_resolved_reference_reason (TSLSPContext * ctx , const char * callee_qn ,
292+ const char * strategy , float confidence ,
293+ const char * reason , TSNode site ) {
284294 if (!ctx || !ctx -> resolved_calls || !callee_qn || !ctx -> enclosing_func_qn ||
285295 ts_node_is_null (site )) {
286296 return ;
@@ -290,13 +300,18 @@ static void ts_emit_resolved_reference(TSLSPContext *ctx, const char *callee_qn,
290300 rc .callee_qn = callee_qn ;
291301 rc .strategy = strategy ? strategy : "lsp_ts_value_reference" ;
292302 rc .confidence = confidence ;
293- rc .reason = NULL ;
303+ rc .reason = reason ;
294304 rc .kind = CBM_RESOLVED_CALL_REFERENCE ;
295305 rc .site_start_byte = ts_node_start_byte (site );
296306 rc .site_end_byte = ts_node_end_byte (site );
297307 cbm_resolvedcall_push (ctx -> resolved_calls , ctx -> arena , rc );
298308}
299309
310+ static void ts_emit_resolved_reference (TSLSPContext * ctx , const char * callee_qn ,
311+ const char * strategy , float confidence , TSNode site ) {
312+ ts_emit_resolved_reference_reason (ctx , callee_qn , strategy , confidence , NULL , site );
313+ }
314+
300315static void ts_emit_unresolved_call_at (TSLSPContext * ctx , const char * expr_text , const char * reason ,
301316 TSNode site ) {
302317 if (!ctx || !ctx -> resolved_calls || !ctx -> enclosing_func_qn )
@@ -1417,6 +1432,19 @@ static bool ts_scope_has_binding(const CBMScope *scope, const char *name) {
14171432 return ts_scope_binding_owner (scope , name ) != NULL ;
14181433}
14191434
1435+ /* The member an import binding names in its module: the exported name of a
1436+ * renamed named import (`foo` for `import { foo as bar }`, #2297), else the
1437+ * binding itself. Callers have already matched `local` to an import binding. */
1438+ static const char * ts_import_export_name (const TSLSPContext * ctx , const char * local ) {
1439+ if (!ctx || !local )
1440+ return local ;
1441+ for (int i = 0 ; i < ctx -> import_alias_count ; i ++ ) {
1442+ if (strcmp (ctx -> import_alias_locals [i ], local ) == 0 )
1443+ return ctx -> import_alias_exports [i ];
1444+ }
1445+ return local ;
1446+ }
1447+
14201448/* Full QN of an imported symbol. An import value is normally the module QN
14211449 * (symbol = module + "." + name), but may already be the symbol QN. When the
14221450 * module QN ends in ".name" the spelling is ambiguous: `Foo.ts` exporting
@@ -1455,8 +1483,9 @@ static const CBMRegisteredFunc *ts_exact_same_module_function(TSLSPContext *ctx,
14551483}
14561484
14571485/* Return a function only when the local import binding maps to one unambiguous
1458- * module and that module contains the exact local-name QN. Aliases whose export
1459- * name is unavailable in CBMImport deliberately fail closed and remain USAGE. */
1486+ * module and that module contains the exact QN of the member the binding names
1487+ * (the exported name for `import { foo as bar }`). A renamed import of a name
1488+ * the module does not export fails closed and remains USAGE. */
14601489static const CBMRegisteredFunc * ts_exact_imported_function (TSLSPContext * ctx , const char * name ) {
14611490 if (!ctx || !ctx -> registry || !name || ts_scope_has_binding (ctx -> current_scope , name )) {
14621491 return NULL ;
@@ -1476,7 +1505,8 @@ static const CBMRegisteredFunc *ts_exact_imported_function(TSLSPContext *ctx, co
14761505 if (!matched_module ) {
14771506 return NULL ;
14781507 }
1479- const char * full_qn = ts_import_symbol_qn (ctx , matched_module , name );
1508+ const char * full_qn =
1509+ ts_import_symbol_qn (ctx , matched_module , ts_import_export_name (ctx , name ));
14801510 return full_qn ? cbm_registry_lookup_func (ctx -> registry , full_qn ) : NULL ;
14811511}
14821512
@@ -1524,7 +1554,7 @@ static const CBMType *type_of_identifier(TSLSPContext *ctx, const char *name) {
15241554
15251555 // Construct the full symbol QN using the same convention as exact
15261556 // imported function-value resolution.
1527- const char * full_qn = ts_import_symbol_qn (ctx , mqn , name );
1557+ const char * full_qn = ts_import_symbol_qn (ctx , mqn , ts_import_export_name ( ctx , name ) );
15281558
15291559 // Try the symbol lookup precedence: registered func, then registered type.
15301560 const CBMRegisteredFunc * f = cbm_registry_lookup_func (ctx -> registry , full_qn );
@@ -2079,7 +2109,8 @@ static const CBMType *ts_new_bare_class_type(TSLSPContext *ctx, const char *cnam
20792109 bool import_matched = false;
20802110 const char * import_module = ts_import_module_for_local (ctx , cname , & import_matched );
20812111 if (import_module )
2082- return cbm_type_named (ctx -> arena , ts_import_symbol_qn (ctx , import_module , cname ));
2112+ return cbm_type_named (
2113+ ctx -> arena , ts_import_symbol_qn (ctx , import_module , ts_import_export_name (ctx , cname )));
20832114 return cbm_type_named (ctx -> arena , local_qn );
20842115}
20852116
@@ -2558,7 +2589,8 @@ static const CBMType *resolve_type_with_imports(TSLSPContext *ctx, const CBMType
25582589 continue ;
25592590 if (strcmp (lname , bare ) != 0 )
25602591 continue ;
2561- return cbm_type_named (ctx -> arena , ts_import_symbol_qn (ctx , mqn , bare ));
2592+ return cbm_type_named (ctx -> arena ,
2593+ ts_import_symbol_qn (ctx , mqn , ts_import_export_name (ctx , bare )));
25622594 }
25632595 return t ;
25642596 }
@@ -2925,12 +2957,19 @@ static void resolve_value_references_at(TSLSPContext *ctx, TSNode call_node) {
29252957 }
29262958 const CBMRegisteredFunc * target = ts_exact_same_module_function (ctx , name );
29272959 const char * strategy = "lsp_ts_same_module_value_reference" ;
2960+ const char * reason = NULL ;
29282961 if (!target ) {
29292962 target = ts_exact_imported_function (ctx , name );
29302963 strategy = "lsp_ts_import_value_reference" ;
2964+ /* `import { foo as bar }`: the site names `bar`, the target is `foo`. */
2965+ if (target && ts_import_export_name (ctx , name ) != name ) {
2966+ strategy = "lsp_callable_value_reference" ;
2967+ reason = name ;
2968+ }
29312969 }
29322970 if (target ) {
2933- ts_emit_resolved_reference (ctx , target -> qualified_name , strategy , 0.95f , arg );
2971+ ts_emit_resolved_reference_reason (ctx , target -> qualified_name , strategy , 0.95f , reason ,
2972+ arg );
29342973 }
29352974 }
29362975}
@@ -3017,14 +3056,20 @@ static void resolve_call_at(TSLSPContext *ctx, TSNode call_node) {
30173056 const char * lname = ctx -> import_local_names ? ctx -> import_local_names [i ] : NULL ;
30183057 const char * mqn = ctx -> import_module_qns ? ctx -> import_module_qns [i ] : NULL ;
30193058 if (lname && mqn && strcmp (lname , name ) == 0 ) {
3020- const CBMRegisteredFunc * f = ts_lookup_symbol_for_call (ctx , mqn , name , args );
3059+ const char * member = ts_import_export_name (ctx , name );
3060+ const CBMRegisteredFunc * f = ts_lookup_symbol_for_call (ctx , mqn , member , args );
3061+ if (f && member != name ) {
3062+ ts_emit_resolved_call_reason_at (ctx , f -> qualified_name , "lsp_import_alias" ,
3063+ 0.95f , name , call_node );
3064+ return ;
3065+ }
30213066 if (f ) {
30223067 ts_emit_resolved_call_at (ctx , f -> qualified_name , "lsp_ts_import" , 0.95f ,
30233068 call_node );
30243069 return ;
30253070 }
30263071 // Fall through to qualified_name fallback.
3027- const char * qn = cbm_arena_sprintf (ctx -> arena , "%s.%s" , mqn , name );
3072+ const char * qn = cbm_arena_sprintf (ctx -> arena , "%s.%s" , mqn , member );
30283073 ts_emit_unresolved_call_at (ctx , qn , "import_symbol_not_in_registry" , call_node );
30293074 return ;
30303075 }
@@ -3366,7 +3411,13 @@ static void resolve_jsx_element(TSLSPContext *ctx, TSNode element_node) {
33663411 * per-file row deliberately keeps the file eligible for the
33673412 * cross-file pass, where resolved import QNs and project defs are
33683413 * available. */
3369- const CBMRegisteredFunc * f = cbm_registry_lookup_symbol (ctx -> registry , mqn , tag_name );
3414+ const char * member = ts_import_export_name (ctx , tag_name );
3415+ const CBMRegisteredFunc * f = cbm_registry_lookup_symbol (ctx -> registry , mqn , member );
3416+ if (f && member != tag_name ) {
3417+ ts_emit_resolved_call_reason_at (ctx , f -> qualified_name , "lsp_import_alias" , 0.95f ,
3418+ tag_name , tag );
3419+ return ;
3420+ }
33703421 if (f ) {
33713422 ts_emit_resolved_call_at (ctx , f -> qualified_name , "lsp_ts_jsx_import" , 0.95f , tag );
33723423 return ;
@@ -3996,6 +4047,63 @@ void ts_lsp_add_import(TSLSPContext *ctx, const char *local_name, const char *mo
39964047 ctx -> import_count = new_count ;
39974048}
39984049
4050+ 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 ;
4067+ }
4068+
4069+ /* Record each `import { foo as bar }` binding of the file (#2297). The import
4070+ * map holds only local bindings, so a renamed binding spelled `<module>.bar`,
4071+ * named nothing, and every call through it lost its CALLS edge. Reading the
4072+ * file's own import statements serves the per-file and both cross-file entry
4073+ * points alike. `default as x` keeps today's local-name lookup (a default
4074+ * import binds no exported member name); string export names and identity
4075+ * aliases need no entry. Must run before the type passes, which resolve
4076+ * imported type names. */
4077+ static void ts_lsp_collect_import_aliases (TSLSPContext * ctx , TSNode root ) {
4078+ if (!ctx || !ctx -> arena || ts_node_is_null (root ) || ctx -> import_count == 0 )
4079+ return ;
4080+ uint32_t kn = 0 ;
4081+ TSNode * kids = collect_children (ctx -> arena , root , & kn );
4082+ for (uint32_t i = 0 ; i < kn ; i ++ ) {
4083+ if (!node_kind_is (kids [i ], "import_statement" ))
4084+ continue ;
4085+ TSNode named = find_first_kind (find_first_kind (kids [i ], "import_clause" ), "named_imports" );
4086+ if (ts_node_is_null (named ))
4087+ continue ;
4088+ uint32_t sn = 0 ;
4089+ TSNode * specs = collect_children (ctx -> arena , named , & sn );
4090+ for (uint32_t j = 0 ; j < sn ; j ++ ) {
4091+ if (!node_kind_is (specs [j ], "import_specifier" ))
4092+ continue ;
4093+ TSNode name = ts_node_child_by_field_name (specs [j ], "name" , TS_LSP_FIELD_LEN ("name" ));
4094+ TSNode alias =
4095+ ts_node_child_by_field_name (specs [j ], "alias" , TS_LSP_FIELD_LEN ("alias" ));
4096+ if (!node_kind_is (name , "identifier" ) || !node_kind_is (alias , "identifier" ))
4097+ continue ;
4098+ char * exported = node_text (ctx , name );
4099+ char * local = node_text (ctx , alias );
4100+ if (exported && local && strcmp (exported , "default" ) != 0 &&
4101+ strcmp (exported , local ) != 0 )
4102+ ts_lsp_add_import_alias (ctx , local , exported );
4103+ }
4104+ }
4105+ }
4106+
39994107// ── Stdlib seeds (Phase 5 will replace with generator) ────────────────────────
40004108
40014109static const CBMType * tt_builtin (CBMArena * a , const char * n ) {
@@ -5787,6 +5895,7 @@ void cbm_run_ts_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
57875895 ts_lsp_add_import (& ctx , imp -> local_name , imp -> module_path );
57885896 }
57895897 }
5898+ ts_lsp_collect_import_aliases (& ctx , root );
57905899
57915900 ast_sweep_shapes (& ctx , root , & reg );
57925901 rebuild_signatures_from_ast (& ctx , root , & reg );
@@ -6041,6 +6150,7 @@ void cbm_run_ts_lsp_cross_with_registry(CBMArena *arena, const char *source, int
60416150 ts_lsp_add_import (& ctx , import_names [i ], import_qns [i ]);
60426151 }
60436152 }
6153+ ts_lsp_collect_import_aliases (& ctx , root );
60446154
60456155 ast_sweep_shapes (& ctx , root , & overlay );
60466156 rebuild_signatures_from_ast (& ctx , root , & overlay );
@@ -6257,6 +6367,7 @@ void cbm_run_ts_lsp_cross(CBMArena *arena, const char *source, int source_len,
62576367 ts_lsp_add_import (& ctx , import_names [i ], import_qns [i ]);
62586368 }
62596369 }
6370+ ts_lsp_collect_import_aliases (& ctx , root );
62606371
62616372 ast_sweep_shapes (& ctx , root , & reg );
62626373 rebuild_signatures_from_ast (& ctx , root , & reg );
0 commit comments