markdownlint/lib/md009.js

46 lines
1.4 KiB
JavaScript
Raw Normal View History

// @ts-check
"use strict";
const shared = require("./shared");
const trailingSpaceRe = /\s+$/;
module.exports = {
"names": [ "MD009", "no-trailing-spaces" ],
"description": "Trailing spaces",
"tags": [ "whitespace" ],
"function": function MD009(params, onError) {
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;
const listItemLineNumbers = [];
if (allowListItemEmptyLines) {
shared.filterTokens(params, "list_item_open", function forToken(token) {
for (let i = token.map[0]; i < token.map[1]; i++) {
listItemLineNumbers.push(i + 1);
}
});
}
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 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));
}
}
});
}
};