Enhance MD022/blanks-around-headings with lines_above/lines_below parameters (fixes #143).

This commit is contained in:
David Anson 2019-03-24 21:50:56 -07:00
parent debc08bca1
commit fa04d29485
24 changed files with 278 additions and 24 deletions

View file

@ -39,12 +39,12 @@ module.exports = {
nestingStyles[nesting] = itemStyle;
} else {
shared.addErrorDetailIf(onError, item.lineNumber,
nestingStyles[nesting], itemStyle, null,
nestingStyles[nesting], itemStyle, null, null,
shared.rangeFromRegExp(item.line, shared.listItemMarkerRe));
}
} else {
shared.addErrorDetailIf(onError, item.lineNumber,
expectedStyle, itemStyle, null,
expectedStyle, itemStyle, null, null,
shared.rangeFromRegExp(item.line, shared.listItemMarkerRe));
}
});

View file

@ -18,7 +18,7 @@ module.exports = {
const actualIndent = shared.indentFor(item);
if (list.unordered) {
shared.addErrorDetailIf(onError, item.lineNumber,
expectedIndent, actualIndent, null,
expectedIndent, actualIndent, null, null,
shared.rangeFromRegExp(item.line, shared.listItemMarkerRe));
} else {
const match = shared.orderedListItemMarkerRe.exec(item.line);

View file

@ -13,7 +13,7 @@ module.exports = {
shared.flattenLists().forEach(function forList(list) {
if (list.unordered && !list.nesting) {
shared.addErrorDetailIf(onError, list.open.lineNumber,
0, list.indent, null,
0, list.indent, null, null,
shared.rangeFromRegExp(list.open.line, shared.listItemMarkerRe));
}
});

View file

@ -13,7 +13,7 @@ module.exports = {
shared.flattenLists().forEach(function forList(list) {
if (list.unordered && list.parentsUnordered && list.indent) {
shared.addErrorDetailIf(onError, list.open.lineNumber,
list.parentIndent + optionsIndent, list.indent, null,
list.parentIndent + optionsIndent, list.indent, null, null,
shared.rangeFromRegExp(list.open.line, shared.listItemMarkerRe));
}
});

View file

@ -59,7 +59,7 @@ module.exports = {
longLineRe.test(line) &&
!labelRe.test(line)) {
shared.addErrorDetailIf(onError, lineNumber, lineLength,
line.length, null, shared.rangeFromRegExp(line, longLineRe));
line.length, null, null, shared.rangeFromRegExp(line, longLineRe));
}
});
}

View file

@ -3,18 +3,37 @@
"use strict";
const shared = require("./shared");
const { addErrorContext, filterTokens, isBlankLine } = shared;
const { addErrorDetailIf, filterTokens, isBlankLine } = shared;
module.exports = {
"names": [ "MD022", "blanks-around-headings", "blanks-around-headers" ],
"description": "Headings should be surrounded by blank lines",
"tags": [ "headings", "headers", "blank_lines" ],
"function": function MD022(params, onError) {
let linesAbove = params.config.lines_above;
if (linesAbove === undefined) {
linesAbove = 1;
}
let linesBelow = params.config.lines_below;
if (linesBelow === undefined) {
linesBelow = 1;
}
const { lines } = params;
filterTokens(params, "heading_open", (token) => {
const [ topIndex, nextIndex ] = token.map;
if (!isBlankLine(lines[topIndex - 1]) || !isBlankLine(lines[nextIndex])) {
addErrorContext(onError, topIndex + 1, lines[topIndex].trim());
for (let i = 0; i < linesAbove; i++) {
if (!isBlankLine(lines[topIndex - i - 1])) {
addErrorDetailIf(onError, topIndex + 1, linesAbove, i, "Above",
lines[topIndex].trim());
return;
}
}
for (let i = 0; i < linesBelow; i++) {
if (!isBlankLine(lines[nextIndex + i])) {
addErrorDetailIf(onError, topIndex + 1, linesBelow, i, "Below",
lines[topIndex].trim());
return;
}
}
});
}

View file

@ -29,7 +29,7 @@ module.exports = {
const match = shared.orderedListItemMarkerRe.exec(item.line);
shared.addErrorDetailIf(onError, item.lineNumber,
String(number), !match || match[1],
"Style: " + listStyleExamples[listStyle],
"Style: " + listStyleExamples[listStyle], null,
shared.rangeFromRegExp(item.line, shared.listItemMarkerRe));
if (listStyle === "ordered") {
number++;

View file

@ -22,7 +22,7 @@ module.exports = {
list.items.forEach(function forItem(item) {
const match = /^[\s>]*\S+(\s+)/.exec(item.line);
shared.addErrorDetailIf(onError, item.lineNumber,
expectedSpaces, (match ? match[1].length : 0), null,
expectedSpaces, (match ? match[1].length : 0), null, null,
shared.rangeFromRegExp(item.line, shared.listItemMarkerRe));
});
});

View file

@ -30,7 +30,7 @@ module.exports = {
const lineNumber = token.lineNumber + index + fenceOffset;
const range = [ match.index + 1, wordMatch.length ];
shared.addErrorDetailIf(onError, lineNumber,
name, match[1], null, range);
name, match[1], null, null, range);
}
}
}

View file

@ -328,14 +328,14 @@ module.exports.addError = addError;
// Adds an error object with details conditionally via the onError callback
module.exports.addErrorDetailIf = function addErrorDetailIf(
onError, lineNumber, expected, actual, detail, range) {
onError, lineNumber, expected, actual, detail, context, range) {
if (expected !== actual) {
addError(
onError,
lineNumber,
"Expected: " + expected + "; Actual: " + actual +
(detail ? "; " + detail : ""),
null,
context,
range);
}
};