mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
wip
This commit is contained in:
parent
2aeffeb5f1
commit
284bf0335f
3 changed files with 97 additions and 25 deletions
|
@ -540,20 +540,21 @@ function expandTildePath(file, os) {
|
||||||
module.exports.expandTildePath = expandTildePath;
|
module.exports.expandTildePath = expandTildePath;
|
||||||
|
|
||||||
/** @typedef {import("../lib/markdownlint.mjs").LintError[]} LintErrors */
|
/** @typedef {import("../lib/markdownlint.mjs").LintError[]} LintErrors */
|
||||||
|
/** @typedef {import("../lib/markdownlint.mjs").LintResults} LintResults */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts lint errors from resultVersion 3 to 2.
|
* 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).
|
* @returns {LintErrors} Lint errors (v2).
|
||||||
*/
|
*/
|
||||||
function convertResultVersion3To2(results) {
|
function convertResultVersion3To2(errors) {
|
||||||
// Remove fixInfo and multiple errors for the same rule and line number
|
// Remove fixInfo and multiple errors for the same rule and line number
|
||||||
const noPrevious = {
|
const noPrevious = {
|
||||||
"ruleNames": [],
|
"ruleNames": [],
|
||||||
"lineNumber": -1
|
"lineNumber": -1
|
||||||
};
|
};
|
||||||
return results.filter((error, index, array) => {
|
return errors.filter((error, index, array) => {
|
||||||
delete error.fixInfo;
|
delete error.fixInfo;
|
||||||
const previous = array[index - 1] || noPrevious;
|
const previous = array[index - 1] || noPrevious;
|
||||||
return (
|
return (
|
||||||
|
@ -561,16 +562,16 @@ function convertResultVersion3To2(results) {
|
||||||
(error.lineNumber !== previous.lineNumber)
|
(error.lineNumber !== previous.lineNumber)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts lint errors from resultVersion 2 to 1.
|
* 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).
|
* @returns {LintErrors} Lint errors (v1).
|
||||||
*/
|
*/
|
||||||
function convertResultVersion2To1(results) {
|
function convertResultVersion2To1(errors) {
|
||||||
for (const error of results) {
|
for (const error of errors) {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
error.ruleName = error.ruleNames[0];
|
error.ruleName = error.ruleNames[0];
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
|
@ -578,18 +579,18 @@ function convertResultVersion2To1(results) {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
delete error.ruleNames;
|
delete error.ruleNames;
|
||||||
}
|
}
|
||||||
return results;
|
return errors;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts lint errors from resultVersion 2 to 0.
|
* 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).
|
* @returns {LintErrors} Lint errors (v0).
|
||||||
*/
|
*/
|
||||||
function convertResultVersion2To0(results) {
|
function convertResultVersion2To0(errors) {
|
||||||
const dictionary = {};
|
const dictionary = {};
|
||||||
for (const error of results) {
|
for (const error of errors) {
|
||||||
const ruleName = error.ruleNames[0];
|
const ruleName = error.ruleNames[0];
|
||||||
const ruleLines = dictionary[ruleName] || [];
|
const ruleLines = dictionary[ruleName] || [];
|
||||||
ruleLines.push(error.lineNumber);
|
ruleLines.push(error.lineNumber);
|
||||||
|
@ -597,16 +598,52 @@ function convertResultVersion2To0(results) {
|
||||||
}
|
}
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
return dictionary;
|
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) {
|
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) {
|
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) {
|
module.exports.convertToResultVersion2 = function convertToResultVersion2(results) {
|
||||||
return convertResultVersion3To2(results);
|
return copyAndTransformResults(results, convertResultVersion3To2);
|
||||||
};
|
};
|
||||||
|
|
|
@ -664,14 +664,6 @@ function lintContent(
|
||||||
a.ruleNames[0].localeCompare(b.ruleNames[0]) ||
|
a.ruleNames[0].localeCompare(b.ruleNames[0]) ||
|
||||||
a.lineNumber - b.lineNumber
|
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;
|
return results;
|
||||||
};
|
};
|
||||||
// Run all rules
|
// Run all rules
|
||||||
|
@ -893,7 +885,16 @@ function lintInput(options, synchronous, callback) {
|
||||||
} else if (concurrency === 0) {
|
} else if (concurrency === 0) {
|
||||||
// Finish
|
// Finish
|
||||||
done = true;
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,9 @@
|
||||||
|
|
||||||
import test from "ava";
|
import test from "ava";
|
||||||
import { lint as lintAsync } from "markdownlint/async";
|
import { lint as lintAsync } from "markdownlint/async";
|
||||||
|
import { lint as lintPromise } from "markdownlint/promise";
|
||||||
import { lint as lintSync } from "markdownlint/sync";
|
import { lint as lintSync } from "markdownlint/sync";
|
||||||
|
import { convertToResultVersion0, convertToResultVersion1, convertToResultVersion2 } from "markdownlint/helpers";
|
||||||
import { importWithTypeJson } from "./esm-helpers.mjs";
|
import { importWithTypeJson } from "./esm-helpers.mjs";
|
||||||
const packageJson = await importWithTypeJson(import.meta, "../package.json");
|
const packageJson = await importWithTypeJson(import.meta, "../package.json");
|
||||||
const { homepage, version } = packageJson;
|
const { homepage, version } = packageJson;
|
||||||
|
@ -623,3 +625,35 @@ test("frontMatterResultVersion3", (t) => new Promise((resolve) => {
|
||||||
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());
|
||||||
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue