| 318 | } |
| 319 | |
| 320 | char *cbm_path_alias_resolve(const cbm_path_alias_map_t *map, const char *module_path) { |
| 321 | if (!map || !module_path) { |
| 322 | return NULL; |
| 323 | } |
| 324 | size_t mod_len = strlen(module_path); |
| 325 | |
| 326 | for (int i = 0; i < map->count; i++) { |
| 327 | const cbm_path_alias_t *e = &map->entries[i]; |
| 328 | |
| 329 | if (e->has_wildcard) { |
| 330 | size_t prefix_len = strlen(e->alias_prefix); |
| 331 | size_t suffix_len = strlen(e->alias_suffix); |
| 332 | if (mod_len < prefix_len + suffix_len) { |
| 333 | continue; |
| 334 | } |
| 335 | if (strncmp(module_path, e->alias_prefix, prefix_len) != 0) { |
| 336 | continue; |
| 337 | } |
| 338 | if (suffix_len > 0 && |
| 339 | strcmp(module_path + mod_len - suffix_len, e->alias_suffix) != 0) { |
| 340 | continue; |
| 341 | } |
| 342 | size_t wild_len = mod_len - prefix_len - suffix_len; |
| 343 | const char *wild_start = module_path + prefix_len; |
| 344 | size_t tp_len = strlen(e->target_prefix); |
| 345 | size_t ts_len = strlen(e->target_suffix); |
| 346 | char *result = malloc(tp_len + wild_len + ts_len + 1); |
| 347 | if (!result) { |
| 348 | return NULL; |
| 349 | } |
| 350 | memcpy(result, e->target_prefix, tp_len); |
| 351 | memcpy(result + tp_len, wild_start, wild_len); |
| 352 | memcpy(result + tp_len + wild_len, e->target_suffix, ts_len); |
| 353 | result[tp_len + wild_len + ts_len] = '\0'; |
| 354 | return strip_resolved_ext(result); |
| 355 | } |
| 356 | |
| 357 | if (strcmp(module_path, e->alias_prefix) == 0) { |
| 358 | return strip_resolved_ext(strdup(e->target_prefix)); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | /* baseUrl fallback. Apply only to non-relative imports that look |
| 363 | * sub-path-ish (contain '/' but don't start with '.' or '@'); skips |
| 364 | * obvious package names like "react" or "lodash". */ |
| 365 | if (map->base_url && module_path[0] != '.' && module_path[0] != '@' && |
| 366 | strchr(module_path, '/') != NULL) { |
| 367 | size_t bu_len = strlen(map->base_url); |
| 368 | size_t need = bu_len + 1 + mod_len + 1; |
| 369 | char *result = malloc(need); |
| 370 | if (!result) { |
| 371 | return NULL; |
| 372 | } |
| 373 | snprintf(result, need, "%s/%s", map->base_url, module_path); |
| 374 | return strip_resolved_ext(result); |
| 375 | } |
| 376 | return NULL; |
| 377 | } |
no test coverage detected