This commit is contained in:
David Anson 2025-09-06 17:59:11 -07:00
parent 0117a71138
commit 29fd76ff8e
12 changed files with 48 additions and 174 deletions

View file

@ -554,6 +554,7 @@ function convertLintErrorsVersion3To2(errors) {
"lineNumber": -1
};
return errors.filter((error, index, array) => {
// @ts-ignore
delete error.fixInfo;
const previous = array[index - 1] || noPrevious;
return (
@ -646,3 +647,29 @@ module.exports.convertToResultVersion1 = function convertToResultVersion1(result
module.exports.convertToResultVersion2 = function convertToResultVersion2(results) {
return copyAndTransformResults(results, convertLintErrorsVersion3To2);
};
/**
* Formats lint results to an array of strings.
*
* @param {LintResults|undefined} lintResults Lint results.
* @returns {string[]} Lint error strings.
*/
module.exports.formatLintResults = function formatLintResults(lintResults) {
const results = [];
for (const [ source, lintErrors ] of Object.entries(lintResults || {})) {
for (const lintError of lintErrors) {
results.push(
source + ": " +
lintError.lineNumber + ": " +
lintError.ruleNames.join("/") + " " +
lintError.ruleDescription +
(lintError.errorDetail ?
" [" + lintError.errorDetail + "]" :
"") +
(lintError.errorContext ?
" [Context: \"" + lintError.errorContext + "\"]" :
""));
}
}
return results;
};

View file

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

View file

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

View file

@ -76,21 +76,6 @@ export function applyFix(line: string, fixInfo: FixInfo, lineEnding?: string): s
* @returns {string} Fixed content.
*/
export function applyFixes(input: string, errors: LintError[]): 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.
*

View file

@ -1304,44 +1304,6 @@ export function applyFixes(input, errors) {
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.
*

View file

@ -5,7 +5,7 @@ import path from "node:path";
import test from "ava";
import { characterEntities } from "character-entities";
import { gemoji } from "gemoji";
import helpers from "../helpers/helpers.cjs";
import helpers, { formatLintResults } from "../helpers/helpers.cjs";
import { lint } from "markdownlint/promise";
import { forEachInlineCodeSpan } from "../lib/markdownit.cjs";
import { getReferenceLinkImageData } from "../lib/cache.mjs";
@ -529,3 +529,17 @@ test("hasOverlap", (t) => {
t.false(helpers.hasOverlap(rangeB, rangeA), JSON.stringify({ rangeB, rangeA }));
}
});
test("formatLintResults", async(t) => {
t.plan(2);
t.deepEqual(formatLintResults(undefined), []);
const lintResults = await lint({ "strings": { "content": "# Heading <br/>" } });
t.deepEqual(
formatLintResults(lintResults),
[
"content: 1: MD019/no-multiple-space-atx Multiple spaces after hash on atx style heading [Context: \"# Heading <br/>\"]",
"content: 1: MD033/no-inline-html Inline HTML [Element: br]",
"content: 1: MD047/single-trailing-newline Files should end with a single newline character"
]
);
});

View file

@ -5,6 +5,7 @@ const { join } = path.posix;
import { globby } from "globby";
import jsoncParser from "jsonc-parser";
import jsYaml from "js-yaml";
import { formatLintResults } from "markdownlint/helpers";
import { lint, readConfig } from "markdownlint/promise";
import { markdownlintParallel } from "./markdownlint-test-parallel.mjs";
@ -49,9 +50,8 @@ export function lintTestRepo(t, globPatterns, configPath, configOverrides, paral
files,
config
}).then((results) => {
const resultsString = results.toString();
t.snapshot(
resultsString,
formatLintResults(results).join("\n"),
"Expected linting violations"
);
});

View file

@ -13,7 +13,7 @@ import pluginInline from "markdown-it-for-inline";
import pluginSub from "markdown-it-sub";
import pluginSup from "markdown-it-sup";
import test from "ava";
import { formatLintError, getVersion, parseLintErrorString } from "markdownlint";
import { getVersion } from "markdownlint";
import { lint as lintAsync } from "markdownlint/async";
import { lint as lintPromise } from "markdownlint/promise";
import { lint as lintSync } from "markdownlint/sync";
@ -1389,32 +1389,6 @@ test("getVersion", (t) => {
t.is(actual, expected, "Version string not correct.");
});
test("formatLintError-parseLintErrorString", async(t) => {
t.plan(29);
t.is(null, parseLintErrorString(""));
const content = "# Heading\nText `code ` text \n\n";
const options = {
"strings": {
"relative/path/file name.md": content
}
};
const results = await lintPromise(options);
for (const source of Object.keys(results)) {
for (const lintError of results[source]) {
const formatted = formatLintError(source, lintError);
const parsed = parseLintErrorString(formatted);
t.is(parsed.source, source);
t.is(parsed.line, lintError.lineNumber.toString());
t.is(Boolean(parsed.column), Boolean(lintError.errorRange));
if (lintError.errorRange) {
t.is(parsed.column, lintError.errorRange[0].toString());
}
t.is(parsed.rule, lintError.ruleNames.join("/"));
t.is(parsed.message.replaceAll(/ \[[^\]]+\]/g, ""), lintError.ruleDescription);
}
}
});
test("constants", (t) => {
t.plan(2);
// @ts-ignore

View file

@ -12,9 +12,7 @@ Generated by [AVA](https://avajs.dev).
markdownlint: [
'applyFix',
'applyFixes',
'formatLintError',
'getVersion',
'parseLintErrorString',
'resolveModule',
],
'markdownlint/async': [
@ -39,6 +37,7 @@ Generated by [AVA](https://avajs.dev).
'endOfLineHtmlEntityRe',
'escapeForRegExp',
'expandTildePath',
'formatLintResults',
'frontMatterHasTitle',
'frontMatterRe',
'getHtmlAttributeRe',

View file

@ -1,87 +0,0 @@
# Snapshot report for `test/markdownlint-test.mjs`
The actual snapshot is saved in `markdownlint-test.mjs.snap`.
Generated by [AVA](https://avajs.dev).
## problemMatcher
> Snapshot 1
[
{
column: undefined,
description: 'Trailing spaces [Expected: 0 or 2; Actual: 1]',
line: '2',
rule: 'MD009/no-trailing-spaces',
source: 'relative/path/file name.md',
},
{
column: undefined,
description: 'Multiple spaces after hash on atx style heading [Context: "# Heading"]',
line: '1',
rule: 'MD019/no-multiple-space-atx',
source: 'relative/path/file name.md',
},
{
column: undefined,
description: 'Headings should be surrounded by blank lines [Expected: 1; Actual: 0; Below] [Context: "# Heading"]',
line: '1',
rule: 'MD022/blanks-around-headings',
source: 'relative/path/file name.md',
},
{
column: undefined,
description: 'Spaces inside code span elements [Context: "`code `"]',
line: '2',
rule: 'MD038/no-space-in-code',
source: 'relative/path/file name.md',
},
{
column: undefined,
description: 'Files should end with a single newline character',
line: '2',
rule: 'MD047/single-trailing-newline',
source: 'relative/path/file name.md',
},
]
> Snapshot 2
[
{
column: '3',
description: 'Multiple spaces after hash on atx style heading [Context: "# Heading"]',
line: '1',
rule: 'MD019/no-multiple-space-atx',
source: 'relative/path/file name.md',
},
{
column: undefined,
description: 'Headings should be surrounded by blank lines [Expected: 1; Actual: 0; Below] [Context: "# Heading"]',
line: '1',
rule: 'MD022/blanks-around-headings',
source: 'relative/path/file name.md',
},
{
column: '18',
description: 'Trailing spaces [Expected: 0 or 2; Actual: 1]',
line: '2',
rule: 'MD009/no-trailing-spaces',
source: 'relative/path/file name.md',
},
{
column: '11',
description: 'Spaces inside code span elements [Context: "`code `"]',
line: '2',
rule: 'MD038/no-space-in-code',
source: 'relative/path/file name.md',
},
{
column: '18',
description: 'Files should end with a single newline character',
line: '2',
rule: 'MD047/single-trailing-newline',
source: 'relative/path/file name.md',
},
]