69 lines
2.0 KiB
TypeScript
69 lines
2.0 KiB
TypeScript
import { sanitizeTerminalText } from "../terminal/safe-text.js";
|
|
import type { ConfigValidationIssue } from "./types.js";
|
|
|
|
type ConfigIssueLineInput = {
|
|
path?: string | null;
|
|
message: string;
|
|
};
|
|
|
|
type ConfigIssueFormatOptions = {
|
|
normalizeRoot?: boolean;
|
|
};
|
|
|
|
export function normalizeConfigIssuePath(path: string | null | undefined): string {
|
|
if (typeof path !== "string") {
|
|
return "<root>";
|
|
}
|
|
const trimmed = path.trim();
|
|
return trimmed ? trimmed : "<root>";
|
|
}
|
|
|
|
export function normalizeConfigIssue(issue: ConfigValidationIssue): ConfigValidationIssue {
|
|
const hasAllowedValues = Array.isArray(issue.allowedValues) && issue.allowedValues.length > 0;
|
|
return {
|
|
path: normalizeConfigIssuePath(issue.path),
|
|
message: issue.message,
|
|
...(hasAllowedValues ? { allowedValues: issue.allowedValues } : {}),
|
|
...(hasAllowedValues &&
|
|
typeof issue.allowedValuesHiddenCount === "number" &&
|
|
issue.allowedValuesHiddenCount > 0
|
|
? { allowedValuesHiddenCount: issue.allowedValuesHiddenCount }
|
|
: {}),
|
|
};
|
|
}
|
|
|
|
export function normalizeConfigIssues(
|
|
issues: ReadonlyArray<ConfigValidationIssue>,
|
|
): ConfigValidationIssue[] {
|
|
return issues.map((issue) => normalizeConfigIssue(issue));
|
|
}
|
|
|
|
function resolveIssuePathForLine(
|
|
path: string | null | undefined,
|
|
opts?: ConfigIssueFormatOptions,
|
|
): string {
|
|
if (opts?.normalizeRoot) {
|
|
return normalizeConfigIssuePath(path);
|
|
}
|
|
return typeof path === "string" ? path : "";
|
|
}
|
|
|
|
export function formatConfigIssueLine(
|
|
issue: ConfigIssueLineInput,
|
|
marker = "-",
|
|
opts?: ConfigIssueFormatOptions,
|
|
): string {
|
|
const prefix = marker ? `${marker} ` : "";
|
|
const path = sanitizeTerminalText(resolveIssuePathForLine(issue.path, opts));
|
|
const message = sanitizeTerminalText(issue.message);
|
|
return `${prefix}${path}: ${message}`;
|
|
}
|
|
|
|
export function formatConfigIssueLines(
|
|
issues: ReadonlyArray<ConfigIssueLineInput>,
|
|
marker = "-",
|
|
opts?: ConfigIssueFormatOptions,
|
|
): string[] {
|
|
return issues.map((issue) => formatConfigIssueLine(issue, marker, opts));
|
|
}
|