From a56441003ec146daa7e4cb1db69b00f99cb98acd Mon Sep 17 00:00:00 2001 From: David Anson Date: Tue, 26 Aug 2025 00:11:40 -0700 Subject: [PATCH] wip --- README.md | 21 +---- helpers/helpers.cjs | 72 ++++++++++++++++++ lib/markdownlint.d.mts | 2 +- lib/markdownlint.mjs | 44 ++--------- .../markdownlint-test-exports.mjs.md | 3 + .../markdownlint-test-exports.mjs.snap | Bin 1504 -> 1544 bytes 6 files changed, 84 insertions(+), 58 deletions(-) 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 a21e64a2950fbd37e16f75a9332d0ebc98e846f9..bf686f39cc606c40a1d48aa09feaf53c82875be7 100644 GIT binary patch literal 1544 zcmV+j2KV_vRzVHo}>TT?w&)g-%kl900~qJj#Ec=8}3fm{^Dizw*LlLvz+7y_avucmvqYx)r% z#62zj{i^EwSO4p)o=e>o5p`DeEANxxMv$bnVWbR&cS$xN&9!7h-H(iZKMnd|4vU&!)d|s z;Itj*$_;qVnS@iBahe0BR>SC=6kM7_{WYU*$*T7;y)HWmGcz$}=5kD*#E_Ius89Zu z8mef(H6_%K8ERXO(q>)IeN2dit51%(J8!~uxQaBBh6}-y0f#dKcHLyaP?0g5R04mK zfp^{nRwUROg$X<5j@g+!n`}_ssXXcePq@HyE}&iDRd@5cDq5o?WZw4Y~z` zZq);t9&oP*Jn7}(O0n$cJ%D+@dI78!K<{-Ac-sR$_JFTF;Hn4w=>csY*y{t2`}qlK zg$Z_?^nuep@RAR_*#tN~nY0M#aNcM~|$1kN>q zcbdSLP2l$?u)763)dF5<0WY`m2I>U^{+lh}-4<}U1$^5AuC;(_8`#qZ_O*dCZQw#X zKZ#e|uMgV5hi%~V0_f*JAC3#BiCJgDHfo&>hhqHzU)vNMCsq7ZA)RUyR_l>@mXzj^ zOtr~WYbnx^I7>&Pa3m;Mb)vYcyAdnJ6&0~?A(CC*ONQN5HXDYEN=2$PbShOAB57#N zlpIf|z&K#JMx?W3#fDl(5L1WDKrBRq0ZFr* zaa6St+YynZ!`|)m5fITv?&3(owzA`eIg68b4)gyC4QJ0jWFs^G+hFtbiZfnZz=#g29F_Im6 zn#1a!O*OqG%90sr#*gL9~%u;$R5948D-;(B2{kBDEZm$NRqhlgiQk z^Yaguf+>0YP$`d+$B&f4Cw}~(je=_Kv7Nh?d;D-x!1n)Kz}!mQK{{Y68p$x|#(i79 zBevzKKC!s_b*a4BJSVs|x>W3RCkkm}r6Bq8D%IVl!LZLX*LmG;z?M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