Update MD014/commands-show-output to allow blanks and output-less commands (fixes #241).

This commit is contained in:
David Anson 2020-01-18 21:28:42 -08:00
parent 6553a13b6c
commit 9ac06456d1
4 changed files with 78 additions and 44 deletions

View file

@ -6,24 +6,6 @@ const { addErrorContext, filterTokens } = require("../helpers");
const dollarCommandRe = /^(\s*)(\$\s+)/;
function addErrorIfPreviousWasCommand(onError, previous) {
if (previous) {
const { lineNumber, lineTrim, column, length } = previous;
addErrorContext(
onError,
lineNumber,
lineTrim,
null,
null,
[ column, length ],
{
"editColumn": column,
"deleteCount": length
}
);
}
}
module.exports = {
"names": [ "MD014", "commands-show-output" ],
"description": "Dollar signs used before commands without showing output",
@ -31,23 +13,40 @@ module.exports = {
"function": function MD014(params, onError) {
[ "code_block", "fence" ].forEach((type) => {
filterTokens(params, type, (token) => {
let previous = null;
const margin = (token.type === "fence") ? 1 : 0;
const dollarInstances = [];
let allDollars = true;
for (let i = token.map[0] + margin; i < token.map[1] - margin; i++) {
const line = params.lines[i];
const lineTrim = line.trim();
const match = dollarCommandRe.exec(line);
if (!lineTrim || match) {
addErrorIfPreviousWasCommand(onError, previous);
if (lineTrim) {
const match = dollarCommandRe.exec(line);
if (match) {
const column = match[1].length + 1;
const length = match[2].length;
dollarInstances.push([ i, lineTrim, column, length ]);
} else {
allDollars = false;
}
}
previous = match ? {
"lineNumber": i + 1,
"lineTrim": lineTrim,
"column": match[1].length + 1,
"length": match[2].length
} : null;
}
addErrorIfPreviousWasCommand(onError, previous);
if (allDollars) {
dollarInstances.forEach((instance) => {
const [ i, lineTrim, column, length ] = instance;
addErrorContext(
onError,
i + 1,
lineTrim,
null,
null,
[ column, length ],
{
"editColumn": column,
"deleteCount": length
}
);
});
}
});
});
}