Update MD014/commands-show-output to report each violation, include fix information (fixes #217).

This commit is contained in:
David Anson 2019-10-01 22:12:12 -07:00
parent 1edb3f0a78
commit c4f77e021d
4 changed files with 81 additions and 33 deletions

View file

@ -2,27 +2,52 @@
"use strict";
const { addErrorContext, filterTokens, newLineRe, rangeFromRegExp } =
require("../helpers");
const { addErrorContext, filterTokens } = require("../helpers");
const dollarCommandRe = /^(\s*)(\$\s)/;
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",
"tags": [ "code" ],
"function": function MD014(params, onError) {
[ "code_block", "fence" ].forEach(function forType(type) {
filterTokens(params, type, function forToken(token) {
let allBlank = true;
if (token.content && token.content.split(newLineRe)
.every(function forLine(line) {
return !line || (allBlank = false) || dollarCommandRe.test(line);
}) && !allBlank) {
addErrorContext(onError, token.lineNumber,
token.content.split(newLineRe)[0].trim(), null, null,
rangeFromRegExp(token.line, dollarCommandRe));
[ "code_block", "fence" ].forEach((type) => {
filterTokens(params, type, (token) => {
let previous = null;
const margin = (token.type === "fence") ? 1 : 0;
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);
}
previous = match ? {
"lineNumber": i + 1,
"lineTrim": lineTrim,
"column": match[1].length + 1,
"length": match[2].length
} : null;
}
addErrorIfPreviousWasCommand(onError, previous);
});
});
}

View file

@ -1,9 +1,9 @@
The following code block shouldn't have $ before the commands:
$ ls {MD014}
$ less foo
$ less foo {MD014}
$ cat bar
$ cat bar {MD014}
However the following code block shows output, and $ can be used to
distinguish between command and output:
@ -31,3 +31,19 @@ shouldn't fire: {MD046:32}
```bash
```
Mixed content:
$ ls
file.md other.md
$ git branch {MD014}
$ cat stuff {MD014}
output
$ ls {MD014}
$ git branch {MD014}
$ cat stuff
stuff here
more stuff
$ tail cat
meow

View file

@ -1,36 +1,43 @@
# heading
```fence
$ code
```
text
```fence
$ code
```
text
```fence
$ code
$ code
$ code {MD014}
```
text
```fence
$ code {MD014}
```
text
```fence
$ code {MD014}
$ code {MD014}
```
text
```fence
$ code {MD014}
$ code {MD014}
```
text
```fence
$ code
code
$ code
code
```
text
```sh
$ npm install --save multimatch
$ npm install --save multimatch {MD014}
```
text
{MD014:3} {MD014:9} {MD014:15} {MD014:22} {MD014:29}

View file

@ -8,7 +8,7 @@ A [reversed](link) example.
## 123456789 123456789 123456789 123456789 123456789 123456789
$ command with no output
command with no output
## No space A