Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396/** Per-source pipeline shared by the path runner and the standard-input runner: rule selection, per-file LFS context, lint or fixpoint, and report rendering.
@module */
import { caughtValueText, } from '@monochromatic-dev/module-caught-value/ts';
import { fixSource, } from './fix.ts';import { type LfsImageContext, type LfsImageRepo, prepareLfsImageContext,} from './lfs-image-context.ts';import { runRules, } from './lint.ts';import { report, type FileReport, type ReporterName,} from './reporters.ts';import { rules, } from './rule/index.ts';import type { Diagnostic, Rule,} from './types.ts';
/** Synthetic rule ID for a file that could not be processed. Reporting it as a diagnostic lets one bad file fail the run without aborting sibling writes. */const PROCESSING_ERROR_RULE_ID = 'markdown-lint-error';
/** Synthetic rule ID for safety checks that block a risky autofix write. */const SAFETY_RULE_ID = 'markdown-lint-safety';
/** Parameters for {@link fileStartDiagnostic}. */type FileStartDiagnosticParams = { /** Synthetic rule identifier. */ readonly ruleId: string; /** Diagnostic message. */ readonly message: string;};
/** Order diagnostics by source position so the report reads top-to-bottom even though rules run in their own order. @param diagnostics - diagnostics for one file @returns diagnostics sorted by line then column */function byPosition(diagnostics: readonly Diagnostic[],): readonly Diagnostic[] { return diagnostics.toSorted(function compare( left: Diagnostic, right: Diagnostic, ): number { return (left.line - right.line) || (left.column - right.column); },);}
/** Create a synthetic diagnostic at the start of a file. @param ruleId - synthetic rule identifier @param message - diagnostic message @returns diagnostic anchored at line 1, column 1 */function fileStartDiagnostic({ ruleId, message,}: FileStartDiagnosticParams,): Diagnostic { return { ruleId, message, line: 1, column: 1, };}
/** Diagnostic for a file-processing failure. @param error - value caught from file processing @returns synthetic diagnostic for the report @example ```ts processingErrorDiagnostic(new Error('EACCES')); // { ruleId: 'markdown-lint-error', line: 1, column: 1, ... } ``` @mutates error - `caughtValueText` may invoke string-conversion hooks. */export function processingErrorDiagnostic(error: unknown,): Diagnostic { /** Error text preserving empty-message class names from existing CLI output. */ const errorText = Error.isError(error,) && (error.message === '') ? error.constructor .name : caughtValueText(error,); return fileStartDiagnostic({ ruleId: PROCESSING_ERROR_RULE_ID, message: `Could not process file: ${errorText}`, },);}
/** Diagnostic for a fixpoint result that would erase a non-empty file. @returns synthetic diagnostic for the report */function emptyRewriteDiagnostic(): Diagnostic { return fileStartDiagnostic({ ruleId: SAFETY_RULE_ID, message: 'Autofix would replace non-empty file with empty output; leaving file unchanged.', },);}
/** Rules selected by id; every rule when `ruleIds` is empty.
@param ruleIds - rule ids to keep
@returns rules in registry order
@example ```ts selectRules(['lfs-image-url']); // just that rule selectRules([]); // every rule ``` */export function selectRules(ruleIds: readonly string[],): readonly Rule[] { if (ruleIds.length === 0) { return rules; } return rules.filter(function selected(rule: Rule,): boolean { return ruleIds.includes(rule.id,); },);}
/** Parameters for {@link lfsContextsFor}. */type LfsContextsForParams = { /** Zero or one repository descriptions from discovery. */ readonly repos: readonly LfsImageRepo[]; /** Absolute path of the file under lint. */ readonly filePath: string; /** Source of the file under lint. */ readonly source: string; /** Whether the source is MDX. */ readonly mdx: boolean;};
/** Per-file LFS context as a one-element list, or empty when the run found no repository or the file is excluded.
@param repos - zero or one repository descriptions from discovery
@param filePath - absolute path of the file under lint
@param source - source of the file under lint
@param mdx - whether the source is MDX
@returns zero or one contexts */async function lfsContextsFor({ repos, filePath, source, mdx,}: LfsContextsForParams,): Promise<readonly LfsImageContext[]> { return await Promise.all(repos .filter(function includesFile(repo: LfsImageRepo,): boolean { return !repo.isExcluded(filePath,); },) .map(async function contextOf(repo: LfsImageRepo,): Promise<LfsImageContext> { return await prepareLfsImageContext({ repo, filePath, source, mdx, },); },),);}
/** Parameters for {@link processSource}. */export type ProcessSourceParams = { /** Source under lint. */ readonly source: string; /** Absolute path the source lives at (or is treated as living at). */ readonly filePath: string; /** Path shown in the report. */ readonly displayPath: string; /** Whether the source is MDX. */ readonly mdx: boolean; /** Whether to run the fixpoint loop. */ readonly fix: boolean; /** Rules to run. */ readonly selectedRules: readonly Rule[]; /** Zero or one repository descriptions for the `lfs-image-url` rule. */ readonly lfsRepos: readonly LfsImageRepo[];};
/** One source after linting or fixing. */export type ProcessedSource = { /** Per-file report. */ readonly report: FileReport; /** Source after fixes; the input when nothing changed or `fix` was off. */ readonly fixedSource: string;};
/** Lint one source, or run it through the fixpoint loop. A fix that would erase a non-empty source is refused and reported instead.
@param source - source under lint
@param filePath - absolute path the source lives at
@param displayPath - path shown in the report
@param mdx - whether the source is MDX
@param fix - whether to run the fixpoint loop
@param selectedRules - rules to run
@param lfsRepos - zero or one repository descriptions for the `lfs-image-url` rule
@returns report and the (possibly fixed) source
@example ```ts await processSource({ source, filePath, displayPath: 'README.md', mdx: false, fix: true, selectedRules: rules, lfsRepos: [] }); ``` */export async function processSource({ source, filePath, displayPath, mdx, fix, selectedRules, lfsRepos,}: ProcessSourceParams,): Promise<ProcessedSource> { /** Per-file LFS context, when the rule applies to this file. */ const [lfs,] = await lfsContextsFor({ repos: lfsRepos, filePath, source, mdx, },); /** Context spread for the rule runs below. */ const lfsSpread = lfs === undefined ? {} : { lfs, }; if (!fix) { return { report: { path: displayPath, diagnostics: byPosition(runRules({ rules: selectedRules, source, mdx, ...lfsSpread, },),), }, fixedSource: source, }; } /** Source and remaining diagnostics after the fixpoint loop. */ const fixed = fixSource({ rules: selectedRules, source, mdx, ...lfsSpread, },); if ((source !== '') && (fixed.source === '')) { return { report: { path: displayPath, diagnostics: [emptyRewriteDiagnostic(),], }, fixedSource: source, }; } return { report: { path: displayPath, diagnostics: byPosition(fixed.diagnostics,), }, fixedSource: fixed.source, };}
/** Render per-file reports that carry diagnostics, sorted by path.
@param reporter - reporter for the rendered output
@param fileReports - every per-file report
@returns rendered output and whether any diagnostics remain
@example ```ts renderReports({ reporter: 'json', fileReports }); // { output: '[...]', hadViolations: true } ``` */export function renderReports({ reporter, fileReports,}: { readonly reporter: ReporterName; readonly fileReports: readonly FileReport[];},): { readonly output: string; readonly hadViolations: boolean} { /** Reports that actually carry diagnostics, sorted by path. */ const reports = fileReports .filter(function hasDiagnostics(fileReport: FileReport,): boolean { return fileReport.diagnostics .length > 0; },) .toSorted(function byPath( left: FileReport, right: FileReport, ): number { return left.path < right.path ? -1 : left.path > right.path ? 1 : 0; },); return { output: report({ reporter, files: reports, },), hadViolations: reports.length > 0, };}