coverage-uncovered-locations.cjs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. 'use strict';
  2. /**
  3. * Istanbul coverage reporter printing one clickable `path:line:col` record per
  4. * uncovered statement, branch path, and function. Vitest's per-file threshold
  5. * failures name only the file; this reporter supplies the exact locations,
  6. * printed just above those ERROR lines (reports run before threshold checks).
  7. * Files at 100% print nothing, so a green run stays silent.
  8. *
  9. * CommonJS by requirement: istanbul-reports loads custom reporters with a bare
  10. * require() outside the tsx/ESM pipeline (istanbul-reports index.js create()),
  11. * so this file can be neither TypeScript nor ESM. Wired into vitest.config.ts
  12. * by absolute path — require() would resolve a relative specifier against
  13. * istanbul-reports' own directory.
  14. */
  15. const path = require('node:path');
  16. const { ReportBase } = require('istanbul-lib-report');
  17. /**
  18. * Editor-convention `line:column` of an istanbul location start (istanbul
  19. * columns are 0-based; editors and terminal link handlers expect 1-based).
  20. */
  21. function pos(loc) {
  22. return `${loc.start.line}:${loc.start.column + 1}`;
  23. }
  24. /** Whether a location carries a usable 1-based start line. */
  25. function usable(loc) {
  26. return Boolean(loc && loc.start && Number.isFinite(loc.start.line) && loc.start.line >= 1);
  27. }
  28. /**
  29. * ` (to line:col)` suffix when the range end adds information beyond the
  30. * start. v8-remapped whole-line statements carry end.column = Infinity; those
  31. * degrade to a line-only suffix, or to nothing on a single line.
  32. */
  33. function endSuffix(loc) {
  34. const end = loc.end;
  35. if (!end || !Number.isFinite(end.line) || end.line < 1) return '';
  36. if (!Number.isFinite(end.column)) {
  37. return end.line === loc.start.line ? '' : ` (to ${end.line})`;
  38. }
  39. if (end.line === loc.start.line && end.column === loc.start.column) return '';
  40. return ` (to ${end.line}:${end.column + 1})`;
  41. }
  42. class UncoveredLocationsReport extends ReportBase {
  43. constructor(opts = {}) {
  44. super(opts);
  45. // Vitest passes the resolved config root alongside reporter options.
  46. this.projectRoot = opts.projectRoot || process.cwd();
  47. this.records = [];
  48. }
  49. onStart() {
  50. this.records = [];
  51. }
  52. onDetail(node) {
  53. const fc = node.getFileCoverage();
  54. const rel = path.relative(this.projectRoot, fc.path).split(path.sep).join('/');
  55. const items = [];
  56. const add = (loc, text) => items.push({ line: loc.start.line, column: loc.start.column, text });
  57. for (const id of Object.keys(fc.statementMap)) {
  58. if (fc.s[id] !== 0) continue;
  59. const loc = fc.statementMap[id];
  60. if (!usable(loc)) continue;
  61. add(loc, `${rel}:${pos(loc)} uncovered statement${endSuffix(loc)}`);
  62. }
  63. for (const id of Object.keys(fc.fnMap)) {
  64. if (fc.f[id] !== 0) continue;
  65. const fn = fc.fnMap[id];
  66. const loc = usable(fn.decl) ? fn.decl : fn.loc;
  67. if (!usable(loc)) continue;
  68. const name = fn.name ? ` ${fn.name}` : '';
  69. add(loc, `${rel}:${pos(loc)} uncovered function${name}`);
  70. }
  71. for (const id of Object.keys(fc.branchMap)) {
  72. const counts = fc.b[id];
  73. const branch = fc.branchMap[id];
  74. for (let i = 0; i < counts.length; i += 1) {
  75. if (counts[i] !== 0) continue;
  76. // Implicit arms (e.g. a missing else) may carry an empty location;
  77. // fall back to the branch's own span so the record stays clickable.
  78. const loc = usable(branch.locations && branch.locations[i]) ? branch.locations[i] : branch.loc;
  79. if (!usable(loc)) continue;
  80. add(loc, `${rel}:${pos(loc)} uncovered branch (${branch.type}, path ${i + 1}/${counts.length})`);
  81. }
  82. }
  83. if (items.length === 0) return;
  84. items.sort((a, b) => a.line - b.line || a.column - b.column);
  85. for (const item of items) this.records.push(item.text);
  86. }
  87. onEnd() {
  88. if (this.records.length === 0) return;
  89. console.log(`\nUncovered locations (per-file 100% gate): ${this.records.length}`);
  90. for (const record of this.records) console.log(record);
  91. console.log('');
  92. }
  93. }
  94. module.exports = UncoveredLocationsReport;