Files
hyperframes/packages/cli/src/utils/lintFormat.ts
T
Miguel Ángel d42bbd67f4 fix: warnings that name project files print control characters as visible escapes (#7863)
* fix: warnings that name project files print control characters as visible escapes

The CLI already escaped control characters in the text its own commands
print, but warnings from core, producer and engine went to the terminal
raw. A composition whose sub-composition path held ESC or BEL still
drove the terminal through the bundler's "Skipping sub-composition"
warning in `info --media` and `snapshot`.

terminalText moves to core so there is one sanitiser, the CLI imports it
from there, and the library warnings that carry project text (the
bundler, the producer logger, the file server, render path skips, asset
load failures, FrameCapture, the import map, fonts, figma and atomic
file warnings) pass that text through it.

* fix: every library warning that carries project text escapes control characters
2026-11-16 04:56:36 +00:00

111 lines
4.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { terminalText } from "@hyperframes/core";
import { c } from "../ui/colors.js";
import type { ProjectLintResult } from "./lintProject.js";
export interface LintFormatOptions {
/** Show elementId in brackets after the code (default: true) */
showElementId?: boolean;
/** Show summary line with error/warning counts (default: false) */
showSummary?: boolean;
/** Group errors before warnings per file (default: false — interleaved) */
errorsFirst?: boolean;
/** Include info-level findings in output (default: false — only errors/warnings) */
verbose?: boolean;
}
/**
* Format lint findings for console output. Used by lint, render, and preview commands.
*/
export function formatLintFindings(
{ results, totalErrors, totalWarnings, totalInfos }: ProjectLintResult,
options: LintFormatOptions = {},
): string[] {
const {
showElementId = true,
showSummary = false,
errorsFirst = false,
verbose = false,
} = options;
const lines: string[] = [];
const multiFile = results.length > 1;
for (const { file, result } of results) {
if (result.findings.length === 0) continue;
const format = (finding: (typeof result.findings)[0]) => {
if (!verbose && finding.severity === "info") return;
const prefix =
finding.severity === "error"
? c.error("✗")
: finding.severity === "warning"
? c.warn("⚠")
: c.dim("ℹ");
const fileLabel = findingFileLabel(file, finding, multiFile);
const loc =
showElementId && finding.elementId
? ` ${c.accent(`[${terminalText(finding.elementId)}]`)}`
: "";
const message = terminalText(finding.message);
lines.push(` ${prefix} ${fileLabel}${c.bold(finding.code)}${loc}: ${message}`);
if (finding.fixHint) lines.push(` ${c.dim(`Fix: ${terminalText(finding.fixHint)}`)}`);
};
if (errorsFirst) {
for (const f of result.findings) if (f.severity === "error") format(f);
for (const f of result.findings) if (f.severity === "warning") format(f);
if (verbose) for (const f of result.findings) if (f.severity === "info") format(f);
} else {
for (const f of result.findings) format(f);
}
}
if (showSummary) {
const icon = totalErrors > 0 ? c.error("◇") : c.success("◇");
lines.push("");
lines.push(`${icon} ${formatLintCounts(totalErrors, totalWarnings, totalInfos, verbose)}`);
}
return lines;
}
function formatLintCounts(
totalErrors: number,
totalWarnings: number,
totalInfos: number,
verbose: boolean,
): string {
const parts = [`${totalErrors} error(s)`, `${totalWarnings} warning(s)`];
if (verbose && totalInfos > 0) parts.push(`${totalInfos} info(s)`);
return parts.join(", ");
}
/** Full findings in verbose mode, a one-line summary otherwise. `pointer` (summary
* mode) picks the hint: both name the lint command, "studio" also the Studio badge. --lint-verbose is
* not named: only render, preview and publish take it, and agents tried it on lint and check. */
export type LintMessageMode =
| { kind: "verbose"; options?: LintFormatOptions }
| { kind: "summary"; pointer?: "cli" | "studio" };
export function formatLintStartupMessage(
lintResult: ProjectLintResult,
mode: LintMessageMode,
): string[] {
if (mode.kind === "verbose") return formatLintFindings(lintResult, mode.options);
const counts = formatLintCounts(lintResult.totalErrors, lintResult.totalWarnings, 0, false);
const hint =
mode.pointer === "studio"
? "see the Lint badge in Studio, or run `hyperframes lint` for full output"
: "run `hyperframes lint` for full output";
return [` Lint: ${counts} — ${hint}.`];
}
function findingFileLabel(
file: string,
finding: import("@hyperframes/lint").HyperframeLintFinding,
multiFile: boolean,
): string {
if (finding.line === undefined) return multiFile ? c.dim(`[${terminalText(file)}] `) : "";
const column = finding.column === undefined ? "" : `:${finding.column}`;
return c.dim(`[${terminalText(finding.file ?? file)}:${finding.line}${column}] `);
}