Skip to content

Commit a4cba17

Browse files
ci(types): render the types coverage comment from our own report
The comment listed every file's coverage, most at 100%. It now shows the overall typed lines, the escape-hatch types the pull request adds or removes, the files it changed, and, collapsed, only the files in lib/ and hot/ that still have an escape hatch or an untyped line.
1 parent b741416 commit a4cba17

2 files changed

Lines changed: 127 additions & 49 deletions

File tree

‎.github/workflows/types-coverage.yml‎

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,33 +44,20 @@ jobs:
4444
env:
4545
BASE_SHA: ${{ github.event.pull_request.base.sha }}
4646

47+
# Renders the report too: overall typed lines, the escape-hatch types this
48+
# pull request adds or removes, and the files that still have any.
4749
- name: Collect types coverage
48-
run: |
49-
rm -rf coverage
50-
node tooling/type-coverage.js --base "$BASE_SHA"
51-
yarn report:cover
50+
run: node tooling/type-coverage.js --base "$BASE_SHA"
5251
env:
5352
BASE_SHA: ${{ github.event.pull_request.base.sha }}
5453

55-
# `output-file` renders the comment without posting it, which is all a
56-
# fork's token allows; `pulls.get` for the title only needs read access.
57-
- name: Render the report
58-
uses: Nef10/lcov-reporter-action@797ec1c1e78e3a9a7890c1a104d58b9e05fcef76 # v0.6.1
59-
with:
60-
pr-number: ${{ github.event.pull_request.number }}
61-
lcov-file: ./coverage/lcov.info
62-
title: Types Coverage
63-
output-file: lcov-report.md
64-
6554
- name: Mark the report for the commenter
6655
run: |
6756
{
6857
echo "<!-- types-coverage-report -->"
69-
cat lcov-report.md
70-
echo
71-
cat coverage/escape-hatch-types.md
58+
cat coverage/types-coverage.md
7259
} > types-coverage-report.md
73-
tail -n +2 types-coverage-report.md >> "$GITHUB_STEP_SUMMARY"
60+
cat coverage/types-coverage.md >> "$GITHUB_STEP_SUMMARY"
7461
7562
# Updated in place on every push, so the thread keeps one comment.
7663
- name: Comment the report on the pull request

‎tooling/type-coverage.js‎

Lines changed: 122 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -323,44 +323,135 @@ const totalEscapeHatches = (byFile) => {
323323
const formatChange = (change) =>
324324
change > 0 ? `🔴 +${change}` : change < 0 ? `🟢 −${-change}` : "—";
325325

326+
/**
327+
* Returns the share of a file's lines whose identifiers are typed, as lcov counts lines.
328+
* @param {FileReport} fileReport the file's report
329+
* @returns {{ lines: number, typed: number }} the line counts
330+
*/
331+
const lineCoverage = (fileReport) => {
332+
/** @type {Map<number, number>} */
333+
const lines = new Map();
334+
for (const [index, count] of Object.entries(fileReport.s)) {
335+
const { line } = fileReport.statementMap[Number(index)].start;
336+
lines.set(line, Math.max(lines.get(line) || 0, count));
337+
}
338+
let typed = 0;
339+
for (const count of lines.values()) if (count > 0) typed++;
340+
return { lines: lines.size, typed };
341+
};
342+
343+
/**
344+
* Formats a share as a percentage.
345+
* @param {number} part the part
346+
* @param {number} whole the whole
347+
* @returns {string} the percentage, with two decimals
348+
*/
349+
const percent = (part, whole) =>
350+
whole === 0 ? "100%" : `${((100 * part) / whole).toFixed(2)}%`;
351+
352+
/** @type {Map<string, { lines: number, typed: number }>} */
353+
const coverageByFile = new Map();
354+
let allLines = 0;
355+
let allTyped = 0;
356+
for (const fileReport of Object.values(coverageReport)) {
357+
const coverage = lineCoverage(fileReport);
358+
allLines += coverage.lines;
359+
allTyped += coverage.typed;
360+
coverageByFile.set(
361+
path.relative(root, fileReport.path).split(path.sep).join("/"),
362+
coverage
363+
);
364+
}
365+
326366
const headCounts = countAllEscapeHatches();
327367
const headTotals = totalEscapeHatches(headCounts);
328-
let report = "";
329-
if (base) {
330-
const baseCounts = countAllEscapeHatches(base);
331-
const baseTotals = totalEscapeHatches(baseCounts);
332-
report += "| Type | Base | Head | Change |\n| :-- | --: | --: | --: |\n";
333-
for (const name of ESCAPE_HATCHES) {
334-
report += `| \`${name}\` | ${baseTotals[name]} | ${headTotals[name]} | ${formatChange(
335-
headTotals[name] - baseTotals[name]
336-
)} |\n`;
337-
}
338-
/** @type {string[]} */
339-
const changed = [];
340-
for (const file of [
368+
const baseCounts = base ? countAllEscapeHatches(base) : undefined;
369+
const baseTotals = baseCounts ? totalEscapeHatches(baseCounts) : undefined;
370+
371+
/**
372+
* Returns a file's count of one escape hatch.
373+
* @param {Record<string, EscapeHatchCounts> | undefined} byFile counts by file
374+
* @param {string} file the file
375+
* @param {string} name the escape hatch
376+
* @returns {number} the count
377+
*/
378+
const countOf = (byFile, file, name) =>
379+
(byFile && byFile[file] && byFile[file][name]) || 0;
380+
381+
let report = `Typed lines: **${percent(allTyped, allLines)}** (${allTyped} of ${allLines})\n\n`;
382+
report += baseTotals
383+
? "| Escape hatch | Base | Head | Change |\n| :-- | --: | --: | --: |\n"
384+
: "| Escape hatch | Count |\n| :-- | --: |\n";
385+
for (const name of ESCAPE_HATCHES) {
386+
report += baseTotals
387+
? `| \`${name}\` | ${baseTotals[name]} | ${headTotals[name]} | ${formatChange(
388+
headTotals[name] - baseTotals[name]
389+
)} |\n`
390+
: `| \`${name}\` | ${headTotals[name]} |\n`;
391+
}
392+
393+
const header = `| File | Typed lines | ${ESCAPE_HATCHES.map((name) => `\`${name}\``).join(" | ")} |\n| :-- | --: |${" --: |".repeat(ESCAPE_HATCHES.length)}\n`;
394+
395+
/**
396+
* Renders a file's row: its coverage and each escape hatch, with its change against the base.
397+
* @param {string} file the file
398+
* @returns {string} the row
399+
*/
400+
const fileRow = (file) => {
401+
const coverage = coverageByFile.get(file);
402+
const cells = ESCAPE_HATCHES.map((name) => {
403+
const head = countOf(headCounts, file, name);
404+
const change = head - countOf(baseCounts, file, name);
405+
if (!baseCounts || change === 0) return head === 0 ? "" : `${head}`;
406+
return `${head} (${formatChange(change)})`;
407+
});
408+
return `| \`${file}\` | ${coverage ? percent(coverage.typed, coverage.lines) : "—"} | ${cells.join(" | ")} |`;
409+
};
410+
411+
if (baseCounts) {
412+
const changed = [
341413
...new Set([...Object.keys(baseCounts), ...Object.keys(headCounts)])
342-
].sort()) {
343-
const parts = ESCAPE_HATCHES.map((name) => {
344-
const change =
345-
((headCounts[file] && headCounts[file][name]) || 0) -
346-
((baseCounts[file] && baseCounts[file][name]) || 0);
347-
return change === 0 ? "" : `\`${name}\` ${formatChange(change)}`;
348-
}).filter(Boolean);
349-
if (parts.length > 0) changed.push(`| \`${file}\` | ${parts.join(", ")} |`);
350-
}
414+
]
415+
.filter((file) =>
416+
ESCAPE_HATCHES.some(
417+
(name) =>
418+
countOf(headCounts, file, name) !== countOf(baseCounts, file, name)
419+
)
420+
)
421+
.sort();
351422
report +=
352423
changed.length > 0
353-
? `\n<details><summary>${changed.length} file(s) changed</summary>\n\n| File | Change |\n| :-- | :-- |\n${changed.join("\n")}\n\n</details>\n`
354-
: "\nNo file changed its escape-hatch types.\n";
355-
} else {
356-
report += "| Type | Count |\n| :-- | --: |\n";
357-
for (const name of ESCAPE_HATCHES) {
358-
report += `| \`${name}\` | ${headTotals[name]} |\n`;
359-
}
424+
? `\n#### Changed by this pull request\n\n${header}${changed.map(fileRow).join("\n")}\n`
425+
: "\nThis pull request adds or removes no escape-hatch type.\n";
360426
}
427+
428+
// Every file still owing a real type: an escape hatch or an untyped line.
429+
const remaining = [
430+
...new Set([
431+
...Object.keys(headCounts),
432+
...[...coverageByFile]
433+
.filter(
434+
([file, { lines, typed }]) =>
435+
typed < lines &&
436+
ESCAPE_HATCH_DIRECTORIES.some((directory) =>
437+
file.startsWith(directory)
438+
)
439+
)
440+
.map(([file]) => file)
441+
])
442+
].sort((a, b) => {
443+
const total = (/** @type {string} */ file) =>
444+
ESCAPE_HATCHES.reduce(
445+
(sum, name) => sum + countOf(headCounts, file, name),
446+
0
447+
);
448+
return total(b) - total(a) || (a < b ? -1 : 1);
449+
});
450+
report += `\n<details><summary>${remaining.length} file(s) in <code>lib/</code> and <code>hot/</code> still use an escape hatch or have untyped lines</summary>\n\n${header}${remaining.map(fileRow).join("\n")}\n\n</details>\n`;
451+
361452
fs.writeFileSync(
362-
path.resolve(outputDirectory, "escape-hatch-types.md"),
363-
`### Escape-hatch types\n\n${report}`,
453+
path.resolve(outputDirectory, "types-coverage.md"),
454+
`## Types Coverage\n\n${report}`,
364455
"utf8"
365456
);
366457
console.log(report);

0 commit comments

Comments
 (0)