Add MD030 with tests, refactor list handling with flattenLists.

This commit is contained in:
David Anson 2015-03-10 23:10:06 -07:00
parent b21548a992
commit 2b289ab5f3
4 changed files with 177 additions and 56 deletions

View file

@ -58,7 +58,7 @@
"no-multiple-empty-lines": [2, { "max": 2 }],
"no-native-reassign": 2,
"no-negated-in-lhs": 2,
"no-nested-ternary": 2,
"no-nested-ternary": 0,
"no-new": 2,
"no-new-func": 2,
"no-new-object": 2,

View file

@ -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);
}
});
});
}
},

View file

@ -0,0 +1,10 @@
{
"default": true,
"MD007": {
"indent": 4
},
"MD030": {
"ul_multi": 3,
"ol_multi": 2
}
}

View file

@ -0,0 +1,74 @@
Normal list
* Foo
* Bar
* Baz
List with incorrect spacing
* Foo {MD030}
* Bar {MD030}
* Baz {MD030}
List with children:
* Foo {MD030}
* Bar {MD030}
* Baz
List with children and correct spacing:
* Foo
* Bar
* Baz (This sublist has no children)
List with Multiple paragraphs and correct spacing
* Foo
Here is the second paragraph
* All items in the list need the same indent
List with multiple paragraphs and incorrect spacing
* Foo {MD030}
Here is the second paragraph
* Bar {MD030}
List with code blocks:
* Foo
Here is some code
* Bar
Ordered lists:
1. Foo
1. Bar
1. Baz
And with incorrect spacing:
1. Foo {MD030}
1. Bar {MD030}
1. Baz {MD030}
Ordered lists with children:
1. Foo {MD030}
* Hi
1. Bar {MD030}
1. Baz {MD030}
Ordered lists with children (correct spacing), and with something other than
the first item determining that the entire list has children:
1. Foo
1. Bar
* Hi
1. Baz