Update MD004/ul-style to auto-fix violations (fixes #351).

This commit is contained in:
David Anson 2021-02-06 15:49:02 -08:00
parent 5aef3a4a51
commit ed325ebb56
8 changed files with 113 additions and 37 deletions

View file

@ -2,10 +2,22 @@
"use strict";
const { addErrorDetailIf, listItemMarkerRe,
rangeFromRegExp, unorderedListStyleFor } = require("../helpers");
const { addErrorDetailIf, listItemMarkerRe, unorderedListStyleFor } =
require("../helpers");
const { flattenedLists } = require("./cache");
const expectedStyleToMarker = {
"dash": "-",
"plus": "+",
"asterisk": "*"
};
const differentItemStyle = {
"dash": "plus",
"plus": "asterisk",
"asterisk": "dash"
};
module.exports = {
"names": [ "MD004", "ul-style" ],
"description": "Unordered list style",
@ -23,19 +35,37 @@ module.exports = {
const itemStyle = unorderedListStyleFor(item);
if (style === "sublist") {
const nesting = list.nesting;
if (!nestingStyles[nesting] &&
(itemStyle !== nestingStyles[nesting - 1])) {
nestingStyles[nesting] = itemStyle;
} else {
addErrorDetailIf(onError, item.lineNumber,
nestingStyles[nesting], itemStyle, null, null,
rangeFromRegExp(item.line, listItemMarkerRe));
if (!nestingStyles[nesting]) {
nestingStyles[nesting] =
(itemStyle === nestingStyles[nesting - 1]) ?
differentItemStyle[itemStyle] :
itemStyle;
}
} else {
addErrorDetailIf(onError, item.lineNumber,
expectedStyle, itemStyle, null, null,
rangeFromRegExp(item.line, listItemMarkerRe));
expectedStyle = nestingStyles[nesting];
}
let range = null;
let fixInfo = null;
const match = item.line.match(listItemMarkerRe);
if (match) {
const column = match.index + 1;
const length = match[0].length;
range = [ column, length ];
fixInfo = {
"editColumn": match[1].length + 1,
"deleteCount": 1,
"insertText": expectedStyleToMarker[expectedStyle]
};
}
addErrorDetailIf(
onError,
item.lineNumber,
expectedStyle,
itemStyle,
null,
null,
range,
fixInfo
);
});
}
});