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,
|
|
|
|
|
orderedListItemMarkerRe, rangeFromRegExp } = require("./shared");
|
|
|
|
|
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) => {
|
|
|
|
|
const actualIndent = indentFor(item);
|
2018-06-15 22:37:12 -07:00
|
|
|
if (list.unordered) {
|
2019-04-10 21:26:59 -07:00
|
|
|
addErrorDetailIf(onError, item.lineNumber,
|
2019-03-24 21:50:56 -07:00
|
|
|
expectedIndent, actualIndent, null, null,
|
2019-04-10 21:26:59 -07:00
|
|
|
rangeFromRegExp(item.line, listItemMarkerRe));
|
2018-06-15 22:37:12 -07:00
|
|
|
} else {
|
2019-04-10 21:26:59 -07:00
|
|
|
const match = orderedListItemMarkerRe.exec(item.line);
|
2018-06-15 22:37:12 -07:00
|
|
|
actualEnd = match && match[0].length;
|
|
|
|
|
expectedEnd = expectedEnd || actualEnd;
|
|
|
|
|
if ((expectedIndent !== actualIndent) || endMatching) {
|
|
|
|
|
if (expectedEnd === actualEnd) {
|
|
|
|
|
endMatching = true;
|
|
|
|
|
} else {
|
|
|
|
|
const detail = endMatching ?
|
|
|
|
|
`Expected: (${expectedEnd}); Actual: (${actualEnd})` :
|
|
|
|
|
`Expected: ${expectedIndent}; Actual: ${actualIndent}`;
|
2019-04-10 21:26:59 -07:00
|
|
|
addError(onError, item.lineNumber, detail, null,
|
|
|
|
|
rangeFromRegExp(item.line, listItemMarkerRe));
|
2018-06-15 22:37:12 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|