Refactor rules for clarity/brevity.

This commit is contained in:
David Anson 2015-03-04 18:23:19 -08:00
parent c864ac1b96
commit 5d641bbf24

View file

@ -1,10 +1,10 @@
"use strict";
function indentFrom(token) {
function indentFor(token) {
return token.line.length - token.line.trimLeft().length;
}
function headingStyleFrom(token) {
function headingStyleFor(token) {
if ((token.lines[1] - token.lines[0]) === 1) {
if (token.line.match(/#\s*$/)) {
return "atx_closed";
@ -14,7 +14,7 @@ function headingStyleFrom(token) {
return "setext";
}
function unorderedListStyleFrom(token) {
function unorderedListStyleFor(token) {
switch (token.line.trimLeft().substr(0, 1)) {
case "*":
return "asterisk";
@ -27,6 +27,12 @@ function unorderedListStyleFrom(token) {
}
}
function filterTokens(tokens, tokenA, tokenB) {
return tokens.filter(function filterToken(token) {
return ((token.type === tokenA) || (token.type === tokenB));
});
}
function padAndTrim(lines) {
return [].concat(
"",
@ -42,14 +48,13 @@ module.exports = [
"desc": "Header levels should only increment by one level at a time",
"func": function MD001(params, errors) {
var prevLevel = 0;
params.tokens.filter(function filterToken(token) {
return (token.type === "heading_open");
}).forEach(function forToken(token) {
if (prevLevel && (token.hLevel > prevLevel + 1)) {
errors.push(token.lineNumber);
}
prevLevel = token.hLevel;
});
filterTokens(params.tokens, "heading_open")
.forEach(function forToken(token) {
if (prevLevel && (token.hLevel > prevLevel + 1)) {
errors.push(token.lineNumber);
}
prevLevel = token.hLevel;
});
}
},
@ -74,14 +79,12 @@ module.exports = [
"desc": "Header style",
"func": function MD003(params, errors) {
var style = params.options.style || "consistent";
var headings = params.tokens.filter(function filterToken(token) {
return (token.type === "heading_open");
});
var headings = filterTokens(params.tokens, "heading_open");
if ((style === "consistent") && headings.length) {
style = headingStyleFrom(headings[0]);
style = headingStyleFor(headings[0]);
}
headings.forEach(function forToken(token) {
if (headingStyleFrom(token) !== style) {
if (headingStyleFor(token) !== style) {
errors.push(token.lineNumber);
}
});
@ -93,14 +96,12 @@ module.exports = [
"desc": "Unordered list style",
"func": function MD004(params, errors) {
var style = params.options.style || "consistent";
var listItems = params.tokens.filter(function filterToken(token) {
return (token.type === "list_item_open");
});
var listItems = filterTokens(params.tokens, "list_item_open");
if ((style === "consistent") && listItems.length) {
style = unorderedListStyleFrom(listItems[0]);
style = unorderedListStyleFor(listItems[0]);
}
listItems.forEach(function forToken(token) {
if (unorderedListStyleFrom(token) !== style) {
if (unorderedListStyleFor(token) !== style) {
errors.push(token.lineNumber);
}
});
@ -112,16 +113,15 @@ module.exports = [
"desc": "Inconsistent indentation for list items at the same level",
"func": function MD005(params, errors) {
var indentLevels = [];
params.tokens.filter(function filterToken(token) {
return (token.type === "list_item_open");
}).forEach(function forToken(token) {
var indentLevel = indentFrom(token);
if (!indentLevels[token.level]) {
indentLevels[token.level] = indentLevel;
} else if (indentLevel !== indentLevels[token.level]) {
errors.push(token.lineNumber);
}
});
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);
}
});
}
},
@ -142,7 +142,7 @@ module.exports = [
return false;
}
}).forEach(function forToken(token) {
if (indentFrom(token) !== 0) {
if (indentFor(token) !== 0) {
errors.push(token.lineNumber);
}
});
@ -155,16 +155,15 @@ module.exports = [
"func": function MD007(params, errors) {
var optionsIndent = params.options.indent || 2;
var prevIndent = 0;
params.tokens.filter(function filterToken(token) {
return (token.type === "bullet_list_open");
}).forEach(function forToken(token) {
var indent = indentFrom(token);
if ((indent > prevIndent) &&
((indent - prevIndent) !== optionsIndent)) {
errors.push(token.lineNumber);
}
prevIndent = indent;
});
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;
});
}
},
@ -198,17 +197,15 @@ module.exports = [
"name": "MD011",
"desc": "Reversed link syntax",
"func": function MD011(params, errors) {
params.tokens.filter(function filterToken(token) {
return (token.type === "inline");
}).forEach(function forToken(token) {
token.children.filter(function filterChild(child) {
return (child.type === "text");
}).forEach(function forChild(child) {
if (child.content.match(/\([^)]+\)\[[^\]]+\]/)) {
errors.push(token.lineNumber);
}
filterTokens(params.tokens, "inline")
.forEach(function forToken(token) {
filterTokens(token.children, "text")
.forEach(function forChild(child) {
if (child.content.match(/\([^)]+\)\[[^\]]+\]/)) {
errors.push(token.lineNumber);
}
});
});
});
}
},
@ -217,13 +214,12 @@ module.exports = [
"desc": "Multiple consecutive blank lines",
"func": function MD012(params, errors) {
var exclusions = [];
params.tokens.filter(function filterToken(token) {
return ((token.type === "code_block") || (token.type === "fence"));
}).forEach(function forToken(token) {
for (var i = token.lines[0] ; i < token.lines[1] ; i++) {
exclusions.push(i);
}
});
filterTokens(params.tokens, "code_block", "fence")
.forEach(function forToken(token) {
for (var i = token.lines[0] ; i < token.lines[1] ; i++) {
exclusions.push(i);
}
});
var prevLine = "-";
params.lines.forEach(function forLine(line, lineIndex) {
line = line.trim();