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