Join dir_prefix with target, collapsing "." and ".." segments so aliases * that climb out of their tsconfig's directory (the common monorepo * pattern: a tsconfig at apps/web/tsconfig.json pointing an alias at a * wildcard target like "../../packages/shared/src/" + wildcard) resolve * to a real repo-relative path. Naive concatenation left literal ".." * components in the target, which never m
| 77 | * concat still lines up. Returns heap-allocated |
| 78 | * repo-relative target. */ |
| 79 | static char *resolve_target_relative(const char *dir_prefix, const char *target) { |
| 80 | if (!target) { |
| 81 | return NULL; |
| 82 | } |
| 83 | size_t dp_len = (dir_prefix && dir_prefix[0] != '\0') ? strlen(dir_prefix) : 0; |
| 84 | size_t t_len = strlen(target); |
| 85 | char *buf = malloc(dp_len + t_len + 2); |
| 86 | if (!buf) { |
| 87 | return NULL; |
| 88 | } |
| 89 | buf[0] = '\0'; |
| 90 | if (dp_len > 0) { |
| 91 | memcpy(buf, dir_prefix, dp_len); |
| 92 | buf[dp_len] = '\0'; |
| 93 | } |
| 94 | |
| 95 | bool trailing_slash = t_len > 0 && target[t_len - 1] == '/'; |
| 96 | |
| 97 | const char *p = target; |
| 98 | while (*p) { |
| 99 | while (*p == '/') { |
| 100 | p++; |
| 101 | } |
| 102 | if (!*p) { |
| 103 | break; |
| 104 | } |
| 105 | const char *seg_start = p; |
| 106 | while (*p && *p != '/') { |
| 107 | p++; |
| 108 | } |
| 109 | size_t seg_len = (size_t)(p - seg_start); |
| 110 | if (seg_len == 1 && seg_start[0] == '.') { |
| 111 | continue; |
| 112 | } |
| 113 | if (seg_len == 2 && seg_start[0] == '.' && seg_start[1] == '.') { |
| 114 | char *last = strrchr(buf, '/'); |
| 115 | if (last) { |
| 116 | *last = '\0'; |
| 117 | } else { |
| 118 | buf[0] = '\0'; |
| 119 | } |
| 120 | continue; |
| 121 | } |
| 122 | size_t cur = strlen(buf); |
| 123 | if (cur > 0) { |
| 124 | buf[cur++] = '/'; |
| 125 | } |
| 126 | memcpy(buf + cur, seg_start, seg_len); |
| 127 | buf[cur + seg_len] = '\0'; |
| 128 | } |
| 129 | |
| 130 | if (trailing_slash) { |
| 131 | size_t cur = strlen(buf); |
| 132 | buf[cur] = '/'; |
| 133 | buf[cur + 1] = '\0'; |
| 134 | } |
| 135 | return buf; |
| 136 | } |
no outgoing calls
no test coverage detected