mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-30 12:38:48 +01:00
Refactor to remove flattenedLists, indentFor, listItemMarkerRe, orderedListItemMarkerRe, rangeFromRegExp, and unorderedListStyleFor helpers, reimplement MD004/MD029 using micromark tokens.
This commit is contained in:
parent
55729cfcf7
commit
7aac5b0553
9 changed files with 318 additions and 482 deletions
101
lib/md004.js
101
lib/md004.js
|
|
@ -2,11 +2,15 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
const { addErrorDetailIf, listItemMarkerRe, unorderedListStyleFor } =
|
||||
require("../helpers");
|
||||
const { flattenedLists } = require("./cache");
|
||||
const { addErrorDetailIf } = require("../helpers");
|
||||
const { filterByTypes, getDescendantsByType, getTokenParentOfType } = require("../helpers/micromark.cjs");
|
||||
|
||||
const expectedStyleToMarker = {
|
||||
const markerToStyle = {
|
||||
"-": "dash",
|
||||
"+": "plus",
|
||||
"*": "asterisk"
|
||||
};
|
||||
const styleToMarker = {
|
||||
"dash": "-",
|
||||
"plus": "+",
|
||||
"asterisk": "*"
|
||||
|
|
@ -16,7 +20,13 @@ const differentItemStyle = {
|
|||
"plus": "asterisk",
|
||||
"asterisk": "dash"
|
||||
};
|
||||
const validStyles = Object.keys(expectedStyleToMarker);
|
||||
const validStyles = new Set([
|
||||
"asterisk",
|
||||
"consistent",
|
||||
"dash",
|
||||
"plus",
|
||||
"sublist"
|
||||
]);
|
||||
|
||||
// eslint-disable-next-line jsdoc/valid-types
|
||||
/** @type import("./markdownlint").Rule */
|
||||
|
|
@ -24,55 +34,50 @@ module.exports = {
|
|||
"names": [ "MD004", "ul-style" ],
|
||||
"description": "Unordered list style",
|
||||
"tags": [ "bullet", "ul" ],
|
||||
"parser": "none",
|
||||
"parser": "micromark",
|
||||
"function": function MD004(params, onError) {
|
||||
const style = String(params.config.style || "consistent");
|
||||
let expectedStyle = style;
|
||||
let expectedStyle = validStyles.has(style) ? style : "dash";
|
||||
const nestingStyles = [];
|
||||
for (const list of flattenedLists()) {
|
||||
if (list.unordered) {
|
||||
if (expectedStyle === "consistent") {
|
||||
expectedStyle = unorderedListStyleFor(list.items[0]);
|
||||
for (const listUnordered of filterByTypes(params.parsers.micromark.tokens, [ "listUnordered" ])) {
|
||||
let nesting = 0;
|
||||
if (style === "sublist") {
|
||||
/** @type {import("../helpers/micromark.cjs").Token | null} */
|
||||
let parent = listUnordered;
|
||||
while ((parent = getTokenParentOfType(parent, [ "listOrdered", "listUnordered" ]))) {
|
||||
nesting++;
|
||||
}
|
||||
for (const item of list.items) {
|
||||
const itemStyle = unorderedListStyleFor(item);
|
||||
if (style === "sublist") {
|
||||
const nesting = list.nesting;
|
||||
if (!nestingStyles[nesting]) {
|
||||
nestingStyles[nesting] =
|
||||
(itemStyle === nestingStyles[nesting - 1]) ?
|
||||
differentItemStyle[itemStyle] :
|
||||
itemStyle;
|
||||
}
|
||||
expectedStyle = nestingStyles[nesting];
|
||||
}
|
||||
const listItemMarkers = getDescendantsByType(listUnordered, [ "listItemPrefix", "listItemMarker" ]);
|
||||
for (const listItemMarker of listItemMarkers) {
|
||||
const itemStyle = markerToStyle[listItemMarker.text];
|
||||
if (style === "sublist") {
|
||||
if (!nestingStyles[nesting]) {
|
||||
nestingStyles[nesting] =
|
||||
(itemStyle === nestingStyles[nesting - 1]) ?
|
||||
differentItemStyle[itemStyle] :
|
||||
itemStyle;
|
||||
}
|
||||
if (!validStyles.includes(expectedStyle)) {
|
||||
expectedStyle = validStyles[0];
|
||||
}
|
||||
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
|
||||
);
|
||||
expectedStyle = nestingStyles[nesting];
|
||||
} else if (expectedStyle === "consistent") {
|
||||
expectedStyle = itemStyle;
|
||||
}
|
||||
const column = listItemMarker.startColumn;
|
||||
const length = listItemMarker.endColumn - listItemMarker.startColumn;
|
||||
addErrorDetailIf(
|
||||
onError,
|
||||
listItemMarker.startLine,
|
||||
expectedStyle,
|
||||
itemStyle,
|
||||
undefined,
|
||||
undefined,
|
||||
[ column, length ],
|
||||
{
|
||||
"editColumn": column,
|
||||
"deleteCount": length,
|
||||
"insertText": styleToMarker[expectedStyle]
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue