mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
Add MD030 with tests, refactor list handling with flattenLists.
This commit is contained in:
parent
b21548a992
commit
2b289ab5f3
4 changed files with 177 additions and 56 deletions
147
lib/rules.js
147
lib/rules.js
|
@ -18,14 +18,13 @@ function headingStyleFor(token) {
|
|||
|
||||
function unorderedListStyleFor(token) {
|
||||
switch (token.line.trimLeft().substr(0, 1)) {
|
||||
case "*":
|
||||
return "asterisk";
|
||||
case "-":
|
||||
return "dash";
|
||||
case "+":
|
||||
return "plus";
|
||||
case "*":
|
||||
default:
|
||||
return null;
|
||||
return "asterisk";
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -54,6 +53,40 @@ function forEachLine(params, callback) {
|
|||
});
|
||||
}
|
||||
|
||||
function flattenLists(tokens, filterBy) {
|
||||
var lists = [];
|
||||
var stack = [];
|
||||
var current = null;
|
||||
var lastWithLines = null;
|
||||
tokens.forEach(function forToken(token) {
|
||||
if ((token.type === "bullet_list_open") ||
|
||||
(token.type === "ordered_list_open")) {
|
||||
stack.push(current);
|
||||
current = {
|
||||
"ordered": (token.type === "ordered_list_open"),
|
||||
"open": token,
|
||||
"items": [],
|
||||
"nesting": stack.length - 1,
|
||||
"lastLineIndex": -1,
|
||||
"insert": lists.length
|
||||
};
|
||||
} else if ((token.type === "bullet_list_close") ||
|
||||
(token.type === "ordered_list_close")) {
|
||||
current.lastLineIndex = lastWithLines.lines[1];
|
||||
if ((filterBy === undefined) || (filterBy === current.ordered)) {
|
||||
lists.splice(current.insert, 0, current);
|
||||
delete current.insert;
|
||||
}
|
||||
current = stack.pop();
|
||||
} else if (token.type === "list_item_open") {
|
||||
current.items.push(token);
|
||||
} else if (token.lines) {
|
||||
lastWithLines = token;
|
||||
}
|
||||
});
|
||||
return lists;
|
||||
}
|
||||
|
||||
module.exports = [
|
||||
{
|
||||
"name": "MD001",
|
||||
|
@ -108,14 +141,15 @@ module.exports = [
|
|||
"desc": "Unordered list style",
|
||||
"func": function MD004(params, errors) {
|
||||
var style = params.options.style || "consistent";
|
||||
var listItems = filterTokens(params.tokens, "list_item_open");
|
||||
if ((style === "consistent") && listItems.length) {
|
||||
style = unorderedListStyleFor(listItems[0]);
|
||||
}
|
||||
listItems.forEach(function forToken(token) {
|
||||
if (unorderedListStyleFor(token) !== style) {
|
||||
errors.push(token.lineNumber);
|
||||
flattenLists(params.tokens, false).forEach(function forList(list) {
|
||||
if (style === "consistent") {
|
||||
style = unorderedListStyleFor(list.items[0]);
|
||||
}
|
||||
list.items.forEach(function forItem(item) {
|
||||
if (unorderedListStyleFor(item) !== style) {
|
||||
errors.push(item.lineNumber);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
},
|
||||
|
@ -124,16 +158,14 @@ module.exports = [
|
|||
"name": "MD005",
|
||||
"desc": "Inconsistent indentation for list items at the same level",
|
||||
"func": function MD005(params, errors) {
|
||||
var indentLevels = [];
|
||||
filterTokens(params.tokens, "list_item_open")
|
||||
.forEach(function forToken(token) {
|
||||
var indentLevel = indentFor(token);
|
||||
if (!indentLevels[token.level]) {
|
||||
indentLevels[token.level] = indentLevel;
|
||||
} else if (indentLevel !== indentLevels[token.level]) {
|
||||
errors.push(token.lineNumber);
|
||||
flattenLists(params.tokens).forEach(function forList(list) {
|
||||
var indent = indentFor(list.items[0]);
|
||||
list.items.forEach(function forItem(item) {
|
||||
if (indentFor(item) !== indent) {
|
||||
errors.push(item.lineNumber);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
|
@ -141,21 +173,9 @@ module.exports = [
|
|||
"name": "MD006",
|
||||
"desc": "Consider starting bulleted lists at the beginning of the line",
|
||||
"func": function MD006(params, errors) {
|
||||
var inList = 0;
|
||||
params.tokens.filter(function filterToken(token) {
|
||||
switch (token.type) {
|
||||
case "bullet_list_open":
|
||||
inList++;
|
||||
return inList === 1;
|
||||
case "bullet_list_close":
|
||||
inList--;
|
||||
return false;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}).forEach(function forToken(token) {
|
||||
if (indentFor(token) !== 0) {
|
||||
errors.push(token.lineNumber);
|
||||
flattenLists(params.tokens, false).forEach(function forList(list) {
|
||||
if (!list.nesting && indentFor(list.open)) {
|
||||
errors.push(list.open.lineNumber);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -167,20 +187,17 @@ module.exports = [
|
|||
"func": function MD007(params, errors) {
|
||||
var optionsIndent = params.options.indent || 2;
|
||||
var prevIndent = 0;
|
||||
filterTokens(params.tokens, "bullet_list_open")
|
||||
.forEach(function forToken(token) {
|
||||
var indent = indentFor(token);
|
||||
if ((indent > prevIndent) &&
|
||||
((indent - prevIndent) !== optionsIndent)) {
|
||||
errors.push(token.lineNumber);
|
||||
}
|
||||
prevIndent = indent;
|
||||
});
|
||||
flattenLists(params.tokens, false).forEach(function forList(list) {
|
||||
var indent = indentFor(list.open);
|
||||
if ((indent > prevIndent) &&
|
||||
((indent - prevIndent) !== optionsIndent)) {
|
||||
errors.push(list.open.lineNumber);
|
||||
}
|
||||
prevIndent = indent;
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
// MD008 does not exist
|
||||
|
||||
{
|
||||
"name": "MD009",
|
||||
"desc": "Trailing spaces",
|
||||
|
@ -378,21 +395,41 @@ module.exports = [
|
|||
"desc": "Ordered list item prefix",
|
||||
"func": function MD029(params, errors) {
|
||||
var style = params.options.style || "one";
|
||||
var number = 0;
|
||||
params.tokens.forEach(function forToken(token) {
|
||||
if (token.type === "ordered_list_open") {
|
||||
number = 1;
|
||||
} else if (token.type === "ordered_list_close") {
|
||||
number = 0;
|
||||
} else if ((token.type === "list_item_open") && number) {
|
||||
var regex = new RegExp("^\\s*" + String(number) + "\\. ");
|
||||
if (!regex.test(token.line)) {
|
||||
errors.push(token.lineNumber);
|
||||
flattenLists(params.tokens, true).forEach(function forList(list) {
|
||||
var number = 1;
|
||||
list.items.forEach(function forItem(item) {
|
||||
var re = new RegExp("^\\s*" + String(number) + "\\. ");
|
||||
if (!re.test(item.line)) {
|
||||
errors.push(item.lineNumber);
|
||||
}
|
||||
if (style === "ordered") {
|
||||
number++;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"name": "MD030",
|
||||
"desc": "Spaces after list markers",
|
||||
"func": function MD030(params, errors) {
|
||||
var ulSingle = params.options.ul_single || 1;
|
||||
var olSingle = params.options.ol_single || 1;
|
||||
var ulMulti = params.options.ul_multi || 1;
|
||||
var olMulti = params.options.ol_multi || 1;
|
||||
flattenLists(params.tokens).forEach(function forList(list) {
|
||||
var lineCount = list.lastLineIndex - list.open.lines[0];
|
||||
var allSingle = lineCount === list.items.length;
|
||||
var expectedSpaces = list.ordered ?
|
||||
(allSingle ? olSingle : olMulti) :
|
||||
(allSingle ? ulSingle : ulMulti);
|
||||
list.items.forEach(function forItem(item) {
|
||||
var match = /^\s*\S+(\s+)/.exec(item.line);
|
||||
if (match[1].length !== expectedSpaces) {
|
||||
errors.push(item.lineNumber);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue