diff --git a/README.md b/README.md index 9a415fd5..63f43c14 100644 --- a/README.md +++ b/README.md @@ -622,26 +622,7 @@ comments. ##### options.resultVersion -Type: `Number` - -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. +DEPRECATED ##### options.strings diff --git a/helpers/helpers.cjs b/helpers/helpers.cjs index 11821eef..81a6a854 100644 --- a/helpers/helpers.cjs +++ b/helpers/helpers.cjs @@ -538,3 +538,75 @@ function expandTildePath(file, os) { return homedir ? file.replace(/^~($|\/|\\)/, `${homedir}$1`) : file; } 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); +}; diff --git a/lib/markdownlint.d.mts b/lib/markdownlint.d.mts index d720a3f9..b0bd3dc9 100644 --- a/lib/markdownlint.d.mts +++ b/lib/markdownlint.d.mts @@ -451,7 +451,7 @@ export type Plugin = any[]; */ export type ToStringCallback = (ruleAliases?: boolean) => string; /** - * Lint results (for resultVersion 3). + * Lint results. */ export type LintResults = { [x: string]: LintError[]; diff --git a/lib/markdownlint.mjs b/lib/markdownlint.mjs index e796f074..1e9b1db4 100644 --- a/lib/markdownlint.mjs +++ b/lib/markdownlint.mjs @@ -623,7 +623,6 @@ function lintContent( const information = errorInfo.information || rule.information; results.push({ lineNumber, - "ruleName": rule.names[0], "ruleNames": rule.names, "ruleDescription": rule.description, "ruleInformation": information ? information.href : null, @@ -662,45 +661,16 @@ function lintContent( const formatResults = () => { // Sort results by rule name by line number results.sort((a, b) => ( - a.ruleName.localeCompare(b.ruleName) || + a.ruleNames[0].localeCompare(b.ruleNames[0]) || a.lineNumber - b.lineNumber )); - if (resultVersion < 3) { - // 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) - ); - }); - } + // Deprecated: Convert results to specified resultVersion if (resultVersion === 0) { - // Return a dictionary of rule->[line numbers] - const dictionary = {}; - for (const error of results) { - const ruleLines = dictionary[error.ruleName] || []; - ruleLines.push(error.lineNumber); - dictionary[error.ruleName] = ruleLines; - } - // @ts-ignore - results = dictionary; + results = helpers.convertToResultVersion0(results); } else if (resultVersion === 1) { - // Use ruleAlias instead of ruleNames - for (const error of results) { - error.ruleAlias = error.ruleNames[1] || error.ruleName; - delete error.ruleNames; - } - } else { - // resultVersion 2 or 3: Remove unwanted ruleName - for (const error of results) { - delete error.ruleName; - } + results = helpers.convertToResultVersion1(results); + } else if (resultVersion === 2) { + results = helpers.convertToResultVersion2(results); } return results; }; @@ -1525,7 +1495,7 @@ export function getVersion() { */ /** - * Lint results (for resultVersion 3). + * Lint results. * * @typedef {Object.} LintResults * @property {ToStringCallback} toString String representation. diff --git a/test/snapshots/markdownlint-test-exports.mjs.md b/test/snapshots/markdownlint-test-exports.mjs.md index 1f86f881..f03e1f08 100644 --- a/test/snapshots/markdownlint-test-exports.mjs.md +++ b/test/snapshots/markdownlint-test-exports.mjs.md @@ -28,6 +28,9 @@ Generated by [AVA](https://avajs.dev). 'clearHtmlCommentText', 'cloneIfArray', 'cloneIfUrl', + 'convertToResultVersion0', + 'convertToResultVersion1', + 'convertToResultVersion2', 'default', 'ellipsify', 'endOfLineGemojiCodeRe', diff --git a/test/snapshots/markdownlint-test-exports.mjs.snap b/test/snapshots/markdownlint-test-exports.mjs.snap index a21e64a2..bf686f39 100644 Binary files a/test/snapshots/markdownlint-test-exports.mjs.snap and b/test/snapshots/markdownlint-test-exports.mjs.snap differ