Reimplement MD005/list-indent using micromark tokens, improve range reporting.

This commit is contained in:
David Anson 2023-10-12 21:43:31 -07:00
parent bfb484b513
commit 9297bcde1c
5 changed files with 271 additions and 77 deletions

View file

@ -2,25 +2,30 @@
"use strict";
const { addError, addErrorDetailIf, indentFor, listItemMarkerRe,
orderedListItemMarkerRe, rangeFromRegExp } = require("../helpers");
const { flattenedLists } = require("./cache");
const { addError, addErrorDetailIf } = require("../helpers");
const { filterByTypes } = require("../helpers/micromark.cjs");
module.exports = {
"names": [ "MD005", "list-indent" ],
"description": "Inconsistent indentation for list items at the same level",
"tags": [ "bullet", "ul", "indentation" ],
"function": function MD005(params, onError) {
for (const list of flattenedLists()) {
const expectedIndent = list.indent;
const lists = filterByTypes(
params.parsers.micromark.tokens,
[ "listOrdered", "listUnordered" ]
);
for (const list of lists) {
const expectedIndent = list.startColumn - 1;
let expectedEnd = 0;
let actualEnd = -1;
let endMatching = false;
for (const item of list.items) {
const { line, lineNumber } = item;
const actualIndent = indentFor(item);
let match = null;
if (list.unordered) {
const listItemPrefixes =
list.children.filter((token) => (token.type === "listItemPrefix"));
for (const listItemPrefix of listItemPrefixes) {
const lineNumber = listItemPrefix.startLine;
const actualIndent = listItemPrefix.startColumn - 1;
const markerLength = listItemPrefix.text.trim().length;
const range = [ 1, listItemPrefix.startColumn + markerLength ];
if (list.type === "listUnordered") {
addErrorDetailIf(
onError,
lineNumber,
@ -28,13 +33,12 @@ module.exports = {
actualIndent,
null,
null,
rangeFromRegExp(line, listItemMarkerRe)
range
// No fixInfo; MD007 handles this scenario better
);
} else if ((match = orderedListItemMarkerRe.exec(line))) {
actualEnd = match[0].length;
} else {
const actualEnd = range[1] - 1;
expectedEnd = expectedEnd || actualEnd;
const markerLength = match[1].length + 1;
if ((expectedIndent !== actualIndent) || endMatching) {
if (expectedEnd === actualEnd) {
endMatching = true;
@ -52,8 +56,8 @@ module.exports = {
onError,
lineNumber,
detail,
null,
rangeFromRegExp(line, listItemMarkerRe),
undefined,
range,
{
"editColumn": Math.min(actual, expected) + 1,
"deleteCount": Math.max(actual - expected, 0),