(cg: CodeGraph, query: DeadCodeQuery = {})
| 456 | * plus a count of every doubt. |
| 457 | */ |
| 458 | export function buildDeadCodeReport(cg: CodeGraph, query: DeadCodeQuery = {}): DeadCodeReport { |
| 459 | const kinds = normalizeKinds(query.kinds); |
| 460 | const includeExported = query.includeExported === true; |
| 461 | const includeTests = query.includeTests === true; |
| 462 | const includeGenerated = query.includeGenerated === true; |
| 463 | const limit = Math.max(1, query.limit ?? 200); |
| 464 | const readSource = |
| 465 | query.readSource === undefined ? defaultSourceReader(cg) : query.readSource; |
| 466 | |
| 467 | const excluded: DeadCodeExclusions = { |
| 468 | tests: 0, |
| 469 | generated: 0, |
| 470 | exported: 0, |
| 471 | exportsUnknown: 0, |
| 472 | declarations: 0, |
| 473 | decorated: 0, |
| 474 | overriding: 0, |
| 475 | hooks: 0, |
| 476 | implicit: 0, |
| 477 | vendored: 0, |
| 478 | testScope: 0, |
| 479 | markup: 0, |
| 480 | unreachableFile: 0, |
| 481 | unresolvedName: 0, |
| 482 | ambiguousName: 0, |
| 483 | mentioned: 0, |
| 484 | unreadable: 0, |
| 485 | nested: 0, |
| 486 | }; |
| 487 | |
| 488 | const raw = cg.getUnreferencedNodes(kinds, MAX_DEAD_CODE_CANDIDATES + 1); |
| 489 | const bounded = raw.length > MAX_DEAD_CODE_CANDIDATES; |
| 490 | const candidates = bounded ? raw.slice(0, MAX_DEAD_CODE_CANDIDATES) : raw; |
| 491 | |
| 492 | // Asking the index whether the exported filter can run at all, per language, |
| 493 | // over the handful of languages the candidates are actually in. Skipped when |
| 494 | // the caller has already accepted outside-reachability by asking for exported |
| 495 | // symbols. |
| 496 | const languagesWithExports = includeExported |
| 497 | ? new Set<string>() |
| 498 | : cg.getLanguagesWithExports(candidates.map((row) => row.node.language)); |
| 499 | |
| 500 | // ---- the cheap, per-row rules ------------------------------------------- |
| 501 | const surviving: Array<{ node: Node; generated: boolean }> = []; |
| 502 | for (const row of candidates) { |
| 503 | const { node } = row; |
| 504 | if (!includeTests && isTestFile(node.filePath)) { |
| 505 | excluded.tests += 1; |
| 506 | continue; |
| 507 | } |
| 508 | if (!includeGenerated && row.generated) { |
| 509 | excluded.generated += 1; |
| 510 | continue; |
| 511 | } |
| 512 | if (!includeExported && (node.isExported || isHeaderFile(node.filePath))) { |
| 513 | excluded.exported += 1; |
| 514 | continue; |
| 515 | } |
no test coverage detected