Skip to content

Commit 9ee111c

Browse files
committed
fix(514): follow TS re-export chains to the defining file
Named re-exports (export { a as b } from './m') and star re-exports (export * from './m') are now recorded as re-export imports instead of one module-level import. JS and TS call resolution uses the cross-file import map, so a call through a barrel file binds to the function's defining file. The MCP evidence class now labels suffix_match and unique_name edges as name_guess. Tested: barrel suite 89 passed; extraction_imports, edge_imports, path_alias and mcp suites 525 passed, 4 skipped. On the frozen bake-off corpus, caller recall is 227 of 232 (was 203) with no new wrong edges.
1 parent 60d96ab commit 9ee111c

10 files changed

Lines changed: 223 additions & 24 deletions

File tree

‎internal/cbm/cbm.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,12 @@ typedef enum {
406406
CBM_IMPORT_KIND_CONST, // PHP `use const A\B`
407407
} CBMImportKind;
408408

409+
typedef enum {
410+
CBM_REEXPORT_NONE = 0,
411+
CBM_REEXPORT_NAMED,
412+
CBM_REEXPORT_STAR,
413+
} CBMReexportKind;
414+
409415
typedef struct {
410416
const char *local_name; // local alias or name
411417
const char *module_path; // resolved module path / QN
@@ -415,6 +421,7 @@ typedef struct {
415421
bool type_checking_only; // Python: inside a module-level `if TYPE_CHECKING:` block --
416422
// binds the name for annotations, never at runtime (#1277)
417423
CBMImportKind kind; // CBM_IMPORT_KIND_DEFAULT unless the syntax names a member kind
424+
CBMReexportKind reexport; // ES `export {a as b} from` or `export * from`; NONE otherwise
418425
} CBMImport;
419426

420427
/* True when an import binds its local name at runtime. A Python import under

‎internal/cbm/extract_imports.c‎

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,6 +1814,54 @@ static bool process_commonjs_require(CBMExtractCtx *ctx, TSNode call) {
18141814
return true;
18151815
}
18161816

1817+
static void process_es_export_statement(CBMExtractCtx *ctx, TSNode node) {
1818+
CBMArena *a = ctx->arena;
1819+
TSNode src = ts_node_child_by_field_name(node, TS_FIELD("source"));
1820+
if (ts_node_is_null(src)) {
1821+
return;
1822+
}
1823+
char *path = strip_quotes(a, cbm_node_text(a, src, ctx->source));
1824+
if (!path || !path[0]) {
1825+
return;
1826+
}
1827+
uint32_t nc = ts_node_child_count(node);
1828+
for (uint32_t i = 0; i < nc; i++) {
1829+
TSNode child = ts_node_child(node, i);
1830+
const char *kind = ts_node_type(child);
1831+
if (strcmp(kind, "export_clause") == 0) {
1832+
uint32_t sc = ts_node_child_count(child);
1833+
for (uint32_t j = 0; j < sc; j++) {
1834+
TSNode spec = ts_node_child(child, j);
1835+
if (strcmp(ts_node_type(spec), "export_specifier") != 0) {
1836+
continue;
1837+
}
1838+
TSNode original = ts_node_child_by_field_name(spec, TS_FIELD("name"));
1839+
TSNode alias = ts_node_child_by_field_name(spec, TS_FIELD("alias"));
1840+
if (ts_node_is_null(original)) {
1841+
continue;
1842+
}
1843+
const char *member = cbm_node_text(a, original, ctx->source);
1844+
const char *exported = ts_node_is_null(alias)
1845+
? member
1846+
: cbm_node_text(a, alias, ctx->source);
1847+
CBMImport imp = {.local_name = exported,
1848+
.module_path = path,
1849+
.member = member,
1850+
.reexport = CBM_REEXPORT_NAMED};
1851+
cbm_imports_push(&ctx->result->imports, a, imp);
1852+
}
1853+
return;
1854+
}
1855+
if (strcmp(kind, "*") == 0) {
1856+
CBMImport imp = {.local_name = "*",
1857+
.module_path = path,
1858+
.reexport = CBM_REEXPORT_STAR};
1859+
cbm_imports_push(&ctx->result->imports, a, imp);
1860+
return;
1861+
}
1862+
}
1863+
}
1864+
18171865
static void walk_es_imports(CBMExtractCtx *ctx, TSNode root) {
18181866
TSNodeStack stack;
18191867
ts_nstack_init(&stack, ctx, CBM_SZ_512);
@@ -1829,18 +1877,7 @@ static void walk_es_imports(CBMExtractCtx *ctx, TSNode root) {
18291877
push_children = false;
18301878
}
18311879
} else if (strcmp(kind, "export_statement") == 0) {
1832-
/* Re-export: `export { x } from './mod'` / `export * from './mod'`.
1833-
* It carries a `source` string just like an import and creates the
1834-
* same module dependency. */
1835-
TSNode src = ts_node_child_by_field_name(node, TS_FIELD("source"));
1836-
if (!ts_node_is_null(src)) {
1837-
char *path = strip_quotes(ctx->arena, cbm_node_text(ctx->arena, src, ctx->source));
1838-
if (path && path[0]) {
1839-
CBMImport imp = {.local_name = path_last(ctx->arena, path),
1840-
.module_path = path};
1841-
cbm_imports_push(&ctx->result->imports, ctx->arena, imp);
1842-
}
1843-
}
1880+
process_es_export_statement(ctx, node);
18441881
} else if (strcmp(kind, "call_expression") == 0) {
18451882
/* CommonJS require() — only consume the node if we recognized
18461883
* it as a require call; otherwise keep traversing the children. */

‎src/mcp/mcp.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,8 @@ static const tool_def_t TOOLS[] = {
644644
"\"description\":\"tree chooses smaller complete direct/grouped output; json uses stable "
645645
"grouped tables.\"},"
646646
"\"include_evidence\":{\"type\":\"boolean\",\"default\":false,"
647-
"\"description\":\"Add resolver class and confidence.\"}},"
647+
"\"description\":\"Add resolver class and confidence; name_guess marks "
648+
"suffix_match and unique_name edges.\"}},"
648649
"\"required\":[\"function_name\",\"project\"]}"},
649650

650651
{"get_code_snippet",
@@ -9382,8 +9383,11 @@ const char *cbm_mcp_edge_strategy_class(const char *strategy) {
93829383
if (strncmp(strategy, "php_", 4) == 0 || strncmp(strategy, "perl_", 5) == 0) {
93839384
return "language_rule";
93849385
}
9386+
if (strcmp(strategy, "suffix_match") == 0 || strcmp(strategy, "unique_name") == 0) {
9387+
return "name_guess";
9388+
}
93859389
/* Everything else is a name/shape heuristic: callee_suffix,
9386-
* field_type_hint, service_pattern, fastapi_depends, unique_name, ... */
9390+
* field_type_hint, service_pattern, fastapi_depends, ... */
93879391
return "heuristic";
93889392
}
93899393

‎src/pipeline/pass_calls.c‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ enum {
2525
#include <stdint.h>
2626
#include "pipeline/pipeline_internal.h"
2727
#include "pipeline/lsp_resolve.h"
28+
#include "pipeline/pass_lsp_cross.h"
2829
#include "pipeline/laravel_routing.h"
2930
#include "graph_buffer/graph_buffer.h"
3031
#include "foundation/log.h"
@@ -138,6 +139,45 @@ static int build_import_map(cbm_pipeline_ctx_t *ctx, const char *rel_path,
138139
*out_vals = NULL;
139140
*out_count = 0;
140141

142+
CBMLanguage lang = cbm_language_for_filename(rel_path);
143+
if (lang == CBM_LANG_JAVASCRIPT || lang == CBM_LANG_TYPESCRIPT || lang == CBM_LANG_TSX) {
144+
const char **keys = NULL;
145+
const char **owned_vals = NULL;
146+
int count = 0;
147+
cbm_pxc_build_import_map(ctx->gbuf, ctx->project_name, rel_path, lang, result, &keys,
148+
&owned_vals, &count);
149+
if (count > 0 && keys && owned_vals) {
150+
const char **borrowed_vals = calloc((size_t)count, sizeof(*borrowed_vals));
151+
if (!borrowed_vals) {
152+
cbm_pxc_free_import_map(keys, owned_vals, count);
153+
return 0;
154+
}
155+
int kept = 0;
156+
for (int i = 0; i < count; i++) {
157+
const cbm_gbuf_node_t *node =
158+
owned_vals[i] ? cbm_gbuf_find_by_qn(ctx->gbuf, owned_vals[i]) : NULL;
159+
if (node && node->qualified_name) {
160+
keys[kept] = keys[i];
161+
borrowed_vals[kept++] = node->qualified_name;
162+
} else {
163+
free((void *)keys[i]);
164+
}
165+
free((void *)owned_vals[i]);
166+
}
167+
free((void *)owned_vals);
168+
if (kept > 0 && borrowed_vals) {
169+
*out_keys = keys;
170+
*out_vals = borrowed_vals;
171+
*out_count = kept;
172+
return 0;
173+
}
174+
free((void *)keys);
175+
free((void *)borrowed_vals);
176+
} else {
177+
cbm_pxc_free_import_map(keys, owned_vals, count);
178+
}
179+
}
180+
141181
/* Fast path: build from cached extraction via the same resolver used for
142182
* IMPORTS edges. Do NOT use cbm_pipeline_fqn_module(module_path) — Python
143183
* from-imports store "pkg.symbol" (and aliases) in module_path, which is

‎src/pipeline/pass_definitions.c‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,19 @@ static int create_import_edges_for_file(cbm_pipeline_ctx_t *ctx, const CBMFileRe
683683
char esc_ln[CBM_SZ_128];
684684
cbm_json_escape(esc_ln, sizeof(esc_ln), imp->local_name ? imp->local_name : "");
685685
char imp_props[CBM_SZ_256];
686-
snprintf(imp_props, sizeof(imp_props), "{\"local_name\":\"%s\"}", esc_ln);
686+
if (imp->reexport == CBM_REEXPORT_NAMED && imp->member) {
687+
char esc_member[CBM_SZ_128];
688+
cbm_json_escape(esc_member, sizeof(esc_member), imp->member);
689+
snprintf(imp_props, sizeof(imp_props),
690+
"{\"local_name\":\"%s\",\"reexport\":\"named\","
691+
"\"imported\":\"%s\"}",
692+
esc_ln, esc_member);
693+
} else if (imp->reexport == CBM_REEXPORT_STAR) {
694+
snprintf(imp_props, sizeof(imp_props),
695+
"{\"local_name\":\"%s\",\"reexport\":\"star\"}", esc_ln);
696+
} else {
697+
snprintf(imp_props, sizeof(imp_props), "{\"local_name\":\"%s\"}", esc_ln);
698+
}
687699
cbm_gbuf_insert_edge(ctx->gbuf, source_node->id, target->id, "IMPORTS", imp_props);
688700
count++;
689701
}

‎src/pipeline/pass_lsp_cross.c‎

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,10 +1422,9 @@ static char *pxc_import_value_qn(CBMLanguage lang, const CBMFileResult *result,
14221422
/* JS/TS barrels. An import that lands on a Folder (`./svc` -> svc/index.ts)
14231423
* or on a module that only re-exports the name never reaches the declaring
14241424
* module, so `module.Name` is not in the registry and DI member calls on the
1425-
* imported type stay unresolved. Follow the barrel's own IMPORTS edges (its
1426-
* `export ... from` statements) to the one module that defines `name`. Those
1427-
* edges do not record which names each statement re-exports, so the
1428-
* definition node is the proof; two different definitions fail closed. */
1425+
* imported type stay unresolved. Follow only the barrel's re-export IMPORTS
1426+
* edges, preserving `export { original as public }` names across each hop.
1427+
* The definition node is the proof; two different definitions fail closed. */
14291428
enum { PXC_BARREL_MAX_DEPTH = 8 };
14301429

14311430
static bool pxc_is_js_family(CBMLanguage lang) {
@@ -1445,6 +1444,30 @@ static char *pxc_defined_symbol_qn(const cbm_gbuf_t *gbuf, const char *project_n
14451444
return NULL;
14461445
}
14471446

1447+
static bool pxc_edge_string_prop(const char *json, const char *key, char *out, size_t out_sz) {
1448+
if (!json || !key || !out || out_sz == 0) {
1449+
return false;
1450+
}
1451+
char needle[CBM_SZ_64];
1452+
int wrote = snprintf(needle, sizeof(needle), "\"%s\":\"", key);
1453+
if (wrote <= 0 || (size_t)wrote >= sizeof(needle)) {
1454+
return false;
1455+
}
1456+
const char *start = strstr(json, needle);
1457+
if (!start) {
1458+
return false;
1459+
}
1460+
start += (size_t)wrote;
1461+
const char *end = strchr(start, '"');
1462+
size_t len = end ? (size_t)(end - start) : 0;
1463+
if (!end || len == 0 || len >= out_sz || memchr(start, '\\', len)) {
1464+
return false;
1465+
}
1466+
memcpy(out, start, len);
1467+
out[len] = '\0';
1468+
return true;
1469+
}
1470+
14481471
/* `visited` holds barrel file QNs already searched for `name`: barrels form
14491472
* DAGs (and cycles) where one lib index is reachable through many paths, and
14501473
* re-walking it on each path is exponential in the depth cap. */
@@ -1454,6 +1477,13 @@ static char *pxc_barrel_symbol_qn(const cbm_gbuf_t *gbuf, const char *project_na
14541477
if (!target || !target->label || !target->file_path || depth > PXC_BARREL_MAX_DEPTH) {
14551478
return NULL;
14561479
}
1480+
if (strcmp(target->label, "Module") != 0 && strcmp(target->label, "Folder") != 0 &&
1481+
strcmp(target->label, "File") != 0) {
1482+
/* A named re-export edge reaches a definition directly when ordinary
1483+
* import resolution proved its imported member. The edge metadata has
1484+
* already matched the public/imported names at the caller above. */
1485+
return target->qualified_name ? strdup(target->qualified_name) : NULL;
1486+
}
14571487
static const char *const index_names[] = {"index.ts", "index.tsx", "index.mts",
14581488
"index.js", "index.jsx", "index.mjs"};
14591489
char rel[CBM_SZ_1K];
@@ -1488,8 +1518,27 @@ static char *pxc_barrel_symbol_qn(const cbm_gbuf_t *gbuf, const char *project_na
14881518
}
14891519
char *found = NULL;
14901520
for (int i = 0; i < edge_count; i++) {
1521+
char kind[CBM_SZ_16];
1522+
char public_name[CBM_SZ_256];
1523+
char imported_name[CBM_SZ_256];
1524+
if (!pxc_edge_string_prop(edges[i]->properties_json, "reexport", kind, sizeof(kind))) {
1525+
continue;
1526+
}
1527+
const char *next_name = name;
1528+
if (strcmp(kind, "named") == 0) {
1529+
if (!pxc_edge_string_prop(edges[i]->properties_json, "local_name", public_name,
1530+
sizeof(public_name)) ||
1531+
strcmp(public_name, name) != 0 ||
1532+
!pxc_edge_string_prop(edges[i]->properties_json, "imported", imported_name,
1533+
sizeof(imported_name))) {
1534+
continue;
1535+
}
1536+
next_name = imported_name;
1537+
} else if (strcmp(kind, "star") != 0) {
1538+
continue;
1539+
}
14911540
const cbm_gbuf_node_t *next = cbm_gbuf_find_by_id(gbuf, edges[i]->target_id);
1492-
char *qn = pxc_barrel_symbol_qn(gbuf, project_name, next, name, depth + 1, visited);
1541+
char *qn = pxc_barrel_symbol_qn(gbuf, project_name, next, next_name, depth + 1, visited);
14931542
if (!qn) {
14941543
continue;
14951544
}

‎src/pipeline/pass_parallel.c‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1900,7 +1900,19 @@ static int create_imports_edges(cbm_pipeline_ctx_t *ctx, const CBMFileResult *re
19001900
char esc_ln[CBM_SZ_128];
19011901
cbm_json_escape(esc_ln, sizeof(esc_ln), imp->local_name ? imp->local_name : "");
19021902
char imp_props[CBM_SZ_256];
1903-
snprintf(imp_props, sizeof(imp_props), "{\"local_name\":\"%s\"}", esc_ln);
1903+
if (imp->reexport == CBM_REEXPORT_NAMED && imp->member) {
1904+
char esc_member[CBM_SZ_128];
1905+
cbm_json_escape(esc_member, sizeof(esc_member), imp->member);
1906+
snprintf(imp_props, sizeof(imp_props),
1907+
"{\"local_name\":\"%s\",\"reexport\":\"named\","
1908+
"\"imported\":\"%s\"}",
1909+
esc_ln, esc_member);
1910+
} else if (imp->reexport == CBM_REEXPORT_STAR) {
1911+
snprintf(imp_props, sizeof(imp_props),
1912+
"{\"local_name\":\"%s\",\"reexport\":\"star\"}", esc_ln);
1913+
} else {
1914+
snprintf(imp_props, sizeof(imp_props), "{\"local_name\":\"%s\"}", esc_ln);
1915+
}
19041916
cbm_gbuf_insert_edge(ctx->gbuf, source_node->id, target->id, "IMPORTS", imp_props);
19051917
count++;
19061918
}

‎tests/test_extraction_imports.c‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,6 +1310,30 @@ TEST(es_named_import_member_survives_compaction) {
13101310
PASS();
13111311
}
13121312

1313+
TEST(es_reexport_names_and_star_survive_compaction) {
1314+
CBMFileResult *r = do_extract("export { internal as publicName, plain } from './child.js';\n"
1315+
"export * from './nested.js';\n",
1316+
CBM_LANG_TYPESCRIPT, "barrel.ts");
1317+
ASSERT_NOT_NULL(r);
1318+
ASSERT_FALSE(r->has_error);
1319+
cbm_result_compact(r);
1320+
const CBMImport *renamed = imp_by_local(r, "publicName");
1321+
const CBMImport *plain = imp_by_local(r, "plain");
1322+
const CBMImport *star = imp_by_local(r, "*");
1323+
ASSERT_NOT_NULL(renamed);
1324+
ASSERT_NOT_NULL(plain);
1325+
ASSERT_NOT_NULL(star);
1326+
ASSERT_STR_EQ(renamed->module_path, "./child.js");
1327+
ASSERT_STR_EQ(renamed->member, "internal");
1328+
ASSERT_EQ(renamed->reexport, CBM_REEXPORT_NAMED);
1329+
ASSERT_STR_EQ(plain->member, "plain");
1330+
ASSERT_EQ(plain->reexport, CBM_REEXPORT_NAMED);
1331+
ASSERT_STR_EQ(star->module_path, "./nested.js");
1332+
ASSERT_EQ(star->reexport, CBM_REEXPORT_STAR);
1333+
cbm_free_result(r);
1334+
PASS();
1335+
}
1336+
13131337
/* ═══════════════════════════════════════════════════════════════════════════
13141338
* SUITE declaration — do NOT register in test_main.c; the main workstream will
13151339
* ═══════════════════════════════════════════════════════════════════════════ */
@@ -1353,4 +1377,5 @@ SUITE(extraction_imports) {
13531377

13541378
/* ES named-import member (call resolution spells calls through it) */
13551379
RUN_TEST(es_named_import_member_survives_compaction);
1380+
RUN_TEST(es_reexport_names_and_star_survive_compaction);
13561381
}

‎tests/test_lsp_resolution_probe.c‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,16 +1315,21 @@ TEST(lrp_ts_barrel_reexport_member_calls) {
13151315
" return weightKg * 2;\n"
13161316
" }\n"
13171317
"}\n"},
1318+
{"apps/api/libs/shipping/src/domain/internal.service.ts",
1319+
"export function internalCall(): number { return 7; }\n"},
1320+
{"apps/api/libs/shipping/src/domain/public.ts",
1321+
"export { internalCall as publicCall } from './internal.service.js';\n"},
13181322
{"apps/api/libs/shipping/src/index.ts",
13191323
"export * from \"./domain/tracking\";\n"
13201324
"export {\n"
13211325
" CarrierService,\n"
13221326
"} from \"./domain/carrier.service\";\n"
1327+
"export { publicCall as apiCall } from './domain/public.js';\n"
13231328
"export type { LabelPrintParams } from \"./application/service/label-print.service\";\n"},
13241329
{"apps/api/libs/invoice/src/application/command/handler/ship-invoice.handler.ts",
13251330
"import { LabelPrintService } from "
13261331
"\"@acme/shipping/application/service/label-print.service\";\n"
1327-
"import { CarrierService, TrackingCodeService } from \"@acme/shipping\";\n"
1332+
"import { apiCall, CarrierService, TrackingCodeService } from \"@acme/shipping\";\n"
13281333
"export class ShipInvoiceHandler {\n"
13291334
" constructor(\n"
13301335
" private readonly labelPrintService: LabelPrintService,\n"
@@ -1334,8 +1339,9 @@ TEST(lrp_ts_barrel_reexport_member_calls) {
13341339
" execute(shipmentId: string): string {\n"
13351340
" const cost = this.carrierService.quote(3);\n"
13361341
" const code = this.trackingCodeService.generate(shipmentId);\n"
1342+
" const renamed = apiCall();\n"
13371343
" const label = this.labelPrintService.printStream({ shipmentId });\n"
1338-
" return `${label}:${code}:${cost}`;\n"
1344+
" return `${label}:${code}:${cost}:${renamed}`;\n"
13391345
" }\n"
13401346
"}\n"},
13411347
{"apps/api/libs/invoice/src/application/service/label-preview.service.ts",
@@ -1360,16 +1366,21 @@ TEST(lrp_ts_barrel_reexport_member_calls) {
13601366
"LabelPrintService.printWithStats");
13611367
int b4 = lrp_exact_edge_by_qn_suffix(store, lp.project, "CALLS", "ShipInvoiceHandler.execute",
13621368
"TrackingCodeService.generate");
1369+
int renamed = lrp_exact_edge_by_qn_suffix(store, lp.project, "CALLS",
1370+
"ShipInvoiceHandler.execute",
1371+
"internal.service.internalCall");
13631372
int self =
13641373
lrp_exact_edge_by_qn_suffix(store, lp.project, "CALLS", "LabelPrintService.printStream",
13651374
"LabelPrintService.printWithStats");
1366-
printf(" [ts/barrel] B1=%d B2=%d B3=%d B4=%d self=%d\n", b1, b2, b3, b4, self);
1375+
printf(" [ts/barrel] B1=%d B2=%d B3=%d B4=%d renamed=%d self=%d\n", b1, b2, b3, b4,
1376+
renamed, self);
13671377
lrp_cleanup(&lp, store);
13681378

13691379
ASSERT_EQ(b1, 1);
13701380
ASSERT_EQ(b2, 1);
13711381
ASSERT_EQ(b3, 1);
13721382
ASSERT_EQ(b4, 1);
1383+
ASSERT_EQ(renamed, 1);
13731384
ASSERT_EQ(self, 1);
13741385
PASS();
13751386
}

0 commit comments

Comments
 (0)