Skip to content

Commit f1efbf6

Browse files
authored
Merge pull request DeusData#2746 from gaurav0107/fix/2297-typescript-calls-through-a-renamed-named
fix(ts-lsp): resolve calls through renamed named imports
2 parents 0002acd + 5d374a7 commit f1efbf6

4 files changed

Lines changed: 492 additions & 17 deletions

File tree

‎internal/cbm/lsp/ts_lsp.c‎

Lines changed: 134 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -269,16 +269,20 @@ static TSNode find_first_kind(TSNode node, const char *kind) {
269269
return null_node;
270270
}
271271

272-
static void ts_emit_resolved_call_at(TSLSPContext *ctx, const char *callee_qn, const char *strategy,
273-
float confidence, TSNode site) {
272+
/* `reason` carries the name written at the site when it differs from the
273+
* callee's leaf (`bar()` through `import { foo as bar }`): the pipeline joins an
274+
* alias strategy by that name instead of by the callee QN's last segment. */
275+
static void ts_emit_resolved_call_reason_at(TSLSPContext *ctx, const char *callee_qn,
276+
const char *strategy, float confidence,
277+
const char *reason, TSNode site) {
274278
if (!ctx || !ctx->resolved_calls || !callee_qn || !ctx->enclosing_func_qn)
275279
return;
276280
CBMResolvedCall rc = {0};
277281
rc.caller_qn = ctx->enclosing_func_qn;
278282
rc.callee_qn = callee_qn;
279283
rc.strategy = strategy ? strategy : "lsp_ts";
280284
rc.confidence = confidence;
281-
rc.reason = NULL;
285+
rc.reason = reason;
282286
rc.kind = CBM_RESOLVED_INVOCATION;
283287
if (!ts_node_is_null(site)) {
284288
rc.site_start_byte = ts_node_start_byte(site);
@@ -287,8 +291,14 @@ static void ts_emit_resolved_call_at(TSLSPContext *ctx, const char *callee_qn, c
287291
cbm_resolvedcall_push(ctx->resolved_calls, ctx->arena, rc);
288292
}
289293

290-
static void ts_emit_resolved_reference(TSLSPContext *ctx, const char *callee_qn,
291-
const char *strategy, float confidence, TSNode site) {
294+
static void ts_emit_resolved_call_at(TSLSPContext *ctx, const char *callee_qn, const char *strategy,
295+
float confidence, TSNode site) {
296+
ts_emit_resolved_call_reason_at(ctx, callee_qn, strategy, confidence, NULL, site);
297+
}
298+
299+
static void ts_emit_resolved_reference_reason(TSLSPContext *ctx, const char *callee_qn,
300+
const char *strategy, float confidence,
301+
const char *reason, TSNode site) {
292302
if (!ctx || !ctx->resolved_calls || !callee_qn || !ctx->enclosing_func_qn ||
293303
ts_node_is_null(site)) {
294304
return;
@@ -298,13 +308,18 @@ static void ts_emit_resolved_reference(TSLSPContext *ctx, const char *callee_qn,
298308
rc.callee_qn = callee_qn;
299309
rc.strategy = strategy ? strategy : "lsp_ts_value_reference";
300310
rc.confidence = confidence;
301-
rc.reason = NULL;
311+
rc.reason = reason;
302312
rc.kind = CBM_RESOLVED_CALL_REFERENCE;
303313
rc.site_start_byte = ts_node_start_byte(site);
304314
rc.site_end_byte = ts_node_end_byte(site);
305315
cbm_resolvedcall_push(ctx->resolved_calls, ctx->arena, rc);
306316
}
307317

318+
static void ts_emit_resolved_reference(TSLSPContext *ctx, const char *callee_qn,
319+
const char *strategy, float confidence, TSNode site) {
320+
ts_emit_resolved_reference_reason(ctx, callee_qn, strategy, confidence, NULL, site);
321+
}
322+
308323
static void ts_emit_unresolved_call_at(TSLSPContext *ctx, const char *expr_text, const char *reason,
309324
TSNode site) {
310325
if (!ctx || !ctx->resolved_calls || !ctx->enclosing_func_qn)
@@ -1425,6 +1440,19 @@ static bool ts_scope_has_binding(const CBMScope *scope, const char *name) {
14251440
return ts_scope_binding_owner(scope, name) != NULL;
14261441
}
14271442

1443+
/* The member an import binding names in its module: the exported name of a
1444+
* renamed named import (`foo` for `import { foo as bar }`, #2297), else the
1445+
* binding itself. Callers have already matched `local` to an import binding. */
1446+
static const char *ts_import_export_name(const TSLSPContext *ctx, const char *local) {
1447+
if (!ctx || !local)
1448+
return local;
1449+
for (int i = 0; i < ctx->import_alias_count; i++) {
1450+
if (strcmp(ctx->import_alias_locals[i], local) == 0)
1451+
return ctx->import_alias_exports[i];
1452+
}
1453+
return local;
1454+
}
1455+
14281456
/* Full QN of an imported symbol. An import value is normally the module QN
14291457
* (symbol = module + "." + name), but may already be the symbol QN. When the
14301458
* module QN ends in ".name" the spelling is ambiguous: `Foo.ts` exporting
@@ -1463,8 +1491,9 @@ static const CBMRegisteredFunc *ts_exact_same_module_function(TSLSPContext *ctx,
14631491
}
14641492

14651493
/* Return a function only when the local import binding maps to one unambiguous
1466-
* module and that module contains the exact local-name QN. Aliases whose export
1467-
* name is unavailable in CBMImport deliberately fail closed and remain USAGE. */
1494+
* module and that module contains the exact QN of the member the binding names
1495+
* (the exported name for `import { foo as bar }`). A renamed import of a name
1496+
* the module does not export fails closed and remains USAGE. */
14681497
static const CBMRegisteredFunc *ts_exact_imported_function(TSLSPContext *ctx, const char *name) {
14691498
if (!ctx || !ctx->registry || !name || ts_scope_has_binding(ctx->current_scope, name)) {
14701499
return NULL;
@@ -1484,7 +1513,8 @@ static const CBMRegisteredFunc *ts_exact_imported_function(TSLSPContext *ctx, co
14841513
if (!matched_module) {
14851514
return NULL;
14861515
}
1487-
const char *full_qn = ts_import_symbol_qn(ctx, matched_module, name);
1516+
const char *full_qn =
1517+
ts_import_symbol_qn(ctx, matched_module, ts_import_export_name(ctx, name));
14881518
return full_qn ? cbm_registry_lookup_func(ctx->registry, full_qn) : NULL;
14891519
}
14901520

@@ -1532,7 +1562,7 @@ static const CBMType *type_of_identifier(TSLSPContext *ctx, const char *name) {
15321562

15331563
// Construct the full symbol QN using the same convention as exact
15341564
// imported function-value resolution.
1535-
const char *full_qn = ts_import_symbol_qn(ctx, mqn, name);
1565+
const char *full_qn = ts_import_symbol_qn(ctx, mqn, ts_import_export_name(ctx, name));
15361566

15371567
// Try the symbol lookup precedence: registered func, then registered type.
15381568
const CBMRegisteredFunc *f = cbm_registry_lookup_func(ctx->registry, full_qn);
@@ -2087,7 +2117,8 @@ static const CBMType *ts_new_bare_class_type(TSLSPContext *ctx, const char *cnam
20872117
bool import_matched = false;
20882118
const char *import_module = ts_import_module_for_local(ctx, cname, &import_matched);
20892119
if (import_module)
2090-
return cbm_type_named(ctx->arena, ts_import_symbol_qn(ctx, import_module, cname));
2120+
return cbm_type_named(
2121+
ctx->arena, ts_import_symbol_qn(ctx, import_module, ts_import_export_name(ctx, cname)));
20912122
return cbm_type_named(ctx->arena, local_qn);
20922123
}
20932124

@@ -2208,7 +2239,8 @@ static const CBMType *ts_signature_for_call(TSLSPContext *ctx, TSNode function,
22082239
if (!registered) {
22092240
const char *module_qn = ts_import_module_for_local(ctx, name, NULL);
22102241
if (module_qn)
2211-
registered = ts_lookup_symbol_for_call(ctx, module_qn, name, args);
2242+
registered = ts_lookup_symbol_for_call(ctx, module_qn,
2243+
ts_import_export_name(ctx, name), args);
22122244
}
22132245
if (registered)
22142246
return registered->signature;
@@ -2566,7 +2598,8 @@ static const CBMType *resolve_type_with_imports(TSLSPContext *ctx, const CBMType
25662598
continue;
25672599
if (strcmp(lname, bare) != 0)
25682600
continue;
2569-
return cbm_type_named(ctx->arena, ts_import_symbol_qn(ctx, mqn, bare));
2601+
return cbm_type_named(ctx->arena,
2602+
ts_import_symbol_qn(ctx, mqn, ts_import_export_name(ctx, bare)));
25702603
}
25712604
return t;
25722605
}
@@ -2933,12 +2966,19 @@ static void resolve_value_references_at(TSLSPContext *ctx, TSNode call_node) {
29332966
}
29342967
const CBMRegisteredFunc *target = ts_exact_same_module_function(ctx, name);
29352968
const char *strategy = "lsp_ts_same_module_value_reference";
2969+
const char *reason = NULL;
29362970
if (!target) {
29372971
target = ts_exact_imported_function(ctx, name);
29382972
strategy = "lsp_ts_import_value_reference";
2973+
/* `import { foo as bar }`: the site names `bar`, the target is `foo`. */
2974+
if (target && ts_import_export_name(ctx, name) != name) {
2975+
strategy = "lsp_callable_value_reference";
2976+
reason = name;
2977+
}
29392978
}
29402979
if (target) {
2941-
ts_emit_resolved_reference(ctx, target->qualified_name, strategy, 0.95f, arg);
2980+
ts_emit_resolved_reference_reason(ctx, target->qualified_name, strategy, 0.95f, reason,
2981+
arg);
29422982
}
29432983
}
29442984
}
@@ -3025,14 +3065,20 @@ static void resolve_call_at(TSLSPContext *ctx, TSNode call_node) {
30253065
const char *lname = ctx->import_local_names ? ctx->import_local_names[i] : NULL;
30263066
const char *mqn = ctx->import_module_qns ? ctx->import_module_qns[i] : NULL;
30273067
if (lname && mqn && strcmp(lname, name) == 0) {
3028-
const CBMRegisteredFunc *f = ts_lookup_symbol_for_call(ctx, mqn, name, args);
3068+
const char *member = ts_import_export_name(ctx, name);
3069+
const CBMRegisteredFunc *f = ts_lookup_symbol_for_call(ctx, mqn, member, args);
3070+
if (f && member != name) {
3071+
ts_emit_resolved_call_reason_at(ctx, f->qualified_name, "lsp_import_alias",
3072+
0.95f, name, call_node);
3073+
return;
3074+
}
30293075
if (f) {
30303076
ts_emit_resolved_call_at(ctx, f->qualified_name, "lsp_ts_import", 0.95f,
30313077
call_node);
30323078
return;
30333079
}
30343080
// Fall through to qualified_name fallback.
3035-
const char *qn = cbm_arena_sprintf(ctx->arena, "%s.%s", mqn, name);
3081+
const char *qn = cbm_arena_sprintf(ctx->arena, "%s.%s", mqn, member);
30363082
ts_emit_unresolved_call_at(ctx, qn, "import_symbol_not_in_registry", call_node);
30373083
return;
30383084
}
@@ -3374,7 +3420,13 @@ static void resolve_jsx_element(TSLSPContext *ctx, TSNode element_node) {
33743420
* per-file row deliberately keeps the file eligible for the
33753421
* cross-file pass, where resolved import QNs and project defs are
33763422
* available. */
3377-
const CBMRegisteredFunc *f = cbm_registry_lookup_symbol(ctx->registry, mqn, tag_name);
3423+
const char *member = ts_import_export_name(ctx, tag_name);
3424+
const CBMRegisteredFunc *f = cbm_registry_lookup_symbol(ctx->registry, mqn, member);
3425+
if (f && member != tag_name) {
3426+
ts_emit_resolved_call_reason_at(ctx, f->qualified_name, "lsp_import_alias", 0.95f,
3427+
tag_name, tag);
3428+
return;
3429+
}
33783430
if (f) {
33793431
ts_emit_resolved_call_at(ctx, f->qualified_name, "lsp_ts_jsx_import", 0.95f, tag);
33803432
return;
@@ -4005,6 +4057,68 @@ void ts_lsp_add_import(TSLSPContext *ctx, const char *local_name, const char *mo
40054057
ctx->import_count = new_count;
40064058
}
40074059

4060+
static void ts_lsp_add_import_alias(TSLSPContext *ctx, const char *local, const char *exported) {
4061+
if ((size_t)ctx->import_alias_count == ctx->import_alias_capacity) {
4062+
/* The arena retains old arrays: grow geometrically so collecting A
4063+
* aliases retains O(A) pointers instead of O(A squared). */
4064+
size_t capacity = ctx->import_alias_capacity ? ctx->import_alias_capacity * 2 : 8;
4065+
const char **locals =
4066+
(const char **)cbm_arena_alloc(ctx->arena, capacity * sizeof(const char *));
4067+
const char **exports =
4068+
(const char **)cbm_arena_alloc(ctx->arena, capacity * sizeof(const char *));
4069+
if (!locals || !exports)
4070+
return;
4071+
for (int i = 0; i < ctx->import_alias_count; i++) {
4072+
locals[i] = ctx->import_alias_locals[i];
4073+
exports[i] = ctx->import_alias_exports[i];
4074+
}
4075+
ctx->import_alias_locals = locals;
4076+
ctx->import_alias_exports = exports;
4077+
ctx->import_alias_capacity = capacity;
4078+
}
4079+
ctx->import_alias_locals[ctx->import_alias_count] = local;
4080+
ctx->import_alias_exports[ctx->import_alias_count] = exported;
4081+
ctx->import_alias_count++;
4082+
}
4083+
4084+
/* Record each `import { foo as bar }` binding of the file (#2297). The import
4085+
* map holds only local bindings, so a renamed binding spelled `<module>.bar`,
4086+
* named nothing, and every call through it lost its CALLS edge. Reading the
4087+
* file's own import statements serves the per-file and both cross-file entry
4088+
* points alike. `default as x` keeps today's local-name lookup (a default
4089+
* import binds no exported member name); string export names and identity
4090+
* aliases need no entry. Must run before the type passes, which resolve
4091+
* imported type names. */
4092+
static void ts_lsp_collect_import_aliases(TSLSPContext *ctx, TSNode root) {
4093+
if (!ctx || !ctx->arena || ts_node_is_null(root) || ctx->import_count == 0)
4094+
return;
4095+
uint32_t kn = 0;
4096+
TSNode *kids = collect_children(ctx->arena, root, &kn);
4097+
for (uint32_t i = 0; i < kn; i++) {
4098+
if (!node_kind_is(kids[i], "import_statement"))
4099+
continue;
4100+
TSNode named = find_first_kind(find_first_kind(kids[i], "import_clause"), "named_imports");
4101+
if (ts_node_is_null(named))
4102+
continue;
4103+
uint32_t sn = 0;
4104+
TSNode *specs = collect_children(ctx->arena, named, &sn);
4105+
for (uint32_t j = 0; j < sn; j++) {
4106+
if (!node_kind_is(specs[j], "import_specifier"))
4107+
continue;
4108+
TSNode name = ts_node_child_by_field_name(specs[j], "name", TS_LSP_FIELD_LEN("name"));
4109+
TSNode alias =
4110+
ts_node_child_by_field_name(specs[j], "alias", TS_LSP_FIELD_LEN("alias"));
4111+
if (!node_kind_is(name, "identifier") || !node_kind_is(alias, "identifier"))
4112+
continue;
4113+
char *exported = node_text(ctx, name);
4114+
char *local = node_text(ctx, alias);
4115+
if (exported && local && strcmp(exported, "default") != 0 &&
4116+
strcmp(exported, local) != 0)
4117+
ts_lsp_add_import_alias(ctx, local, exported);
4118+
}
4119+
}
4120+
}
4121+
40084122
// ── Stdlib seeds (Phase 5 will replace with generator) ────────────────────────
40094123

40104124
static const CBMType *tt_builtin(CBMArena *a, const char *n) {
@@ -5948,6 +6062,7 @@ void cbm_run_ts_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
59486062
ts_lsp_add_import(&ctx, imp->local_name, imp->module_path);
59496063
}
59506064
}
6065+
ts_lsp_collect_import_aliases(&ctx, root);
59516066

59526067
ast_sweep_shapes(&ctx, root, &reg);
59536068
refine_ast_signatures(&ctx, root, &reg);
@@ -6201,6 +6316,7 @@ void cbm_run_ts_lsp_cross_with_registry(CBMArena *arena, const char *source, int
62016316
ts_lsp_add_import(&ctx, import_names[i], import_qns[i]);
62026317
}
62036318
}
6319+
ts_lsp_collect_import_aliases(&ctx, root);
62046320

62056321
ast_sweep_shapes(&ctx, root, &overlay);
62066322
refine_ast_signatures(&ctx, root, &overlay);
@@ -6416,6 +6532,7 @@ void cbm_run_ts_lsp_cross(CBMArena *arena, const char *source, int source_len,
64166532
ts_lsp_add_import(&ctx, import_names[i], import_qns[i]);
64176533
}
64186534
}
6535+
ts_lsp_collect_import_aliases(&ctx, root);
64196536

64206537
ast_sweep_shapes(&ctx, root, &reg);
64216538
refine_ast_signatures(&ctx, root, &reg);

‎internal/cbm/lsp/ts_lsp.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ typedef struct {
3333
const char **import_module_qns;
3434
int import_count;
3535

36+
// Renamed named imports (`import { foo as bar }`): local binding -> the name
37+
// the module exports. Parallel arrays of length import_alias_count; a
38+
// binding absent here is exported under its local name.
39+
const char **import_alias_locals;
40+
const char **import_alias_exports;
41+
int import_alias_count;
42+
size_t import_alias_capacity;
43+
3644
// File / surrounding context.
3745
const char *module_qn; // QN of this file's module
3846
const char *enclosing_func_qn; // current function being walked, NULL at module scope

0 commit comments

Comments
 (0)