From 3e1317709a2f05ac92537b89ba60eab53fdeaa34 Mon Sep 17 00:00:00 2001 From: David Anson Date: Mon, 30 Apr 2018 21:34:19 -0700 Subject: [PATCH] Update MD009/no-trailing-spaces default configuration to allow 2 spaces for hard break (fixes #114). --- doc/Rules.md | 20 +++++++++---------- lib/md009.js | 18 ++++++++++++----- schema/build-config-schema.js | 2 +- schema/markdownlint-config-schema.json | 4 ++-- .../detailed-results-MD001-MD010.results.json | 2 +- test/detailed-results-front-matter.json | 3 +++ test/trailing_spaces_br.json | 6 ------ 7 files changed, 29 insertions(+), 26 deletions(-) delete mode 100644 test/trailing_spaces_br.json diff --git a/doc/Rules.md b/doc/Rules.md index 43fc23bb..fedfaaf0 100644 --- a/doc/Rules.md +++ b/doc/Rules.md @@ -288,19 +288,17 @@ Tags: whitespace Aliases: no-trailing-spaces -Parameters: br_spaces, list_item_empty_lines (number; default 0, boolean; default false) +Parameters: br_spaces, list_item_empty_lines (number; default 2, boolean; default false) -This rule is triggered on any lines that end with whitespace. To fix this, -find the line that is triggered and remove any trailing spaces from the end. +This rule is triggered on any lines that end with unexpected whitespace. To fix this, +remove the trailing space from the end of the line. -The `br_spaces` parameter allows an exception to this rule for a specific amount -of trailing spaces used to insert an explicit line break/br element. For -example, set `br_spaces` to 2 to allow exactly 2 spaces at the end of a line. +The `br_spaces` parameter allows an exception to this rule for a specific number +of trailing spaces, typically used to insert an explicit line break. The default +value allows 2 spaces to indicate a hard break (\
element). -Note: you have to set `br_spaces` to 2 or higher for this exception to take -effect - you can't insert a br element with just a single trailing space, so -if you set `br_spaces` to 1, the exception will be disabled, just as if it was -set to the default of 0. +Note: You must set `br_spaces` to a value >= 2 for this parameter to take effect. +Setting `br_spaces` to 1 behaves the same as 0, disallowing any trailing spaces. Using spaces to indent blank lines inside a list item is usually not necessary, but some parsers require it. Set the `list_item_empty_lines` parameter to `true` @@ -308,7 +306,7 @@ to allow this: ```markdown - list item text - + [2 spaces] list item text ``` diff --git a/lib/md009.js b/lib/md009.js index d6456056..2905c60c 100644 --- a/lib/md009.js +++ b/lib/md009.js @@ -11,7 +11,10 @@ module.exports = { "description": "Trailing spaces", "tags": [ "whitespace" ], "function": function MD009(params, onError) { - const brSpaces = params.config.br_spaces || 0; + let brSpaces = params.config.br_spaces; + if (brSpaces === undefined) { + brSpaces = 2; + } const listItemEmptyLines = params.config.list_item_empty_lines; const allowListItemEmptyLines = (listItemEmptyLines === undefined) ? false : !!listItemEmptyLines; @@ -23,14 +26,19 @@ module.exports = { } }); } + const expected = (brSpaces < 2) ? 0 : brSpaces; shared.forEachLine(function forLine(line, lineIndex) { const lineNumber = lineIndex + 1; if (trailingSpaceRe.test(line) && (listItemLineNumbers.indexOf(lineNumber) === -1)) { - const expected = (brSpaces < 2) ? 0 : brSpaces; - shared.addErrorDetailIf(onError, lineNumber, - expected, line.length - shared.trimRight(line).length, null, - shared.rangeFromRegExp(line, trailingSpaceRe)); + const actual = line.length - shared.trimRight(line).length; + if (expected !== actual) { + shared.addError(onError, lineNumber, + "Expected: " + (expected === 0 ? "" : "0 or ") + + expected + "; Actual: " + actual, + null, + shared.rangeFromRegExp(line, trailingSpaceRe)); + } } }); } diff --git a/schema/build-config-schema.js b/schema/build-config-schema.js index 1073bf25..b8037952 100644 --- a/schema/build-config-schema.js +++ b/schema/build-config-schema.js @@ -95,7 +95,7 @@ rules.forEach(function forRule(rule) { "br_spaces": { "description": "Spaces for line break", "type": "integer", - "default": 0 + "default": 2 }, "list_item_empty_lines": { "description": "Allow spaces for empty lines in list items", diff --git a/schema/markdownlint-config-schema.json b/schema/markdownlint-config-schema.json index 42213196..e9e4fcdd 100644 --- a/schema/markdownlint-config-schema.json +++ b/schema/markdownlint-config-schema.json @@ -256,7 +256,7 @@ "br_spaces": { "description": "Spaces for line break", "type": "integer", - "default": 0 + "default": 2 }, "list_item_empty_lines": { "description": "Allow spaces for empty lines in list items", @@ -277,7 +277,7 @@ "br_spaces": { "description": "Spaces for line break", "type": "integer", - "default": 0 + "default": 2 }, "list_item_empty_lines": { "description": "Allow spaces for empty lines in list items", diff --git a/test/detailed-results-MD001-MD010.results.json b/test/detailed-results-MD001-MD010.results.json index 55cb4488..9c6918e4 100644 --- a/test/detailed-results-MD001-MD010.results.json +++ b/test/detailed-results-MD001-MD010.results.json @@ -59,7 +59,7 @@ "lineNumber": 15, "ruleNames": [ "MD009", "no-trailing-spaces" ], "ruleDescription": "Trailing spaces", - "errorDetail": "Expected: 0; Actual: 1", + "errorDetail": "Expected: 0 or 2; Actual: 1", "errorContext": null, "errorRange": [5, 1] }, diff --git a/test/detailed-results-front-matter.json b/test/detailed-results-front-matter.json index 054e8566..636b736f 100644 --- a/test/detailed-results-front-matter.json +++ b/test/detailed-results-front-matter.json @@ -1,4 +1,7 @@ { "default": true, + "MD009": { + "br_spaces": 0 + }, "MD041": true } diff --git a/test/trailing_spaces_br.json b/test/trailing_spaces_br.json deleted file mode 100644 index 50296faa..00000000 --- a/test/trailing_spaces_br.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "default": true, - "MD009": { - "br_spaces": 2 - } -}