Add MD032 with tests, improve infrastructure.

This commit is contained in:
David Anson 2015-02-24 23:35:34 -08:00
parent 160146ac3a
commit aef1524308
3 changed files with 111 additions and 1 deletions

View file

@ -3,6 +3,14 @@
var fs = require("fs"); var fs = require("fs");
var rules = require("./rules"); var rules = require("./rules");
function numberComparison(a, b) {
return a - b;
}
function uniqueFilterForSorted(value, index, array) {
return (index === 0) || (value > array[index - 1]);
}
function lintFile(file, options, callback) { function lintFile(file, options, callback) {
fs.readFile(file, { "encoding": "utf8" }, function readFile(err, contents) { fs.readFile(file, { "encoding": "utf8" }, function readFile(err, contents) {
if (err) { if (err) {
@ -14,7 +22,8 @@ function lintFile(file, options, callback) {
var rule = rules[name]; var rule = rules[name];
var errors = rule(lines); var errors = rule(lines);
if (errors.length) { if (errors.length) {
result[name] = errors; errors.sort(numberComparison);
result[name] = errors.filter(uniqueFilterForSorted);
} }
}); });
callback(null, result); callback(null, result);

View file

@ -26,5 +26,31 @@ module.exports = {
} }
}); });
return errors; return errors;
},
"MD032": function MD032(lines) {
// Some parsers have trouble detecting lists without surrounding
// whitespace, so examine the lines directly.
var errors = [];
var inList = false;
var inCode = false;
var prevLine = "";
lines.forEach(function forLine(line, lineNum) {
if (!inCode) {
var listMarker = line.trim().match(/^([\*\+\-]|(\d+\.))\s/);
if (listMarker && !inList && !prevLine.match(/^($|\s)/)) {
errors.push(lineNum + 1);
} else if (!listMarker && inList && !line.match(/^($|\s)/)) {
errors.push(lineNum);
}
inList = listMarker;
}
if (line.trim().match(/^(```|~~~)/)) {
inCode = !inCode;
inList = false;
}
prevLine = line;
});
return errors;
} }
}; };

View file

@ -0,0 +1,75 @@
* list (on first line)
text
* list
text
* list {MD032}
text
+ list {MD032}
text
- list {MD032}
text
1. list {MD032}
text
* list
* list {MD032}
text
text
10. list {MD032}
20. list
text
* list
* list
* list
text
* list
with hanging indent
* list
with hanging indent
* list
with hanging indent
Note: list without hanging indent violates MD032
* list
item with blank lines
* list
item with blank lines
text
```js
/*
* code block
* not a list
*/
```
text
* list {MD032}
``` {MD031}
code
```
text
```
code
``` {MD031}
* list {MD032}
text
* list (on last line without newline)