This commit is contained in:
David Anson 2025-08-26 00:11:40 -07:00
parent 124ee3f733
commit 2aeffeb5f1
6 changed files with 84 additions and 58 deletions

View file

@ -622,26 +622,7 @@ comments.
##### options.resultVersion ##### options.resultVersion
Type: `Number` DEPRECATED
Specifies which version of the `result` object to return (see the "Usage"
section below for examples).
Passing a `resultVersion` of `0` corresponds to the original, simple format
where each error is identified by rule name and line number. *Deprecated*
Passing a `resultVersion` of `1` corresponds to a detailed format where each
error includes information about the line number, rule name, alias, description,
as well as any additional detail or context that is available. *Deprecated*
Passing a `resultVersion` of `2` corresponds to a detailed format where each
error includes information about the line number, rule names, description, as
well as any additional detail or context that is available. *Deprecated*
Passing a `resultVersion` of `3` corresponds to the detailed version `2` format
with additional information about how to fix automatically-fixable errors. In
this mode, all errors that occur on each line are reported (other versions
report only the first error for each rule). This is the default behavior.
##### options.strings ##### options.strings

View file

@ -538,3 +538,75 @@ function expandTildePath(file, os) {
return homedir ? file.replace(/^~($|\/|\\)/, `${homedir}$1`) : file; return homedir ? file.replace(/^~($|\/|\\)/, `${homedir}$1`) : file;
} }
module.exports.expandTildePath = expandTildePath; module.exports.expandTildePath = expandTildePath;
/** @typedef {import("../lib/markdownlint.mjs").LintError[]} LintErrors */
/**
* Converts lint errors from resultVersion 3 to 2.
*
* @param {LintErrors} results Lint errors (v3).
* @returns {LintErrors} Lint errors (v2).
*/
function convertResultVersion3To2(results) {
// Remove fixInfo and multiple errors for the same rule and line number
const noPrevious = {
"ruleNames": [],
"lineNumber": -1
};
return results.filter((error, index, array) => {
delete error.fixInfo;
const previous = array[index - 1] || noPrevious;
return (
(error.ruleNames[0] !== previous.ruleNames[0]) ||
(error.lineNumber !== previous.lineNumber)
);
});
};
/**
* Converts lint errors from resultVersion 2 to 1.
*
* @param {LintErrors} results Lint errors (v2).
* @returns {LintErrors} Lint errors (v1).
*/
function convertResultVersion2To1(results) {
for (const error of results) {
// @ts-ignore
error.ruleName = error.ruleNames[0];
// @ts-ignore
error.ruleAlias = error.ruleNames[1] || error.ruleName;
// @ts-ignore
delete error.ruleNames;
}
return results;
};
/**
* Converts lint errors from resultVersion 2 to 0.
*
* @param {LintErrors} results Lint errors (v2).
* @returns {LintErrors} Lint errors (v0).
*/
function convertResultVersion2To0(results) {
const dictionary = {};
for (const error of results) {
const ruleName = error.ruleNames[0];
const ruleLines = dictionary[ruleName] || [];
ruleLines.push(error.lineNumber);
dictionary[ruleName] = ruleLines;
}
// @ts-ignore
return dictionary;
};
module.exports.convertToResultVersion0 = function convertToResultVersion0(results) {
return convertResultVersion2To0(convertResultVersion3To2(results));
};
module.exports.convertToResultVersion1 = function convertToResultVersion1(results) {
return convertResultVersion2To1(convertResultVersion3To2(results));
};
module.exports.convertToResultVersion2 = function convertToResultVersion2(results) {
return convertResultVersion3To2(results);
};

View file

@ -451,7 +451,7 @@ export type Plugin = any[];
*/ */
export type ToStringCallback = (ruleAliases?: boolean) => string; export type ToStringCallback = (ruleAliases?: boolean) => string;
/** /**
* Lint results (for resultVersion 3). * Lint results.
*/ */
export type LintResults = { export type LintResults = {
[x: string]: LintError[]; [x: string]: LintError[];

View file

@ -623,7 +623,6 @@ function lintContent(
const information = errorInfo.information || rule.information; const information = errorInfo.information || rule.information;
results.push({ results.push({
lineNumber, lineNumber,
"ruleName": rule.names[0],
"ruleNames": rule.names, "ruleNames": rule.names,
"ruleDescription": rule.description, "ruleDescription": rule.description,
"ruleInformation": information ? information.href : null, "ruleInformation": information ? information.href : null,
@ -662,45 +661,16 @@ function lintContent(
const formatResults = () => { const formatResults = () => {
// Sort results by rule name by line number // Sort results by rule name by line number
results.sort((a, b) => ( results.sort((a, b) => (
a.ruleName.localeCompare(b.ruleName) || a.ruleNames[0].localeCompare(b.ruleNames[0]) ||
a.lineNumber - b.lineNumber a.lineNumber - b.lineNumber
)); ));
if (resultVersion < 3) { // Deprecated: Convert results to specified resultVersion
// Remove fixInfo and multiple errors for the same rule and line number
const noPrevious = {
"ruleName": null,
"lineNumber": -1
};
results = results.filter((error, index, array) => {
delete error.fixInfo;
const previous = array[index - 1] || noPrevious;
return (
(error.ruleName !== previous.ruleName) ||
(error.lineNumber !== previous.lineNumber)
);
});
}
if (resultVersion === 0) { if (resultVersion === 0) {
// Return a dictionary of rule->[line numbers] results = helpers.convertToResultVersion0(results);
const dictionary = {};
for (const error of results) {
const ruleLines = dictionary[error.ruleName] || [];
ruleLines.push(error.lineNumber);
dictionary[error.ruleName] = ruleLines;
}
// @ts-ignore
results = dictionary;
} else if (resultVersion === 1) { } else if (resultVersion === 1) {
// Use ruleAlias instead of ruleNames results = helpers.convertToResultVersion1(results);
for (const error of results) { } else if (resultVersion === 2) {
error.ruleAlias = error.ruleNames[1] || error.ruleName; results = helpers.convertToResultVersion2(results);
delete error.ruleNames;
}
} else {
// resultVersion 2 or 3: Remove unwanted ruleName
for (const error of results) {
delete error.ruleName;
}
} }
return results; return results;
}; };
@ -1525,7 +1495,7 @@ export function getVersion() {
*/ */
/** /**
* Lint results (for resultVersion 3). * Lint results.
* *
* @typedef {Object.<string, LintError[]>} LintResults * @typedef {Object.<string, LintError[]>} LintResults
* @property {ToStringCallback} toString String representation. * @property {ToStringCallback} toString String representation.

View file

@ -28,6 +28,9 @@ Generated by [AVA](https://avajs.dev).
'clearHtmlCommentText', 'clearHtmlCommentText',
'cloneIfArray', 'cloneIfArray',
'cloneIfUrl', 'cloneIfUrl',
'convertToResultVersion0',
'convertToResultVersion1',
'convertToResultVersion2',
'default', 'default',
'ellipsify', 'ellipsify',
'endOfLineGemojiCodeRe', 'endOfLineGemojiCodeRe',