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

@ -703,8 +703,9 @@ module.exports.applyFixes = function applyFixes(input, errors) {
var lineIndex = lineNumber - 1;
var editIndex = editColumn - 1;
if ((lineIndex !== lastLineIndex) ||
((editIndex + deleteCount) < lastEditIndex) ||
(deleteCount === -1)) {
(deleteCount === -1) ||
((editIndex + deleteCount) <=
(lastEditIndex - ((deleteCount > 0) ? 0 : 1)))) {
lines[lineIndex] = applyFix(lines[lineIndex], fixInfo, lineEnding);
}
lastLineIndex = lineIndex;
@ -1836,8 +1837,18 @@ module.exports = {
"use strict";
// @ts-check
var _a = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"), addErrorDetailIf = _a.addErrorDetailIf, listItemMarkerRe = _a.listItemMarkerRe, rangeFromRegExp = _a.rangeFromRegExp, unorderedListStyleFor = _a.unorderedListStyleFor;
var _a = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"), addErrorDetailIf = _a.addErrorDetailIf, listItemMarkerRe = _a.listItemMarkerRe, unorderedListStyleFor = _a.unorderedListStyleFor;
var flattenedLists = __webpack_require__(/*! ./cache */ "../lib/cache.js").flattenedLists;
var expectedStyleToMarker = {
"dash": "-",
"plus": "+",
"asterisk": "*"
};
var differentItemStyle = {
"dash": "plus",
"plus": "asterisk",
"asterisk": "dash"
};
module.exports = {
"names": ["MD004", "ul-style"],
"description": "Unordered list style",
@ -1855,17 +1866,28 @@ module.exports = {
var itemStyle = unorderedListStyleFor(item);
if (style === "sublist") {
var 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;
}
expectedStyle = nestingStyles[nesting];
}
else {
addErrorDetailIf(onError, item.lineNumber, expectedStyle, itemStyle, null, null, rangeFromRegExp(item.line, listItemMarkerRe));
var range = null;
var fixInfo = null;
var match = item.line.match(listItemMarkerRe);
if (match) {
var column = match.index + 1;
var length_1 = match[0].length;
range = [column, length_1];
fixInfo = {
"editColumn": match[1].length + 1,
"deleteCount": 1,
"insertText": expectedStyleToMarker[expectedStyle]
};
}
addErrorDetailIf(onError, item.lineNumber, expectedStyle, itemStyle, null, null, range, fixInfo);
});
}
});

View file

@ -142,6 +142,8 @@ Aliases: ul-style
Parameters: style ("consistent", "asterisk", "plus", "dash", "sublist"; default
"consistent")
Fixable: Most violations can be fixed by tooling
This rule is triggered when the symbols used in the document for unordered
list items do not match the configured unordered list style:

View file

@ -723,8 +723,9 @@ module.exports.applyFixes = function applyFixes(input, errors) {
const editIndex = editColumn - 1;
if (
(lineIndex !== lastLineIndex) ||
((editIndex + deleteCount) < lastEditIndex) ||
(deleteCount === -1)
(deleteCount === -1) ||
((editIndex + deleteCount) <=
(lastEditIndex - ((deleteCount > 0) ? 0 : 1)))
) {
lines[lineIndex] = applyFix(lines[lineIndex], fixInfo, lineEnding);
}

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
);
});
}
});

View file

@ -7,7 +7,7 @@
* Alpha
* Bravo
- Charlie
* Charlie
* Delta
* Echo

View file

@ -459,7 +459,7 @@ test("applyFix", (t) => {
});
test("applyFixes", (t) => {
t.plan(29);
t.plan(30);
const testCases = [
[
"Hello world.",
@ -905,6 +905,27 @@ test("applyFixes", (t) => {
}
],
""
],
[
" hello world",
[
{
"lineNumber": 1,
"fixInfo": {
"editColumn": 1,
"deleteCount": 1
}
},
{
"lineNumber": 1,
"fixInfo": {
"editColumn": 2,
"deleteCount": 1,
"insertText": "H"
}
}
],
"Hello world"
]
];
testCases.forEach((testCase) => {

View file

@ -1,6 +0,0 @@
{
"default": true,
"MD004": {
"style": "sublist"
}
}

View file

@ -47,12 +47,18 @@
- item
* item
* item {MD004}
+ item
+ item {MD004}
- item
* item
+ item
- item
* item
- item {MD004}
+ item
- item
+ item {MD004}
<!-- markdownlint-configure-file {
"ul-style": {
"style": "sublist"
}
} -->