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"; "use strict";
const { addErrorContext, filterTokens, newLineRe, rangeFromRegExp } = const { addErrorContext, filterTokens } = require("../helpers");
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 = { module.exports = {
"names": [ "MD014", "commands-show-output" ], "names": [ "MD014", "commands-show-output" ],
"description": "Dollar signs used before commands without showing output", "description": "Dollar signs used before commands without showing output",
"tags": [ "code" ], "tags": [ "code" ],
"function": function MD014(params, onError) { "function": function MD014(params, onError) {
[ "code_block", "fence" ].forEach(function forType(type) { [ "code_block", "fence" ].forEach((type) => {
filterTokens(params, type, function forToken(token) { filterTokens(params, type, (token) => {
let allBlank = true; let previous = null;
if (token.content && token.content.split(newLineRe) const margin = (token.type === "fence") ? 1 : 0;
.every(function forLine(line) { for (let i = token.map[0] + margin; i < token.map[1] - margin; i++) {
return !line || (allBlank = false) || dollarCommandRe.test(line); const line = params.lines[i];
}) && !allBlank) { const lineTrim = line.trim();
addErrorContext(onError, token.lineNumber, const match = dollarCommandRe.exec(line);
token.content.split(newLineRe)[0].trim(), null, null, if (!lineTrim || match) {
rangeFromRegExp(token.line, dollarCommandRe)); 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: The following code block shouldn't have $ before the commands:
$ ls {MD014} $ ls {MD014}
$ less foo $ less foo {MD014}
$ cat bar $ cat bar {MD014}
However the following code block shows output, and $ can be used to However the following code block shows output, and $ can be used to
distinguish between command and output: distinguish between command and output:
@ -31,3 +31,19 @@ shouldn't fire: {MD046:32}
```bash ```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 # heading
```fence ```fence
$ code $ code {MD014}
``` ```
text text
```fence ```fence
$ code $ code {MD014}
``` ```
text text
```fence ```fence
$ code $ code {MD014}
$ code $ code {MD014}
```
text
```fence
$ code {MD014}
$ code {MD014}
``` ```
text text
```fence ```fence
$ code $ code
code
$ code $ code
code
``` ```
text text
```sh ```sh
$ npm install --save multimatch $ npm install --save multimatch {MD014}
``` ```
text 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 ## 123456789 123456789 123456789 123456789 123456789 123456789
$ command with no output command with no output
## No space A ## No space A