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