Skip to content

Commit 6707c46

Browse files
committed
fix(registry): add CommonJS exports same-module resolution
Signed-off-by: sahil-mangla <manglasahil2017@gmail.com>
1 parent dcde270 commit 6707c46

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

src/pipeline/registry.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,8 @@ static bool is_same_module_receiver(const char *prefix, const char *module_qn) {
610610
return true; /* Bare names are always candidates for same-module resolution */
611611
}
612612
/* 1. Check known self-receivers */
613-
static const char *const self_receivers[] = {"self", "this", "cls", "@self", NULL};
613+
static const char *const self_receivers[] = {"self", "this", "cls", "@self",
614+
"exports", "module", NULL};
614615
for (int i = 0; self_receivers[i]; i++) {
615616
if (strcmp(prefix, self_receivers[i]) == 0) {
616617
return true;
@@ -641,7 +642,11 @@ static cbm_resolution_t resolve_same_module(const cbm_registry_t *r, const char
641642
}
642643
if (suffix && suffix[0]) {
643644
if (is_same_module_receiver(prefix, module_qn)) {
644-
snprintf(candidate, sizeof(candidate), "%s.%s", module_qn, suffix);
645+
const char *real_suffix = suffix;
646+
if (strcmp(prefix, "module") == 0 && strncmp(suffix, "exports.", 8) == 0) {
647+
real_suffix = suffix + 8;
648+
}
649+
snprintf(candidate, sizeof(candidate), "%s.%s", module_qn, real_suffix);
645650
stored_key = cbm_ht_get_key(r->exact, candidate);
646651
if (stored_key) {
647652
return (cbm_resolution_t){stored_key, "same_module", CONF_SAME_MODULE,

tests/test_registry.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,17 @@ TEST(resolve_same_module_only_on_self_receiver) {
290290
ASSERT_STR_EQ(res5.qualified_name, "proj.pkg.service.get");
291291
ASSERT_STR_EQ(res5.strategy, "same_module");
292292

293+
/* CommonJS same-module exports.get / module.exports.get -> should resolve */
294+
cbm_registry_add(r, "check", "proj.pkg.service.check", "Function");
295+
296+
cbm_resolution_t res6 = cbm_registry_resolve(r, "exports.check", "proj.pkg.service", NULL, NULL, 0);
297+
ASSERT_STR_EQ(res6.qualified_name, "proj.pkg.service.check");
298+
ASSERT_STR_EQ(res6.strategy, "same_module");
299+
300+
cbm_resolution_t res7 = cbm_registry_resolve(r, "module.exports.check", "proj.pkg.service", NULL, NULL, 0);
301+
ASSERT_STR_EQ(res7.qualified_name, "proj.pkg.service.check");
302+
ASSERT_STR_EQ(res7.strategy, "same_module");
303+
293304
cbm_registry_free(r);
294305
PASS();
295306
}

0 commit comments

Comments
 (0)