diff --git a/lib/md014.js b/lib/md014.js index 30cbc1d7..d1c9a228 100644 --- a/lib/md014.js +++ b/lib/md014.js @@ -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); }); }); } diff --git a/test/code_block_dollar.md b/test/code_block_dollar.md index a97be4a2..6e98f66f 100644 --- a/test/code_block_dollar.md +++ b/test/code_block_dollar.md @@ -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 diff --git a/test/code_block_dollar_fence.md b/test/code_block_dollar_fence.md index ff60984b..0cb72382 100644 --- a/test/code_block_dollar_fence.md +++ b/test/code_block_dollar_fence.md @@ -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} diff --git a/test/detailed-results-MD011-MD021.md.fixed b/test/detailed-results-MD011-MD021.md.fixed index 407f771c..069e7122 100644 --- a/test/detailed-results-MD011-MD021.md.fixed +++ b/test/detailed-results-MD011-MD021.md.fixed @@ -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