mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 06:20:12 +01:00
Update MD006/MD023 to report fixInfo for violations, process input to fixErrors.
This commit is contained in:
parent
84e972c72c
commit
5e73aa1d9d
5 changed files with 94 additions and 25 deletions
|
|
@ -404,7 +404,7 @@ module.exports.frontMatterHasTitle =
|
||||||
module.exports.fixErrors = function fixErrors(input, errors) {
|
module.exports.fixErrors = function fixErrors(input, errors) {
|
||||||
const lines = input.split(newLineRe);
|
const lines = input.split(newLineRe);
|
||||||
// Normalize fixInfo objects
|
// Normalize fixInfo objects
|
||||||
const fixInfos = errors.filter((error) => !!error.fixInfo).map((error) => {
|
let fixInfos = errors.filter((error) => !!error.fixInfo).map((error) => {
|
||||||
const { fixInfo } = error;
|
const { fixInfo } = error;
|
||||||
return {
|
return {
|
||||||
"lineNumber": fixInfo.lineNumber || error.lineNumber,
|
"lineNumber": fixInfo.lineNumber || error.lineNumber,
|
||||||
|
|
@ -413,14 +413,46 @@ module.exports.fixErrors = function fixErrors(input, errors) {
|
||||||
"insertText": fixInfo.insertText || ""
|
"insertText": fixInfo.insertText || ""
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
// Sort bottom-to-top, deletes last, right-to-left, long-to-short
|
// Sort bottom-to-top, line-deletes last, right-to-left, long-to-short
|
||||||
fixInfos.sort((a, b) => (
|
fixInfos.sort((a, b) => {
|
||||||
(b.lineNumber - a.lineNumber) ||
|
const aDeletingLine = (a.deleteCount === -1);
|
||||||
((a.deleteCount === -1) ? 1 : ((b.deleteCount === -1) ? -1 : 0)) ||
|
const bDeletingLine = (b.deleteCount === -1);
|
||||||
(b.editColumn - a.editColumn) ||
|
return (
|
||||||
(b.insertText.length - a.insertText.length)
|
(b.lineNumber - a.lineNumber) ||
|
||||||
));
|
(aDeletingLine ? 1 : (bDeletingLine ? -1 : 0)) ||
|
||||||
// Apply all fixes
|
(b.editColumn - a.editColumn) ||
|
||||||
|
(b.insertText.length - a.insertText.length)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
// Remove duplicate entries (needed for following collapse step)
|
||||||
|
let lastFixInfo = {};
|
||||||
|
fixInfos = fixInfos.filter((fixInfo) => {
|
||||||
|
const unique = (
|
||||||
|
(fixInfo.lineNumber !== lastFixInfo.lineNumber) ||
|
||||||
|
(fixInfo.editColumn !== lastFixInfo.editColumn) ||
|
||||||
|
(fixInfo.deleteCount !== lastFixInfo.deleteCount) ||
|
||||||
|
(fixInfo.insertText !== lastFixInfo.insertText)
|
||||||
|
);
|
||||||
|
lastFixInfo = fixInfo;
|
||||||
|
return unique;
|
||||||
|
});
|
||||||
|
// Collapse insert/no-delete and no-insert/delete for same line/column
|
||||||
|
lastFixInfo = {};
|
||||||
|
fixInfos.forEach((fixInfo) => {
|
||||||
|
if (
|
||||||
|
(fixInfo.lineNumber === lastFixInfo.lineNumber) &&
|
||||||
|
(fixInfo.editColumn === lastFixInfo.editColumn) &&
|
||||||
|
!fixInfo.insertText &&
|
||||||
|
(fixInfo.deleteCount > 0) &&
|
||||||
|
lastFixInfo.insertText &&
|
||||||
|
!lastFixInfo.deleteCount) {
|
||||||
|
fixInfo.insertText = lastFixInfo.insertText;
|
||||||
|
lastFixInfo.lineNumber = 0;
|
||||||
|
}
|
||||||
|
lastFixInfo = fixInfo;
|
||||||
|
});
|
||||||
|
fixInfos = fixInfos.filter((fixInfo) => fixInfo.lineNumber);
|
||||||
|
// Apply all (remaining/updated) fixes
|
||||||
let lastLineIndex = -1;
|
let lastLineIndex = -1;
|
||||||
let lastEditIndex = -1;
|
let lastEditIndex = -1;
|
||||||
fixInfos.forEach((fixInfo) => {
|
fixInfos.forEach((fixInfo) => {
|
||||||
|
|
|
||||||
17
lib/md006.js
17
lib/md006.js
|
|
@ -13,10 +13,19 @@ module.exports = {
|
||||||
"tags": [ "bullet", "ul", "indentation" ],
|
"tags": [ "bullet", "ul", "indentation" ],
|
||||||
"function": function MD006(params, onError) {
|
"function": function MD006(params, onError) {
|
||||||
flattenedLists().forEach((list) => {
|
flattenedLists().forEach((list) => {
|
||||||
if (list.unordered && !list.nesting) {
|
if (list.unordered && !list.nesting && (list.indent !== 0)) {
|
||||||
addErrorDetailIf(onError, list.open.lineNumber,
|
const { lineNumber, line } = list.open;
|
||||||
0, list.indent, null, null,
|
addErrorDetailIf(
|
||||||
rangeFromRegExp(list.open.line, listItemMarkerRe));
|
onError,
|
||||||
|
lineNumber,
|
||||||
|
0,
|
||||||
|
list.indent,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
rangeFromRegExp(line, listItemMarkerRe),
|
||||||
|
{
|
||||||
|
"deleteCount": line.length - line.trimLeft().length
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
26
lib/md023.js
26
lib/md023.js
|
|
@ -2,8 +2,7 @@
|
||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const { addErrorContext, filterTokens, rangeFromRegExp } =
|
const { addErrorContext, filterTokens } = require("../helpers");
|
||||||
require("../helpers");
|
|
||||||
|
|
||||||
const spaceBeforeHeadingRe = /^((?:\s+)|(?:[>\s]+\s\s))[^>\s]/;
|
const spaceBeforeHeadingRe = /^((?:\s+)|(?:[>\s]+\s\s))[^>\s]/;
|
||||||
|
|
||||||
|
|
@ -13,9 +12,26 @@ module.exports = {
|
||||||
"tags": [ "headings", "headers", "spaces" ],
|
"tags": [ "headings", "headers", "spaces" ],
|
||||||
"function": function MD023(params, onError) {
|
"function": function MD023(params, onError) {
|
||||||
filterTokens(params, "heading_open", function forToken(token) {
|
filterTokens(params, "heading_open", function forToken(token) {
|
||||||
if (spaceBeforeHeadingRe.test(token.line)) {
|
const { lineNumber, line } = token;
|
||||||
addErrorContext(onError, token.lineNumber, token.line, null,
|
const match = line.match(spaceBeforeHeadingRe);
|
||||||
null, rangeFromRegExp(token.line, spaceBeforeHeadingRe));
|
if (match) {
|
||||||
|
const [ prefixAndFirstChar, prefix ] = match;
|
||||||
|
let deleteCount = prefix.length;
|
||||||
|
const prefixLengthNoSpace = prefix.trimRight().length;
|
||||||
|
if (prefixLengthNoSpace) {
|
||||||
|
deleteCount -= prefixLengthNoSpace - 1;
|
||||||
|
}
|
||||||
|
addErrorContext(
|
||||||
|
onError,
|
||||||
|
lineNumber,
|
||||||
|
line,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
[ 1, prefixAndFirstChar.length ],
|
||||||
|
{
|
||||||
|
"editColumn": prefixLengthNoSpace + 1,
|
||||||
|
"deleteCount": deleteCount
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,13 +16,11 @@
|
||||||
|
|
||||||
- one {MD032}
|
- one {MD032}
|
||||||
1. two {MD032}
|
1. two {MD032}
|
||||||
1. three {MD032}
|
- three {MD032}
|
||||||
- four {MD032}
|
|
||||||
|
|
||||||
1. one {MD032}
|
1. one {MD032}
|
||||||
- two {MD006} {MD032}
|
- two {MD006} {MD032}
|
||||||
- three {MD032}
|
1. three {MD032}
|
||||||
1. four {MD032}
|
|
||||||
|
|
||||||
## Correct nesting, same type
|
## Correct nesting, same type
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1898,7 +1898,7 @@ module.exports.forEachInlineCodeSpan = function forEachInlineCodeSpan(test) {
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.fixErrors = function fixErrors(test) {
|
module.exports.fixErrors = function fixErrors(test) {
|
||||||
test.expect(23);
|
test.expect(24);
|
||||||
const testCases = [
|
const testCases = [
|
||||||
[
|
[
|
||||||
"Hello world.",
|
"Hello world.",
|
||||||
|
|
@ -2221,6 +2221,20 @@ module.exports.fixErrors = function fixErrors(test) {
|
||||||
],
|
],
|
||||||
"Hello wld"
|
"Hello wld"
|
||||||
],
|
],
|
||||||
|
[
|
||||||
|
"Hello world",
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"fixInfo": {
|
||||||
|
"lineNumber": 1,
|
||||||
|
"editColumn": 7,
|
||||||
|
"deleteCount": 1,
|
||||||
|
"insertText": "z"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"Hello zorld"
|
||||||
|
],
|
||||||
[
|
[
|
||||||
"Hello world",
|
"Hello world",
|
||||||
[
|
[
|
||||||
|
|
@ -2239,7 +2253,7 @@ module.exports.fixErrors = function fixErrors(test) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"Hello zworld"
|
"Hello zorld"
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
"Hello world",
|
"Hello world",
|
||||||
|
|
@ -2259,7 +2273,7 @@ module.exports.fixErrors = function fixErrors(test) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"Hello zworld"
|
"Hello zorld"
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
testCases.forEach((testCase) => {
|
testCases.forEach((testCase) => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue