Skip to content

Commit fd9f1d8

Browse files
committed
fix(ts-lsp): resolve calls through renamed named imports
`import { foo as fooShared } from "./lib"; fooShared(1)` produced no CALLS edge to lib.foo, so trace_path(foo, inbound) missed the caller while plain named and namespace imports resolved. The TS LSP import map holds only the local binding, and every import-binding lookup spelled `<module>.<local>` (lib.fooShared), which names nothing. Read the file's own `import { a as b }` specifiers into a local -> exported-name map in all three LSP entry points (per-file and both cross-file paths), before the type passes, and look up the exported member wherever an import binding becomes a member QN: free calls, function values, identifier and `new` types, imported type names and JSX components. The lookup stays exact: a renamed import of a name the module does not export resolves to nothing instead of to a same-named export that matches the alias. `default as x` keeps its local-name lookup. The pipeline joins an LSP row to its call by the callee's leaf name, which for an alias is not the name written at the site. Calls and JSX tags through a renamed import are emitted as lsp_import_alias and function values as lsp_callable_value_reference, with the written name in `reason`, the reason-join strategies the Python, Go, Kotlin, C# and C resolvers already use for aliases. Fixes DeusData#2297 Signed-off-by: Gaurav Dubey <gauravdubey0107@gmail.com>
1 parent 3457b21 commit fd9f1d8

4 files changed

Lines changed: 339 additions & 16 deletions

File tree

‎internal/cbm/lsp/ts_lsp.c‎

Lines changed: 127 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
300315
static 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. */
14601489
static 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

40014109
static 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);

‎internal/cbm/lsp/ts_lsp.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ 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+
3643
// File / surrounding context.
3744
const char *module_qn; // QN of this file's module
3845
const char *enclosing_func_qn; // current function being walked, NULL at module scope

‎tests/test_pipeline.c‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9282,6 +9282,81 @@ TEST(pipeline_ts_crossfile_new_instance_method_call_issue1354) {
92829282
PASS();
92839283
}
92849284

9285+
/* Issue #2297: a call through `import { foo as fooShared }` produced no CALLS
9286+
* edge, so trace_path(foo, inbound) missed the caller. End to end through the
9287+
* real pipeline: the renamed call, an app-local wrapper that shares the
9288+
* exported name, and a method on a renamed class import all reach the
9289+
* exported member, while the plain named import control keeps lsp_ts_import. */
9290+
TEST(pipeline_ts_renamed_import_call_issue2297) {
9291+
char tmp[256];
9292+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_ts_2297_XXXXXX");
9293+
if (!cbm_mkdtemp(tmp)) {
9294+
FAIL("tmpdir");
9295+
}
9296+
9297+
write_temp_file(tmp, "src/lib.ts",
9298+
"export function fooUniqueQrs(x: number): number {\n"
9299+
" return x + 1;\n"
9300+
"}\n"
9301+
"export function buildHealthUniqueQrs(input: { a: number }): number {\n"
9302+
" return input.a;\n"
9303+
"}\n"
9304+
"export class ConnUniqueQrs {\n"
9305+
" pingUniqueQrs(): number {\n"
9306+
" return 1;\n"
9307+
" }\n"
9308+
"}\n");
9309+
write_temp_file(tmp, "src/plain.ts",
9310+
"import { fooUniqueQrs } from './lib';\n"
9311+
"export function callsPlain(): number {\n"
9312+
" return fooUniqueQrs(1);\n"
9313+
"}\n");
9314+
write_temp_file(tmp, "src/renamed.ts",
9315+
"import { fooUniqueQrs as fooShared } from './lib';\n"
9316+
"export function callsRenamed(): number {\n"
9317+
" return fooShared(1);\n"
9318+
"}\n");
9319+
write_temp_file(tmp, "src/wrapper.ts",
9320+
"import { buildHealthUniqueQrs as buildShared } from './lib';\n"
9321+
"export function buildHealthUniqueQrs(input: { a: number }): number {\n"
9322+
" return buildShared({ ...input });\n"
9323+
"}\n");
9324+
write_temp_file(tmp, "src/klass.ts",
9325+
"import { ConnUniqueQrs as Connection } from './lib';\n"
9326+
"export function viaRenamedClass(): number {\n"
9327+
" return new Connection().pingUniqueQrs();\n"
9328+
"}\n");
9329+
9330+
char db_path[512];
9331+
snprintf(db_path, sizeof(db_path), "%s/ts_2297.db", tmp);
9332+
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
9333+
ASSERT_NOT_NULL(p);
9334+
ASSERT_EQ(cbm_pipeline_run(p), 0);
9335+
const char *project = cbm_pipeline_project_name(p);
9336+
9337+
cbm_store_t *s = cbm_store_open_path(db_path);
9338+
ASSERT_NOT_NULL(s);
9339+
9340+
bool plain =
9341+
cross_file_call_has_strategy(s, project, "callsPlain", "fooUniqueQrs", "lsp_ts_import");
9342+
bool renamed = cross_file_call_has_strategy(s, project, "callsRenamed", "fooUniqueQrs",
9343+
"lsp_import_alias");
9344+
bool wrapper = cross_file_call_has_strategy(s, project, "buildHealthUniqueQrs",
9345+
"buildHealthUniqueQrs", "lsp_import_alias");
9346+
bool renamed_class = cross_file_call_has_strategy(s, project, "viaRenamedClass",
9347+
"pingUniqueQrs", "lsp_ts_method");
9348+
9349+
cbm_store_close(s);
9350+
cbm_pipeline_free(p);
9351+
th_rmtree(tmp);
9352+
9353+
ASSERT_TRUE(plain);
9354+
ASSERT_TRUE(renamed);
9355+
ASSERT_TRUE(wrapper);
9356+
ASSERT_TRUE(renamed_class);
9357+
PASS();
9358+
}
9359+
92859360
/* A query failure is distinct from a valid zero-edge result. */
92869361
static int ts514_pipeline_count(cbm_store_t *store, const char *sql, const char **values,
92879362
int value_count) {
@@ -27677,6 +27752,7 @@ SUITE(pipeline) {
2767727752
RUN_TEST(pipeline_tsjs_receiver_suppresses_weak_method_edge);
2767827753
RUN_TEST(pipeline_axios_wrapper_baseurl_composes_http_calls_issue1916);
2767927754
RUN_TEST(pipeline_ts_crossfile_new_instance_method_call_issue1354);
27755+
RUN_TEST(pipeline_ts_renamed_import_call_issue2297);
2768027756
RUN_TEST(pipeline_ts_param_property_injection_issue514);
2768127757
RUN_TEST(pipeline_python_receiver_suppresses_weak_method_edge);
2768227758
RUN_TEST(pipeline_php_receiver_suppresses_weak_method_edge);

0 commit comments

Comments
 (0)