(
files: ReadonlyArray<{ path: string; symbols: number; test: boolean }>
)
| 343 | * less and the honest answer is "this repository has no single source root". |
| 344 | */ |
| 345 | export function pickDefaultRoot( |
| 346 | files: ReadonlyArray<{ path: string; symbols: number; test: boolean }> |
| 347 | ): string { |
| 348 | const byDir = new Map<string, number>(); |
| 349 | let total = 0; |
| 350 | for (const file of files) { |
| 351 | if (file.test) continue; |
| 352 | // A file loose in the repository root is program too: git's hundreds of |
| 353 | // top-level `.c` files made `builtin/` look like the majority of the code. |
| 354 | total += file.symbols; |
| 355 | const slash = file.path.indexOf('/'); |
| 356 | if (slash <= 0) continue; |
| 357 | const dir = file.path.slice(0, slash); |
| 358 | byDir.set(dir, (byDir.get(dir) ?? 0) + file.symbols); |
| 359 | } |
| 360 | if (total === 0) return ''; |
| 361 | let best = ''; |
| 362 | let bestSymbols = 0; |
| 363 | let second = 0; |
| 364 | for (const [dir, symbols] of [...byDir].sort((a, b) => a[0].localeCompare(b[0]))) { |
| 365 | if (symbols > bestSymbols) { |
| 366 | second = bestSymbols; |
| 367 | best = dir; |
| 368 | bestSymbols = symbols; |
| 369 | } else if (symbols > second) second = symbols; |
| 370 | } |
| 371 | // A second root holding a fifth of the code (a React Native app's `ios/` |
| 372 | // beside its `src/`) belongs on the picture: map the whole project. |
| 373 | if (second * 5 >= total) return ''; |
| 374 | return bestSymbols * 2 > total ? best : ''; |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * A box holding more than this share of the mapped symbols IS the program, and |
no test coverage detected