Update MD009/no-trailing-spaces to include strict mode (fixes #216).

This commit is contained in:
David Anson 2019-12-09 22:05:57 -08:00
parent a9251c533f
commit 6f3c67f760
10 changed files with 193 additions and 11 deletions

View file

@ -2,10 +2,14 @@
"use strict";
const { addError, filterTokens, forEachLine, includesSorted } =
require("../helpers");
const { addError, filterTokens, forEachInlineCodeSpan, forEachLine,
includesSorted, newLineRe } = require("../helpers");
const { lineMetadata } = require("./cache");
function numericSortAscending(a, b) {
return a - b;
}
module.exports = {
"names": [ "MD009", "no-trailing-spaces" ],
"description": "Trailing spaces",
@ -15,17 +19,38 @@ module.exports = {
if (brSpaces === undefined) {
brSpaces = 2;
}
const listItemEmptyLines = params.config.list_item_empty_lines;
const allowListItemEmptyLines =
(listItemEmptyLines === undefined) ? false : !!listItemEmptyLines;
const listItemEmptyLines = !!params.config.list_item_empty_lines;
const strict = !!params.config.strict;
const listItemLineNumbers = [];
if (allowListItemEmptyLines) {
if (listItemEmptyLines) {
filterTokens(params, "list_item_open", (token) => {
for (let i = token.map[0]; i < token.map[1]; i++) {
listItemLineNumbers.push(i + 1);
}
});
listItemLineNumbers.sort((a, b) => a - b);
listItemLineNumbers.sort(numericSortAscending);
}
const paragraphLineNumbers = [];
const codeInlineLineNumbers = [];
if (strict) {
filterTokens(params, "paragraph_open", (token) => {
for (let i = token.map[0]; i < token.map[1] - 1; i++) {
paragraphLineNumbers.push(i + 1);
}
});
paragraphLineNumbers.sort(numericSortAscending);
filterTokens(params, "inline", (token) => {
if (token.children.some((child) => child.type === "code_inline")) {
const tokenLines = params.lines.slice(token.map[0], token.map[1]);
forEachInlineCodeSpan(tokenLines.join("\n"), (code, lineIndex) => {
const codeLineCount = code.split(newLineRe).length;
for (let i = 0; i < codeLineCount; i++) {
codeInlineLineNumbers.push(token.lineNumber + lineIndex + i);
}
});
}
});
codeInlineLineNumbers.sort(numericSortAscending);
}
const expected = (brSpaces < 2) ? 0 : brSpaces;
let inFencedCode = 0;
@ -35,7 +60,10 @@ module.exports = {
const trailingSpaces = line.length - line.trimRight().length;
if ((!inCode || inFencedCode) && trailingSpaces &&
!includesSorted(listItemLineNumbers, lineNumber)) {
if (expected !== trailingSpaces) {
if ((expected !== trailingSpaces) ||
(strict &&
(!includesSorted(paragraphLineNumbers, lineNumber) ||
includesSorted(codeInlineLineNumbers, lineNumber)))) {
const column = line.length - trailingSpaces + 1;
addError(
onError,