mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2026-01-04 15:08:49 +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
73
lib/md029.js
73
lib/md029.js
|
|
@ -2,9 +2,8 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
const { addErrorDetailIf, listItemMarkerRe, orderedListItemMarkerRe,
|
||||
rangeFromRegExp } = require("../helpers");
|
||||
const { flattenedLists } = require("./cache");
|
||||
const { addErrorDetailIf } = require("../helpers");
|
||||
const { filterByTypes, getDescendantsByType, getTokenTextByType } = require("../helpers/micromark.cjs");
|
||||
|
||||
const listStyleExamples = {
|
||||
"one": "1/1/1",
|
||||
|
|
@ -12,32 +11,37 @@ const listStyleExamples = {
|
|||
"zero": "0/0/0"
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the value of an ordered list item prefix token.
|
||||
*
|
||||
* @param {import("../helpers/micromark.cjs").Token} listItemPrefix List item prefix token.
|
||||
* @returns {number} List item value.
|
||||
*/
|
||||
function getOrderedListItemValue(listItemPrefix) {
|
||||
return Number(getTokenTextByType(listItemPrefix.children, "listItemValue"));
|
||||
}
|
||||
|
||||
// eslint-disable-next-line jsdoc/valid-types
|
||||
/** @type import("./markdownlint").Rule */
|
||||
module.exports = {
|
||||
"names": [ "MD029", "ol-prefix" ],
|
||||
"description": "Ordered list item prefix",
|
||||
"tags": [ "ol" ],
|
||||
"parser": "none",
|
||||
"parser": "micromark",
|
||||
"function": function MD029(params, onError) {
|
||||
const style = String(params.config.style || "one_or_ordered");
|
||||
const filteredLists = flattenedLists().filter((list) => !list.unordered);
|
||||
for (const list of filteredLists) {
|
||||
const { items } = list;
|
||||
let current = 1;
|
||||
for (const listOrdered of filterByTypes(params.parsers.micromark.tokens, [ "listOrdered" ])) {
|
||||
const listItemPrefixes = getDescendantsByType(listOrdered, [ "listItemPrefix" ]);
|
||||
let expected = 1;
|
||||
let incrementing = false;
|
||||
// Check for incrementing number pattern 1/2/3 or 0/1/2
|
||||
if (items.length >= 2) {
|
||||
const first = orderedListItemMarkerRe.exec(items[0].line);
|
||||
const second = orderedListItemMarkerRe.exec(items[1].line);
|
||||
if (first && second) {
|
||||
const [ , firstNumber ] = first;
|
||||
const [ , secondNumber ] = second;
|
||||
if ((secondNumber !== "1") || (firstNumber === "0")) {
|
||||
incrementing = true;
|
||||
if (firstNumber === "0") {
|
||||
current = 0;
|
||||
}
|
||||
if (listItemPrefixes.length >= 2) {
|
||||
const firstValue = getOrderedListItemValue(listItemPrefixes[0]);
|
||||
const secondValue = getOrderedListItemValue(listItemPrefixes[1]);
|
||||
if ((secondValue !== 1) || (firstValue === 0)) {
|
||||
incrementing = true;
|
||||
if (firstValue === 0) {
|
||||
expected = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -45,24 +49,25 @@ module.exports = {
|
|||
let listStyle = style;
|
||||
if (listStyle === "one_or_ordered") {
|
||||
listStyle = incrementing ? "ordered" : "one";
|
||||
}
|
||||
// Force expected value for 0/0/0 and 1/1/1 patterns
|
||||
if (listStyle === "zero") {
|
||||
current = 0;
|
||||
} else if (listStyle === "zero") {
|
||||
expected = 0;
|
||||
} else if (listStyle === "one") {
|
||||
current = 1;
|
||||
expected = 1;
|
||||
}
|
||||
// Validate each list item marker
|
||||
for (const item of items) {
|
||||
const match = orderedListItemMarkerRe.exec(item.line);
|
||||
if (match) {
|
||||
addErrorDetailIf(onError, item.lineNumber,
|
||||
String(current), match[1],
|
||||
"Style: " + listStyleExamples[listStyle], null,
|
||||
rangeFromRegExp(item.line, listItemMarkerRe));
|
||||
if (listStyle === "ordered") {
|
||||
current++;
|
||||
}
|
||||
for (const listItemPrefix of listItemPrefixes) {
|
||||
const actual = getOrderedListItemValue(listItemPrefix);
|
||||
addErrorDetailIf(
|
||||
onError,
|
||||
listItemPrefix.startLine,
|
||||
expected,
|
||||
actual,
|
||||
"Style: " + listStyleExamples[listStyle],
|
||||
undefined,
|
||||
[ listItemPrefix.startColumn, listItemPrefix.endColumn - listItemPrefix.startColumn ]
|
||||
);
|
||||
if (listStyle === "ordered") {
|
||||
expected++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue