2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
2019-04-10 21:26:59 -07:00
|
|
|
const { addError, addErrorDetailIf, indentFor, listItemMarkerRe,
|
2019-04-13 11:18:57 -07:00
|
|
|
orderedListItemMarkerRe, rangeFromRegExp } = require("../helpers");
|
2019-04-10 21:26:59 -07:00
|
|
|
const { flattenedLists } = require("./cache");
|
2018-01-21 21:44:25 -08:00
|
|
|
|
|
|
|
|
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) {
|
2019-04-10 21:26:59 -07:00
|
|
|
flattenedLists().forEach((list) => {
|
2018-06-15 22:37:12 -07:00
|
|
|
const expectedIndent = list.indent;
|
|
|
|
|
let expectedEnd = 0;
|
|
|
|
|
let actualEnd = -1;
|
|
|
|
|
let endMatching = false;
|
2019-04-10 21:26:59 -07:00
|
|
|
list.items.forEach((item) => {
|
2020-01-15 22:06:41 -08:00
|
|
|
const { line, lineNumber } = item;
|
2019-04-10 21:26:59 -07:00
|
|
|
const actualIndent = indentFor(item);
|
2020-04-09 20:14:36 -07:00
|
|
|
let match = null;
|
2018-06-15 22:37:12 -07:00
|
|
|
if (list.unordered) {
|
2020-01-15 22:06:41 -08:00
|
|
|
addErrorDetailIf(
|
|
|
|
|
onError,
|
|
|
|
|
lineNumber,
|
|
|
|
|
expectedIndent,
|
|
|
|
|
actualIndent,
|
|
|
|
|
null,
|
|
|
|
|
null,
|
|
|
|
|
rangeFromRegExp(line, listItemMarkerRe)
|
|
|
|
|
// No fixInfo; MD007 handles this scenario better
|
|
|
|
|
);
|
2020-04-09 20:14:36 -07:00
|
|
|
} else if ((match = orderedListItemMarkerRe.exec(line))) {
|
2020-01-15 22:06:41 -08:00
|
|
|
actualEnd = match[0].length;
|
2018-06-15 22:37:12 -07:00
|
|
|
expectedEnd = expectedEnd || actualEnd;
|
2020-01-15 22:06:41 -08:00
|
|
|
const markerLength = match[1].length + 1;
|
2018-06-15 22:37:12 -07:00
|
|
|
if ((expectedIndent !== actualIndent) || endMatching) {
|
|
|
|
|
if (expectedEnd === actualEnd) {
|
|
|
|
|
endMatching = true;
|
|
|
|
|
} else {
|
|
|
|
|
const detail = endMatching ?
|
|
|
|
|
`Expected: (${expectedEnd}); Actual: (${actualEnd})` :
|
|
|
|
|
`Expected: ${expectedIndent}; Actual: ${actualIndent}`;
|
2020-01-15 22:06:41 -08:00
|
|
|
const expected = endMatching ?
|
|
|
|
|
expectedEnd - markerLength :
|
|
|
|
|
expectedIndent;
|
|
|
|
|
const actual = endMatching ?
|
|
|
|
|
actualEnd - markerLength :
|
|
|
|
|
actualIndent;
|
|
|
|
|
addError(
|
|
|
|
|
onError,
|
|
|
|
|
lineNumber,
|
|
|
|
|
detail,
|
|
|
|
|
null,
|
|
|
|
|
rangeFromRegExp(line, listItemMarkerRe),
|
|
|
|
|
{
|
|
|
|
|
"editColumn": Math.min(actual, expected) + 1,
|
|
|
|
|
"deleteCount": Math.max(actual - expected, 0),
|
|
|
|
|
"insertText": "".padEnd(Math.max(expected - actual, 0))
|
|
|
|
|
}
|
|
|
|
|
);
|
2018-06-15 22:37:12 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|