| 16 | * Format lint findings for console output. Used by lint, render, and preview commands. |
| 17 | */ |
| 18 | export function formatLintFindings( |
| 19 | { results, totalErrors, totalWarnings, totalInfos }: ProjectLintResult, |
| 20 | options: LintFormatOptions = {}, |
| 21 | ): string[] { |
| 22 | const { |
| 23 | showElementId = true, |
| 24 | showSummary = false, |
| 25 | errorsFirst = false, |
| 26 | verbose = false, |
| 27 | } = options; |
| 28 | const lines: string[] = []; |
| 29 | const multiFile = results.length > 1; |
| 30 | |
| 31 | for (const { file, result } of results) { |
| 32 | if (result.findings.length === 0) continue; |
| 33 | |
| 34 | const format = (finding: (typeof result.findings)[0]) => { |
| 35 | if (!verbose && finding.severity === "info") return; |
| 36 | const prefix = |
| 37 | finding.severity === "error" |
| 38 | ? c.error("✗") |
| 39 | : finding.severity === "warning" |
| 40 | ? c.warn("⚠") |
| 41 | : c.dim("ℹ"); |
| 42 | const fileLabel = multiFile ? c.dim(`[${file}] `) : ""; |
| 43 | const loc = |
| 44 | showElementId && finding.elementId ? ` ${c.accent(`[${finding.elementId}]`)}` : ""; |
| 45 | lines.push(` ${prefix} ${fileLabel}${c.bold(finding.code)}${loc}: ${finding.message}`); |
| 46 | if (finding.fixHint) lines.push(` ${c.dim(`Fix: ${finding.fixHint}`)}`); |
| 47 | }; |
| 48 | |
| 49 | if (errorsFirst) { |
| 50 | for (const f of result.findings) if (f.severity === "error") format(f); |
| 51 | for (const f of result.findings) if (f.severity === "warning") format(f); |
| 52 | if (verbose) for (const f of result.findings) if (f.severity === "info") format(f); |
| 53 | } else { |
| 54 | for (const f of result.findings) format(f); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | if (showSummary) { |
| 59 | const icon = totalErrors > 0 ? c.error("◇") : c.success("◇"); |
| 60 | lines.push(""); |
| 61 | const summaryParts = [`${totalErrors} error(s)`, `${totalWarnings} warning(s)`]; |
| 62 | if (verbose && totalInfos > 0) summaryParts.push(`${totalInfos} info(s)`); |
| 63 | lines.push(`${icon} ${summaryParts.join(", ")}`); |
| 64 | } |
| 65 | |
| 66 | return lines; |
| 67 | } |