* Pick the same-FQN candidate closest to `fromPath` by shared directory * prefix, preferring an `expect` declaration on a tie. Used to keep a Kotlin * Multiplatform `expect`/`actual` import resolving within the importer's own * source set instead of an arbitrary platform `actual`.
(candidates: Node[], fromPath: string)
| 1280 | * source set instead of an arbitrary platform `actual`. |
| 1281 | */ |
| 1282 | function pickClosestJvmCandidate(candidates: Node[], fromPath: string): Node { |
| 1283 | const fromDirs = fromPath.split('/').slice(0, -1); |
| 1284 | const sharedPrefix = (p: string): number => { |
| 1285 | const d = p.split('/').slice(0, -1); |
| 1286 | let shared = 0; |
| 1287 | for (let i = 0; i < Math.min(fromDirs.length, d.length); i++) { |
| 1288 | if (fromDirs[i] === d[i]) shared++; |
| 1289 | else break; |
| 1290 | } |
| 1291 | return shared; |
| 1292 | }; |
| 1293 | const isExpect = (n: Node): boolean => Array.isArray(n.decorators) && n.decorators.includes('expect'); |
| 1294 | let best = candidates[0]!; |
| 1295 | let bestProx = sharedPrefix(best.filePath); |
| 1296 | for (let i = 1; i < candidates.length; i++) { |
| 1297 | const c = candidates[i]!; |
| 1298 | const prox = sharedPrefix(c.filePath); |
| 1299 | if (prox > bestProx || (prox === bestProx && isExpect(c) && !isExpect(best))) { |
| 1300 | best = c; |
| 1301 | bestProx = prox; |
| 1302 | } |
| 1303 | } |
| 1304 | return best; |
| 1305 | } |
| 1306 | |
| 1307 | export function resolveViaImport( |
| 1308 | ref: UnresolvedRef, |
no test coverage detected