mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
wip
This commit is contained in:
parent
124ee3f733
commit
2aeffeb5f1
6 changed files with 84 additions and 58 deletions
21
README.md
21
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
|
||||
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
|
|
@ -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[];
|
||||
|
|
|
@ -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.<string, LintError[]>} LintResults
|
||||
* @property {ToStringCallback} toString String representation.
|
||||
|
|
|
@ -28,6 +28,9 @@ Generated by [AVA](https://avajs.dev).
|
|||
'clearHtmlCommentText',
|
||||
'cloneIfArray',
|
||||
'cloneIfUrl',
|
||||
'convertToResultVersion0',
|
||||
'convertToResultVersion1',
|
||||
'convertToResultVersion2',
|
||||
'default',
|
||||
'ellipsify',
|
||||
'endOfLineGemojiCodeRe',
|
||||
|
|
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue