// Which diagnostics a report prints in full, which it rolls up and which it only counts. import type { Issue, Level } from "./RunView.js"; /** A count of one rule's warnings, with their file when they share one. */ export interface IssueRollup { readonly code: string; readonly count: number; readonly file?: string; } /** What to print of a run's issues. */ export interface IssueLayout { /** Printed in full, in order. */ readonly lines: ReadonlyArray; /** The API Extractor and TSDoc warnings. Absent when there are none. */ readonly api?: { readonly total: number; readonly full: ReadonlyArray; readonly rollup: ReadonlyArray; }; /** Warnings counted and not shown, at the quiet level. */ readonly otherWarnings: number; readonly firstError?: Issue; } /** The most API warnings a default report prints in full before it rolls them up by rule. */ export const API_ROLLUP_THRESHOLD = 10; const isApi = (issue: Issue): boolean => issue.source === "api-extractor"; const rollupOf = (issues: ReadonlyArray): ReadonlyArray => { const byCode = new Map>(); for (const issue of issues) { const code = issue.code ?? "(no code)"; byCode.set(code, [...(byCode.get(code) ?? []), issue]); } return ( [...byCode] .map(([code, members]): IssueRollup => { const file = members[0]?.file; const shared = file !== undefined && members.every((member) => member.file === file); return { code, count: members.length, ...(shared ? { file } : {}) }; }) // A stable sort, so rules with the same count keep the order they first appeared in. .sort((a, b) => b.count - a.count) ); }; /** * Lays a run's issues out for one level. * * @remarks * Errors are always printed in full, and so is any warning that fails CI. An API warning (`source` `api-extractor`) * goes in the `api` block: all of them in full at the verbose level or with `rollup` off, and at the default level * while the ones that do not fail CI number {@link API_ROLLUP_THRESHOLD} or fewer; past that they roll up by rule. * At the quiet level they always roll up, and every other warning is only counted. With `rollup` off, which is what * an agent and a CI log get, the quiet level counts the API warnings too, since there is no rollup to show them in. * * @param issues - the run's errors and warnings * @param options - the level, and whether rolling up is allowed */ export const layoutIssues = ( issues: ReadonlyArray, options: { readonly level: Level; readonly rollup: boolean }, ): IssueLayout => { const { level, rollup } = options; const firstError = issues.find((issue) => issue.level === "error"); const apiWarnings = issues.filter((issue) => issue.level === "warning" && isApi(issue)); const fatal = apiWarnings.filter((issue) => issue.ciFatal === true); const rest = apiWarnings.filter((issue) => issue.ciFatal !== true); const others = issues.filter((issue) => issue.level === "warning" && !isApi(issue)); const everyApiInFull = level === "verbose" || (level === "default" && (!rollup || rest.length <= API_ROLLUP_THRESHOLD)); const rolled = !everyApiInFull && rollup; const hiddenOthers = level === "quiet" ? others.filter((issue) => issue.ciFatal !== true) : []; const hiddenApi = !everyApiInFull && !rollup ? rest.length : 0; return { lines: issues.filter( (issue) => issue.level === "error" || (!isApi(issue) && (level !== "quiet" || issue.ciFatal === true)), ), ...(apiWarnings.length > 0 ? { api: { total: apiWarnings.length, full: everyApiInFull ? apiWarnings : fatal, rollup: rolled ? rollupOf(rest) : [], }, } : {}), otherWarnings: hiddenOthers.length + hiddenApi, ...(firstError !== undefined ? { firstError } : {}), }; };