Update MD009/no-trailing-spaces default configuration to allow 2 spaces for hard break (fixes #114).

This commit is contained in:
David Anson 2018-04-30 21:34:19 -07:00
parent 36dc946f46
commit 3e1317709a
7 changed files with 29 additions and 26 deletions

View file

@ -288,19 +288,17 @@ Tags: whitespace
Aliases: no-trailing-spaces 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, This rule is triggered on any lines that end with unexpected whitespace. To fix this,
find the line that is triggered and remove any trailing spaces from the end. remove the trailing space from the end of the line.
The `br_spaces` parameter allows an exception to this rule for a specific amount The `br_spaces` parameter allows an exception to this rule for a specific number
of trailing spaces used to insert an explicit line break/br element. For of trailing spaces, typically used to insert an explicit line break. The default
example, set `br_spaces` to 2 to allow exactly 2 spaces at the end of a line. value allows 2 spaces to indicate a hard break (\<br> element).
Note: you have to set `br_spaces` to 2 or higher for this exception to take Note: You must set `br_spaces` to a value >= 2 for this parameter to take effect.
effect - you can't insert a br element with just a single trailing space, so Setting `br_spaces` to 1 behaves the same as 0, disallowing any trailing spaces.
if you set `br_spaces` to 1, the exception will be disabled, just as if it was
set to the default of 0.
Using spaces to indent blank lines inside a list item is usually not necessary, 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` but some parsers require it. Set the `list_item_empty_lines` parameter to `true`
@ -308,7 +306,7 @@ to allow this:
```markdown ```markdown
- list item text - list item text
[2 spaces]
list item text list item text
``` ```

View file

@ -11,7 +11,10 @@ module.exports = {
"description": "Trailing spaces", "description": "Trailing spaces",
"tags": [ "whitespace" ], "tags": [ "whitespace" ],
"function": function MD009(params, onError) { "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 listItemEmptyLines = params.config.list_item_empty_lines;
const allowListItemEmptyLines = const allowListItemEmptyLines =
(listItemEmptyLines === undefined) ? false : !!listItemEmptyLines; (listItemEmptyLines === undefined) ? false : !!listItemEmptyLines;
@ -23,15 +26,20 @@ module.exports = {
} }
}); });
} }
const expected = (brSpaces < 2) ? 0 : brSpaces;
shared.forEachLine(function forLine(line, lineIndex) { shared.forEachLine(function forLine(line, lineIndex) {
const lineNumber = lineIndex + 1; const lineNumber = lineIndex + 1;
if (trailingSpaceRe.test(line) && if (trailingSpaceRe.test(line) &&
(listItemLineNumbers.indexOf(lineNumber) === -1)) { (listItemLineNumbers.indexOf(lineNumber) === -1)) {
const expected = (brSpaces < 2) ? 0 : brSpaces; const actual = line.length - shared.trimRight(line).length;
shared.addErrorDetailIf(onError, lineNumber, if (expected !== actual) {
expected, line.length - shared.trimRight(line).length, null, shared.addError(onError, lineNumber,
"Expected: " + (expected === 0 ? "" : "0 or ") +
expected + "; Actual: " + actual,
null,
shared.rangeFromRegExp(line, trailingSpaceRe)); shared.rangeFromRegExp(line, trailingSpaceRe));
} }
}
}); });
} }
}; };

View file

@ -95,7 +95,7 @@ rules.forEach(function forRule(rule) {
"br_spaces": { "br_spaces": {
"description": "Spaces for line break", "description": "Spaces for line break",
"type": "integer", "type": "integer",
"default": 0 "default": 2
}, },
"list_item_empty_lines": { "list_item_empty_lines": {
"description": "Allow spaces for empty lines in list items", "description": "Allow spaces for empty lines in list items",

View file

@ -256,7 +256,7 @@
"br_spaces": { "br_spaces": {
"description": "Spaces for line break", "description": "Spaces for line break",
"type": "integer", "type": "integer",
"default": 0 "default": 2
}, },
"list_item_empty_lines": { "list_item_empty_lines": {
"description": "Allow spaces for empty lines in list items", "description": "Allow spaces for empty lines in list items",
@ -277,7 +277,7 @@
"br_spaces": { "br_spaces": {
"description": "Spaces for line break", "description": "Spaces for line break",
"type": "integer", "type": "integer",
"default": 0 "default": 2
}, },
"list_item_empty_lines": { "list_item_empty_lines": {
"description": "Allow spaces for empty lines in list items", "description": "Allow spaces for empty lines in list items",

View file

@ -59,7 +59,7 @@
"lineNumber": 15, "lineNumber": 15,
"ruleNames": [ "MD009", "no-trailing-spaces" ], "ruleNames": [ "MD009", "no-trailing-spaces" ],
"ruleDescription": "Trailing spaces", "ruleDescription": "Trailing spaces",
"errorDetail": "Expected: 0; Actual: 1", "errorDetail": "Expected: 0 or 2; Actual: 1",
"errorContext": null, "errorContext": null,
"errorRange": [5, 1] "errorRange": [5, 1]
}, },

View file

@ -1,4 +1,7 @@
{ {
"default": true, "default": true,
"MD009": {
"br_spaces": 0
},
"MD041": true "MD041": true
} }

View file

@ -1,6 +0,0 @@
{
"default": true,
"MD009": {
"br_spaces": 2
}
}