2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2024-08-12 23:24:32 -07:00
|
|
|
const { addError } = require("../helpers");
|
2024-09-28 16:26:38 -07:00
|
|
|
const { addRangeToSet } = require("../helpers/micromark-helpers.cjs");
|
2024-08-24 22:05:16 -07:00
|
|
|
const { filterByTypesCached } = require("./cache");
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2024-02-27 20:42:09 -08:00
|
|
|
// eslint-disable-next-line jsdoc/valid-types
|
|
|
|
/** @type import("./markdownlint").Rule */
|
2018-01-21 21:44:25 -08:00
|
|
|
module.exports = {
|
|
|
|
"names": [ "MD009", "no-trailing-spaces" ],
|
|
|
|
"description": "Trailing spaces",
|
|
|
|
"tags": [ "whitespace" ],
|
2024-08-12 23:24:32 -07:00
|
|
|
"parser": "micromark",
|
2018-01-21 21:44:25 -08:00
|
|
|
"function": function MD009(params, onError) {
|
2018-04-30 21:34:19 -07:00
|
|
|
let brSpaces = params.config.br_spaces;
|
2020-01-25 18:40:39 -08:00
|
|
|
brSpaces = Number((brSpaces === undefined) ? 2 : brSpaces);
|
2019-12-09 22:05:57 -08:00
|
|
|
const listItemEmptyLines = !!params.config.list_item_empty_lines;
|
|
|
|
const strict = !!params.config.strict;
|
2024-08-12 23:24:32 -07:00
|
|
|
const codeBlockLineNumbers = new Set();
|
2024-08-24 22:05:16 -07:00
|
|
|
for (const codeBlock of filterByTypesCached([ "codeFenced" ])) {
|
2024-08-12 23:24:32 -07:00
|
|
|
addRangeToSet(codeBlockLineNumbers, codeBlock.startLine + 1, codeBlock.endLine - 1);
|
|
|
|
}
|
2024-08-24 22:05:16 -07:00
|
|
|
for (const codeBlock of filterByTypesCached([ "codeIndented" ])) {
|
2024-08-12 23:24:32 -07:00
|
|
|
addRangeToSet(codeBlockLineNumbers, codeBlock.startLine, codeBlock.endLine);
|
|
|
|
}
|
|
|
|
const listItemLineNumbers = new Set();
|
2019-12-09 22:05:57 -08:00
|
|
|
if (listItemEmptyLines) {
|
2024-08-24 22:05:16 -07:00
|
|
|
for (const listBlock of filterByTypesCached([ "listOrdered", "listUnordered" ])) {
|
2024-08-12 23:24:32 -07:00
|
|
|
addRangeToSet(listItemLineNumbers, listBlock.startLine, listBlock.endLine);
|
|
|
|
let trailingIndent = true;
|
|
|
|
for (let i = listBlock.children.length - 1; i >= 0; i--) {
|
|
|
|
const child = listBlock.children[i];
|
|
|
|
switch (child.type) {
|
|
|
|
case "content":
|
|
|
|
trailingIndent = false;
|
|
|
|
break;
|
|
|
|
case "listItemIndent":
|
|
|
|
if (trailingIndent) {
|
|
|
|
listItemLineNumbers.delete(child.startLine);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "listItemPrefix":
|
|
|
|
trailingIndent = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
2024-08-12 23:24:32 -07:00
|
|
|
}
|
2019-12-09 22:05:57 -08:00
|
|
|
}
|
2024-08-12 23:24:32 -07:00
|
|
|
const paragraphLineNumbers = new Set();
|
|
|
|
const codeInlineLineNumbers = new Set();
|
2019-12-09 22:05:57 -08:00
|
|
|
if (strict) {
|
2024-08-24 22:05:16 -07:00
|
|
|
for (const paragraph of filterByTypesCached([ "paragraph" ])) {
|
2024-08-12 23:24:32 -07:00
|
|
|
addRangeToSet(paragraphLineNumbers, paragraph.startLine, paragraph.endLine - 1);
|
|
|
|
}
|
2024-08-24 22:05:16 -07:00
|
|
|
for (const codeText of filterByTypesCached([ "codeText" ])) {
|
2024-08-12 23:24:32 -07:00
|
|
|
addRangeToSet(codeInlineLineNumbers, codeText.startLine, codeText.endLine - 1);
|
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
2018-04-30 21:34:19 -07:00
|
|
|
const expected = (brSpaces < 2) ? 0 : brSpaces;
|
2024-08-12 23:24:32 -07:00
|
|
|
for (let lineIndex = 0; lineIndex < params.lines.length; lineIndex++) {
|
|
|
|
const line = params.lines[lineIndex];
|
2018-04-27 22:05:34 -07:00
|
|
|
const lineNumber = lineIndex + 1;
|
2021-02-06 19:55:22 -08:00
|
|
|
const trailingSpaces = line.length - line.trimEnd().length;
|
|
|
|
if (
|
|
|
|
trailingSpaces &&
|
2024-08-12 23:24:32 -07:00
|
|
|
!codeBlockLineNumbers.has(lineNumber) &&
|
|
|
|
!listItemLineNumbers.has(lineNumber) &&
|
2021-02-06 19:55:22 -08:00
|
|
|
(
|
|
|
|
(expected !== trailingSpaces) ||
|
|
|
|
(strict &&
|
2024-08-12 23:24:32 -07:00
|
|
|
(!paragraphLineNumbers.has(lineNumber) ||
|
|
|
|
codeInlineLineNumbers.has(lineNumber)))
|
2021-02-06 19:55:22 -08:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
const column = line.length - trailingSpaces + 1;
|
|
|
|
addError(
|
|
|
|
onError,
|
|
|
|
lineNumber,
|
|
|
|
"Expected: " + (expected === 0 ? "" : "0 or ") +
|
|
|
|
expected + "; Actual: " + trailingSpaces,
|
2022-06-13 22:53:48 -07:00
|
|
|
undefined,
|
2021-02-06 19:55:22 -08:00
|
|
|
[ column, trailingSpaces ],
|
|
|
|
{
|
|
|
|
"editColumn": column,
|
|
|
|
"deleteCount": trailingSpaces
|
2024-08-12 23:24:32 -07:00
|
|
|
}
|
|
|
|
);
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
2024-08-12 23:24:32 -07:00
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
};
|