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 lineIndex = lineNumber - 1;
var editIndex = editColumn - 1; var editIndex = editColumn - 1;
if ((lineIndex !== lastLineIndex) || if ((lineIndex !== lastLineIndex) ||
((editIndex + deleteCount) < lastEditIndex) || (deleteCount === -1) ||
(deleteCount === -1)) { ((editIndex + deleteCount) <=
(lastEditIndex - ((deleteCount > 0) ? 0 : 1)))) {
lines[lineIndex] = applyFix(lines[lineIndex], fixInfo, lineEnding); lines[lineIndex] = applyFix(lines[lineIndex], fixInfo, lineEnding);
} }
lastLineIndex = lineIndex; lastLineIndex = lineIndex;
@ -1836,8 +1837,18 @@ module.exports = {
"use strict"; "use strict";
// @ts-check // @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 flattenedLists = __webpack_require__(/*! ./cache */ "../lib/cache.js").flattenedLists;
var expectedStyleToMarker = {
"dash": "-",
"plus": "+",
"asterisk": "*"
};
var differentItemStyle = {
"dash": "plus",
"plus": "asterisk",
"asterisk": "dash"
};
module.exports = { module.exports = {
"names": ["MD004", "ul-style"], "names": ["MD004", "ul-style"],
"description": "Unordered list style", "description": "Unordered list style",
@ -1855,17 +1866,28 @@ module.exports = {
var itemStyle = unorderedListStyleFor(item); var itemStyle = unorderedListStyleFor(item);
if (style === "sublist") { if (style === "sublist") {
var nesting = list.nesting; var nesting = list.nesting;
if (!nestingStyles[nesting] && if (!nestingStyles[nesting]) {
(itemStyle !== nestingStyles[nesting - 1])) { nestingStyles[nesting] =
nestingStyles[nesting] = itemStyle; (itemStyle === nestingStyles[nesting - 1]) ?
} differentItemStyle[itemStyle] :
else { itemStyle;
addErrorDetailIf(onError, item.lineNumber, nestingStyles[nesting], itemStyle, null, null, rangeFromRegExp(item.line, listItemMarkerRe));
} }
expectedStyle = nestingStyles[nesting];
} }
else { var range = null;
addErrorDetailIf(onError, item.lineNumber, expectedStyle, itemStyle, null, null, rangeFromRegExp(item.line, listItemMarkerRe)); 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 Parameters: style ("consistent", "asterisk", "plus", "dash", "sublist"; default
"consistent") "consistent")
Fixable: Most violations can be fixed by tooling
This rule is triggered when the symbols used in the document for unordered This rule is triggered when the symbols used in the document for unordered
list items do not match the configured unordered list style: 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; const editIndex = editColumn - 1;
if ( if (
(lineIndex !== lastLineIndex) || (lineIndex !== lastLineIndex) ||
((editIndex + deleteCount) < lastEditIndex) || (deleteCount === -1) ||
(deleteCount === -1) ((editIndex + deleteCount) <=
(lastEditIndex - ((deleteCount > 0) ? 0 : 1)))
) { ) {
lines[lineIndex] = applyFix(lines[lineIndex], fixInfo, lineEnding); lines[lineIndex] = applyFix(lines[lineIndex], fixInfo, lineEnding);
} }

View file

@ -2,10 +2,22 @@
"use strict"; "use strict";
const { addErrorDetailIf, listItemMarkerRe, const { addErrorDetailIf, listItemMarkerRe, unorderedListStyleFor } =
rangeFromRegExp, unorderedListStyleFor } = require("../helpers"); require("../helpers");
const { flattenedLists } = require("./cache"); const { flattenedLists } = require("./cache");
const expectedStyleToMarker = {
"dash": "-",
"plus": "+",
"asterisk": "*"
};
const differentItemStyle = {
"dash": "plus",
"plus": "asterisk",
"asterisk": "dash"
};
module.exports = { module.exports = {
"names": [ "MD004", "ul-style" ], "names": [ "MD004", "ul-style" ],
"description": "Unordered list style", "description": "Unordered list style",
@ -23,19 +35,37 @@ module.exports = {
const itemStyle = unorderedListStyleFor(item); const itemStyle = unorderedListStyleFor(item);
if (style === "sublist") { if (style === "sublist") {
const nesting = list.nesting; const nesting = list.nesting;
if (!nestingStyles[nesting] && if (!nestingStyles[nesting]) {
(itemStyle !== nestingStyles[nesting - 1])) { nestingStyles[nesting] =
nestingStyles[nesting] = itemStyle; (itemStyle === nestingStyles[nesting - 1]) ?
} else { differentItemStyle[itemStyle] :
addErrorDetailIf(onError, item.lineNumber, itemStyle;
nestingStyles[nesting], itemStyle, null, null,
rangeFromRegExp(item.line, listItemMarkerRe));
} }
} else { expectedStyle = nestingStyles[nesting];
addErrorDetailIf(onError, item.lineNumber,
expectedStyle, itemStyle, null, null,
rangeFromRegExp(item.line, listItemMarkerRe));
} }
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 * Alpha
* Bravo * Bravo
- Charlie * Charlie
* Delta * Delta
* Echo * Echo

View file

@ -459,7 +459,7 @@ test("applyFix", (t) => {
}); });
test("applyFixes", (t) => { test("applyFixes", (t) => {
t.plan(29); t.plan(30);
const testCases = [ const testCases = [
[ [
"Hello world.", "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) => { testCases.forEach((testCase) => {

View file

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

View file

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