diff --git a/helpers/helpers.cjs b/helpers/helpers.cjs index 81a6a854..da23784d 100644 --- a/helpers/helpers.cjs +++ b/helpers/helpers.cjs @@ -540,20 +540,21 @@ function expandTildePath(file, os) { module.exports.expandTildePath = expandTildePath; /** @typedef {import("../lib/markdownlint.mjs").LintError[]} LintErrors */ +/** @typedef {import("../lib/markdownlint.mjs").LintResults} LintResults */ /** * Converts lint errors from resultVersion 3 to 2. * - * @param {LintErrors} results Lint errors (v3). + * @param {LintErrors} errors Lint errors (v3). * @returns {LintErrors} Lint errors (v2). */ -function convertResultVersion3To2(results) { +function convertResultVersion3To2(errors) { // Remove fixInfo and multiple errors for the same rule and line number const noPrevious = { "ruleNames": [], "lineNumber": -1 }; - return results.filter((error, index, array) => { + return errors.filter((error, index, array) => { delete error.fixInfo; const previous = array[index - 1] || noPrevious; return ( @@ -561,16 +562,16 @@ function convertResultVersion3To2(results) { (error.lineNumber !== previous.lineNumber) ); }); -}; +} /** * Converts lint errors from resultVersion 2 to 1. * - * @param {LintErrors} results Lint errors (v2). + * @param {LintErrors} errors Lint errors (v2). * @returns {LintErrors} Lint errors (v1). */ -function convertResultVersion2To1(results) { - for (const error of results) { +function convertResultVersion2To1(errors) { + for (const error of errors) { // @ts-ignore error.ruleName = error.ruleNames[0]; // @ts-ignore @@ -578,18 +579,18 @@ function convertResultVersion2To1(results) { // @ts-ignore delete error.ruleNames; } - return results; -}; + return errors; +} /** * Converts lint errors from resultVersion 2 to 0. * - * @param {LintErrors} results Lint errors (v2). + * @param {LintErrors} errors Lint errors (v2). * @returns {LintErrors} Lint errors (v0). */ -function convertResultVersion2To0(results) { +function convertResultVersion2To0(errors) { const dictionary = {}; - for (const error of results) { + for (const error of errors) { const ruleName = error.ruleNames[0]; const ruleLines = dictionary[ruleName] || []; ruleLines.push(error.lineNumber); @@ -597,16 +598,52 @@ function convertResultVersion2To0(results) { } // @ts-ignore return dictionary; -}; +} +/** + * Copies and transforms lint results from resultVersion 3 to ?. + * + * @param {LintResults} results Lint results (v3). + * @param {(LintErrors)=> LintErrors} boop Lint errors (v?). + * @returns {LintResults} Lint results (v?). + */ +function copyAndTransformResults(results, boop) { + const newr = {}; + Object.defineProperty(newr, "toString", { "value": results.toString }); + for (const key of Object.keys(results)) { + const newrr = results[key].map((r) => ({ ...r })); + newr[key] = boop(newrr); + } + // @ts-ignore + return newr; +} + +/** + * Converts lint results from resultVersion 3 to 0. + * + * @param {LintResults} results Lint results (v3). + * @returns {LintResults} Lint results (v0). + */ module.exports.convertToResultVersion0 = function convertToResultVersion0(results) { - return convertResultVersion2To0(convertResultVersion3To2(results)); + return copyAndTransformResults(results, (r) => convertResultVersion2To0(convertResultVersion3To2(r))); }; +/** + * Converts lint results from resultVersion 3 to 1. + * + * @param {LintResults} results Lint results (v3). + * @returns {LintResults} Lint results (v1). + */ module.exports.convertToResultVersion1 = function convertToResultVersion1(results) { - return convertResultVersion2To1(convertResultVersion3To2(results)); + return copyAndTransformResults(results, (r) => convertResultVersion2To1(convertResultVersion3To2(r))); }; +/** + * Converts lint results from resultVersion 3 to 2. + * + * @param {LintResults} results Lint results (v3). + * @returns {LintResults} Lint results (v2). + */ module.exports.convertToResultVersion2 = function convertToResultVersion2(results) { - return convertResultVersion3To2(results); + return copyAndTransformResults(results, convertResultVersion3To2); }; diff --git a/lib/markdownlint.mjs b/lib/markdownlint.mjs index 1e9b1db4..523ffd1f 100644 --- a/lib/markdownlint.mjs +++ b/lib/markdownlint.mjs @@ -664,14 +664,6 @@ function lintContent( a.ruleNames[0].localeCompare(b.ruleNames[0]) || a.lineNumber - b.lineNumber )); - // Deprecated: Convert results to specified resultVersion - if (resultVersion === 0) { - results = helpers.convertToResultVersion0(results); - } else if (resultVersion === 1) { - results = helpers.convertToResultVersion1(results); - } else if (resultVersion === 2) { - results = helpers.convertToResultVersion2(results); - } return results; }; // Run all rules @@ -893,7 +885,16 @@ function lintInput(options, synchronous, callback) { } else if (concurrency === 0) { // Finish done = true; - return callback(null, results); + // Deprecated: Convert results to specified resultVersion + let convertedResults = results; + if (resultVersion === 0) { + convertedResults = helpers.convertToResultVersion0(results); + } else if (resultVersion === 1) { + convertedResults = helpers.convertToResultVersion1(results); + } else if (resultVersion === 2) { + convertedResults = helpers.convertToResultVersion2(results); + } + return callback(null, convertedResults); } return null; } diff --git a/test/markdownlint-test-result-object.mjs b/test/markdownlint-test-result-object.mjs index 5b89aa65..d543a3fb 100644 --- a/test/markdownlint-test-result-object.mjs +++ b/test/markdownlint-test-result-object.mjs @@ -2,7 +2,9 @@ import test from "ava"; import { lint as lintAsync } from "markdownlint/async"; +import { lint as lintPromise } from "markdownlint/promise"; import { lint as lintSync } from "markdownlint/sync"; +import { convertToResultVersion0, convertToResultVersion1, convertToResultVersion2 } from "markdownlint/helpers"; import { importWithTypeJson } from "./esm-helpers.mjs"; const packageJson = await importWithTypeJson(import.meta, "../package.json"); const { homepage, version } = packageJson; @@ -623,3 +625,35 @@ test("frontMatterResultVersion3", (t) => new Promise((resolve) => { resolve(); }); })); + +test("convertToResultVersionN", async (t) => { + t.plan(6); + const options = { + "files": [ + "./test/break-all-the-rules.md", + "./test/inline-disable-enable.md" + ], + "strings": { + "apple": "# Heading", + "banana": "## Heading" + } + }; + const [ version3, version2, version1, version0 ] = await Promise.all([ + lintPromise(options), + lintPromise({ ...options, "resultVersion": 2 }), + lintPromise({ ...options, "resultVersion": 1 }), + lintPromise({ ...options, "resultVersion": 0 }) + ]); + + const v2 = convertToResultVersion2(version3); + t.deepEqual(v2, version2); + t.is(v2.toString(), version2.toString()); + + const v1 = convertToResultVersion1(version3); + t.deepEqual(v1, version1); + t.is(v1.toString(), version1.toString()); + + const v0 = convertToResultVersion0(version3); + t.deepEqual(v0, version0); + t.is(v0.toString(), version0.toString()); +});