Skip to content

Commit

Permalink
Reimplement MD005/list-indent using micromark tokens, improve range r…
Browse files Browse the repository at this point in the history
…eporting.
  • Loading branch information
DavidAnson committed Oct 13, 2023
1 parent bfb484b commit 9297bcd
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 77 deletions.
42 changes: 20 additions & 22 deletions demo/markdownlint-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3400,52 +3400,50 @@ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o =
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
var _require = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"),
addError = _require.addError,
addErrorDetailIf = _require.addErrorDetailIf,
indentFor = _require.indentFor,
listItemMarkerRe = _require.listItemMarkerRe,
orderedListItemMarkerRe = _require.orderedListItemMarkerRe,
rangeFromRegExp = _require.rangeFromRegExp;
var _require2 = __webpack_require__(/*! ./cache */ "../lib/cache.js"),
flattenedLists = _require2.flattenedLists;
addErrorDetailIf = _require.addErrorDetailIf;
var _require2 = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs"),
filterByTypes = _require2.filterByTypes;
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) {
var _iterator = _createForOfIteratorHelper(flattenedLists()),
var lists = filterByTypes(params.parsers.micromark.tokens, ["listOrdered", "listUnordered"]);
var _iterator = _createForOfIteratorHelper(lists),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var list = _step.value;
var expectedIndent = list.indent;
var expectedIndent = list.startColumn - 1;
var expectedEnd = 0;
var actualEnd = -1;
var endMatching = false;
var _iterator2 = _createForOfIteratorHelper(list.items),
var listItemPrefixes = list.children.filter(function (token) {
return token.type === "listItemPrefix";
});
var _iterator2 = _createForOfIteratorHelper(listItemPrefixes),
_step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var item = _step2.value;
var line = item.line,
lineNumber = item.lineNumber;
var actualIndent = indentFor(item);
var match = null;
if (list.unordered) {
addErrorDetailIf(onError, lineNumber, expectedIndent, actualIndent, null, null, rangeFromRegExp(line, listItemMarkerRe)
var listItemPrefix = _step2.value;
var lineNumber = listItemPrefix.startLine;
var actualIndent = listItemPrefix.startColumn - 1;
var markerLength = listItemPrefix.text.trim().length;
var range = [1, listItemPrefix.startColumn + markerLength];
if (list.type === "listUnordered") {
addErrorDetailIf(onError, lineNumber, expectedIndent, actualIndent, null, null, range
// No fixInfo; MD007 handles this scenario better
);
} else if (match = orderedListItemMarkerRe.exec(line)) {
actualEnd = match[0].length;
} else {
var actualEnd = range[1] - 1;
expectedEnd = expectedEnd || actualEnd;
var markerLength = match[1].length + 1;
if (expectedIndent !== actualIndent || endMatching) {
if (expectedEnd === actualEnd) {
endMatching = true;
} else {
var detail = endMatching ? "Expected: (".concat(expectedEnd, "); Actual: (").concat(actualEnd, ")") : "Expected: ".concat(expectedIndent, "; Actual: ").concat(actualIndent);
var expected = endMatching ? expectedEnd - markerLength : expectedIndent;
var actual = endMatching ? actualEnd - markerLength : actualIndent;
addError(onError, lineNumber, detail, null, rangeFromRegExp(line, listItemMarkerRe), {
addError(onError, lineNumber, detail, undefined, range, {
"editColumn": Math.min(actual, expected) + 1,
"deleteCount": Math.max(actual - expected, 0),
"insertText": "".padEnd(Math.max(expected - actual, 0))
Expand Down
38 changes: 21 additions & 17 deletions lib/md005.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,43 @@

"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,
expectedIndent,
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;
Expand All @@ -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),
Expand Down
24 changes: 12 additions & 12 deletions test/lists-in-blockquote.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,37 +83,37 @@ Text
> Text
>
> > - Item {MD004}
> > - Item {MD004} {MD027}
> > - Item {MD004}
> > - Item {MD004} {MD005} {MD027}
> > - Item {MD004} {MD005}
> >
> > > - Item {MD004}
> > > - Item {MD004} {MD027}
> > > - Item {MD004} {MD027}
> > > - Item {MD004} {MD005} {MD027}
> > > - Item {MD004} {MD005} {MD027}
Text

> Text
> > - Item {MD004} {MD032}
> > - Item {MD004} {MD027}
> > - Item {MD004} {MD032}
> > - Item {MD004} {MD005} {MD027}
> > - Item {MD004} {MD005} {MD032}
> > > - Item {MD004} {MD032}
> > > - Item {MD004} {MD027}
> > > - Item {MD004} {MD027}
> > > - Item {MD004} {MD005} {MD027}
> > > - Item {MD004} {MD005} {MD027}
Text

>+ Item
> + Item
> + Item {MD005}
>>
>>+ Item
>> + Item
>> + Item {MD005}
Text

>+ Item
> + Item {MD032}
> + Item {MD005} {MD032}
>>+ Item {MD032}
>> + Item
>> + Item {MD005}
Text

Expand Down
Loading

0 comments on commit 9297bcd

Please sign in to comment.