wip
Some checks are pending
Checkers / linkcheck (push) Waiting to run
Checkers / spellcheck (push) Waiting to run
CI / build (20, macos-latest) (push) Waiting to run
CI / build (20, ubuntu-latest) (push) Waiting to run
CI / build (20, windows-latest) (push) Waiting to run
CI / build (22, macos-latest) (push) Waiting to run
CI / build (22, ubuntu-latest) (push) Waiting to run
CI / build (22, windows-latest) (push) Waiting to run
CI / build (24, macos-latest) (push) Waiting to run
CI / build (24, ubuntu-latest) (push) Waiting to run
CI / build (24, windows-latest) (push) Waiting to run
CI / pnpm (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
TestRepos / build (latest, ubuntu-latest) (push) Waiting to run
UpdateTestRepos / update (push) Waiting to run

This commit is contained in:
David Anson 2025-09-04 22:19:42 -07:00
parent 697b04834f
commit 546cf1c08b
7 changed files with 81 additions and 36 deletions

View file

@ -26,5 +26,4 @@ export type RuleOnErrorFixInfo = import("./markdownlint.mjs").RuleOnErrorFixInfo
export type RuleOnErrorFixInfoNormalized = import("./markdownlint.mjs").RuleOnErrorFixInfoNormalized; export type RuleOnErrorFixInfoNormalized = import("./markdownlint.mjs").RuleOnErrorFixInfoNormalized;
export type RuleOnErrorInfo = import("./markdownlint.mjs").RuleOnErrorInfo; export type RuleOnErrorInfo = import("./markdownlint.mjs").RuleOnErrorInfo;
export type RuleParams = import("./markdownlint.mjs").RuleParams; export type RuleParams = import("./markdownlint.mjs").RuleParams;
export type ToStringCallback = import("./markdownlint.mjs").ToStringCallback; export { applyFix, applyFixes, formatLintError, getVersion, parseLintErrorString } from "./markdownlint.mjs";
export { applyFix, applyFixes, getVersion } from "./markdownlint.mjs";

View file

@ -1,6 +1,6 @@
// @ts-check // @ts-check
export { applyFix, applyFixes, getVersion } from "./markdownlint.mjs"; export { applyFix, applyFixes, formatLintError, getVersion, parseLintErrorString } from "./markdownlint.mjs";
export { resolveModule } from "./resolve-module.cjs"; export { resolveModule } from "./resolve-module.cjs";
/** @typedef {import("./markdownlint.mjs").Configuration} Configuration */ /** @typedef {import("./markdownlint.mjs").Configuration} Configuration */
@ -30,4 +30,3 @@ export { resolveModule } from "./resolve-module.cjs";
/** @typedef {import("./markdownlint.mjs").RuleOnErrorFixInfoNormalized} RuleOnErrorFixInfoNormalized */ /** @typedef {import("./markdownlint.mjs").RuleOnErrorFixInfoNormalized} RuleOnErrorFixInfoNormalized */
/** @typedef {import("./markdownlint.mjs").RuleOnErrorInfo} RuleOnErrorInfo */ /** @typedef {import("./markdownlint.mjs").RuleOnErrorInfo} RuleOnErrorInfo */
/** @typedef {import("./markdownlint.mjs").RuleParams} RuleParams */ /** @typedef {import("./markdownlint.mjs").RuleParams} RuleParams */
/** @typedef {import("./markdownlint.mjs").ToStringCallback} ToStringCallback */

View file

@ -76,6 +76,21 @@ export function applyFix(line: string, fixInfo: RuleOnErrorFixInfo, lineEnding?:
* @returns {string} Fixed content. * @returns {string} Fixed content.
*/ */
export function applyFixes(input: string, errors: RuleOnErrorInfo[]): string; export function applyFixes(input: string, errors: RuleOnErrorInfo[]): string;
/**
* TBD
*
* @param {string} source Source file name or identifier.
* @param {LintError} lintError Lint error.
* @returns {string} Lint error string.
*/
export function formatLintError(source: string, lintError: LintError): string;
/**
* TBD
*
* @param {string} lintErrorString Lint error string.
* @returns {Object|null} Lint error (if valid).
*/
export function parseLintErrorString(lintErrorString: string): any | null;
/** /**
* Gets the (semantic) version of the library. * Gets the (semantic) version of the library.
* *
@ -442,10 +457,6 @@ export type Options = {
* A markdown-it plugin. * A markdown-it plugin.
*/ */
export type Plugin = any[]; export type Plugin = any[];
/**
* Function to pretty-print lint results.
*/
export type ToStringCallback = () => string;
/** /**
* Lint results. * Lint results.
*/ */
@ -483,11 +494,11 @@ export type LintError = {
/** /**
* Column number (1-based) and length. * Column number (1-based) and length.
*/ */
errorRange: number[]; errorRange: number[] | null;
/** /**
* Fix information. * Fix information.
*/ */
fixInfo?: FixInfo; fixInfo: FixInfo | null;
}; };
/** /**
* Fix information. * Fix information.

View file

@ -522,6 +522,7 @@ function lintContent(
"config": null "config": null
}); });
// Function to run for each rule // Function to run for each rule
/** @type {LintError[]} */
const results = []; const results = [];
/** /**
* @param {Rule} rule Rule. * @param {Rule} rule Rule.
@ -1303,6 +1304,44 @@ export function applyFixes(input, errors) {
return lines.filter((line) => line !== null).join(lineEnding); return lines.filter((line) => line !== null).join(lineEnding);
} }
/**
* TBD
*
* @param {string} source Source file name or identifier.
* @param {LintError} lintError Lint error.
* @returns {string} Lint error string.
*/
export function formatLintError(source, lintError) {
const { lineNumber, ruleNames, ruleDescription, errorDetail, errorContext, errorRange } = lintError;
const ruleName = ruleNames.join("/");
const description = ruleDescription +
(errorDetail ? ` [${errorDetail}]` : "") +
(errorContext ? ` [Context: "${errorContext}"]` : "");
const column = (errorRange && errorRange[0]) || 0;
const columnText = column ? `:${column}` : "";
return `${source}:${lineNumber}${columnText} ${ruleName} ${description}`;
}
/**
* TBD
*
* @param {string} lintErrorString Lint error string.
* @returns {Object|null} Lint error (if valid).
*/
export function parseLintErrorString(lintErrorString) {
const matchRe = /^([^:]+):\s*(\d+)(?::(\d+))?:?\s(\S+)\s(.+)$/;
const match = matchRe.exec(lintErrorString);
return match ?
{
"source": match[1],
"line": match[2],
"column": match[3],
"rule": match[4],
"message": match[5]
} :
null;
}
/** /**
* Gets the (semantic) version of the library. * Gets the (semantic) version of the library.
* *
@ -1492,18 +1531,10 @@ export function getVersion() {
* @typedef {Array} Plugin * @typedef {Array} Plugin
*/ */
/**
* Function to pretty-print lint results.
*
* @callback ToStringCallback
* @returns {string} Pretty-printed results.
*/
/** /**
* Lint results. * Lint results.
* *
* @typedef {Object.<string, LintError[]>} LintResults * @typedef {Object.<string, LintError[]>} LintResults
* @property {ToStringCallback} toString String representation.
*/ */
/** /**
@ -1516,8 +1547,8 @@ export function getVersion() {
* @property {string} ruleInformation Link to more information. * @property {string} ruleInformation Link to more information.
* @property {string} errorDetail Detail about the error. * @property {string} errorDetail Detail about the error.
* @property {string} errorContext Context for the error. * @property {string} errorContext Context for the error.
* @property {number[]} errorRange Column number (1-based) and length. * @property {number[]|null} errorRange Column number (1-based) and length.
* @property {FixInfo} [fixInfo] Fix information. * @property {FixInfo|null} fixInfo Fix information.
*/ */
/** /**

View file

@ -13,7 +13,7 @@ import pluginInline from "markdown-it-for-inline";
import pluginSub from "markdown-it-sub"; import pluginSub from "markdown-it-sub";
import pluginSup from "markdown-it-sup"; import pluginSup from "markdown-it-sup";
import test from "ava"; import test from "ava";
import { getVersion } from "markdownlint"; import { formatLintError, getVersion, parseLintErrorString } from "markdownlint";
import { lint as lintAsync } from "markdownlint/async"; import { lint as lintAsync } from "markdownlint/async";
import { lint as lintPromise } from "markdownlint/promise"; import { lint as lintPromise } from "markdownlint/promise";
import { lint as lintSync } from "markdownlint/sync"; import { lint as lintSync } from "markdownlint/sync";
@ -1392,27 +1392,30 @@ test("getVersion", (t) => {
t.is(actual, expected, "Version string not correct."); t.is(actual, expected, "Version string not correct.");
}); });
const matcherRe = /^(?<source>[^:]+):\s*(?<line>\d+)(?::(?<column>\d+))?:?\s(?<rule>\S+)\s(?<description>.+)$/; test("formatLintError-parseLintErrorString", async(t) => {
t.plan(29);
test("problemMatcher", async(t) => { t.is(null, parseLintErrorString(""));
t.plan(2); const content = "# Heading\nText `code ` text \n\n";
const content = "# Heading\nText `code ` text ";
const options = { const options = {
"strings": { "strings": {
"relative/path/file name.md": content "relative/path/file name.md": content
} }
}; };
const results = await lintPromise(options); const results = await lintPromise(options);
const getMatches = (input) => input.split("\n").map((line) => matcherRe.exec(line)?.groups); for (const source of Object.keys(results)) {
t.snapshot(getMatches(results.toString())); for (const lintError of results[source]) {
// eslint-disable-next-line camelcase const formatted = formatLintError(source, lintError);
const cli_0_45_0__cli2_0_18_1 = const parsed = parseLintErrorString(formatted);
`relative/path/file name.md:1:3 MD019/no-multiple-space-atx Multiple spaces after hash on atx style heading [Context: "# Heading"] t.is(parsed.source, source);
relative/path/file name.md:1 MD022/blanks-around-headings Headings should be surrounded by blank lines [Expected: 1; Actual: 0; Below] [Context: "# Heading"] t.is(parsed.line, lintError.lineNumber.toString());
relative/path/file name.md:2:18 MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 1] t.is(Boolean(parsed.column), Boolean(lintError.errorRange));
relative/path/file name.md:2:11 MD038/no-space-in-code Spaces inside code span elements [Context: "\`code \`"] if (lintError.errorRange) {
relative/path/file name.md:2:18 MD047/single-trailing-newline Files should end with a single newline character`; t.is(parsed.column, lintError.errorRange[0].toString());
t.snapshot(getMatches(cli_0_45_0__cli2_0_18_1)); }
t.is(parsed.rule, lintError.ruleNames.join("/"));
t.is(parsed.message.replaceAll(/ \[[^\]]+\]/g, ""), lintError.ruleDescription);
}
}
}); });
test("constants", (t) => { test("constants", (t) => {

View file

@ -12,7 +12,9 @@ Generated by [AVA](https://avajs.dev).
markdownlint: [ markdownlint: [
'applyFix', 'applyFix',
'applyFixes', 'applyFixes',
'formatLintError',
'getVersion', 'getVersion',
'parseLintErrorString',
'resolveModule', 'resolveModule',
], ],
'markdownlint/async': [ 'markdownlint/async': [