mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
Reimplement MD009/no-trailing-spaces using micromark tokens.
This commit is contained in:
parent
37ab4a0faf
commit
4072cf7417
7 changed files with 380 additions and 116 deletions
|
@ -181,17 +181,6 @@ function isBlankLine(line) {
|
|||
}
|
||||
module.exports.isBlankLine = isBlankLine;
|
||||
|
||||
/**
|
||||
* Compare function for Array.prototype.sort for ascending order of numbers.
|
||||
*
|
||||
* @param {number} a First number.
|
||||
* @param {number} b Second number.
|
||||
* @returns {number} Positive value if a>b, negative value if b<a, 0 otherwise.
|
||||
*/
|
||||
module.exports.numericSortAscending = function numericSortAscending(a, b) {
|
||||
return a - b;
|
||||
};
|
||||
|
||||
// Returns true iff the sorted array contains the specified element
|
||||
module.exports.includesSorted = function includesSorted(array, element) {
|
||||
let left = 0;
|
||||
|
@ -3747,9 +3736,22 @@ module.exports = {
|
|||
|
||||
|
||||
|
||||
const { addError, filterTokens, forEachLine, includesSorted,
|
||||
numericSortAscending } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
|
||||
const { lineMetadata } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
|
||||
const { addError } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
|
||||
const { filterByTypes } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
|
||||
|
||||
/**
|
||||
* Adds a range of numbers to a set.
|
||||
*
|
||||
* @param {Set<number>} set Set of numbers.
|
||||
* @param {number} start Starting number.
|
||||
* @param {number} end Ending number.
|
||||
* @returns {void}
|
||||
*/
|
||||
function addRangeToSet(set, start, end) {
|
||||
for (let i = start; i <= end; i++) {
|
||||
set.add(i);
|
||||
}
|
||||
}
|
||||
|
||||
// eslint-disable-next-line jsdoc/valid-types
|
||||
/** @type import("./markdownlint").Rule */
|
||||
|
@ -3757,63 +3759,69 @@ module.exports = {
|
|||
"names": [ "MD009", "no-trailing-spaces" ],
|
||||
"description": "Trailing spaces",
|
||||
"tags": [ "whitespace" ],
|
||||
"parser": "markdownit",
|
||||
"parser": "micromark",
|
||||
"function": function MD009(params, onError) {
|
||||
let brSpaces = params.config.br_spaces;
|
||||
brSpaces = Number((brSpaces === undefined) ? 2 : brSpaces);
|
||||
const listItemEmptyLines = !!params.config.list_item_empty_lines;
|
||||
const strict = !!params.config.strict;
|
||||
const listItemLineNumbers = [];
|
||||
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(numericSortAscending);
|
||||
const { tokens } = params.parsers.micromark;
|
||||
const codeBlockLineNumbers = new Set();
|
||||
for (const codeBlock of filterByTypes(tokens, [ "codeFenced" ])) {
|
||||
addRangeToSet(codeBlockLineNumbers, codeBlock.startLine + 1, codeBlock.endLine - 1);
|
||||
}
|
||||
const paragraphLineNumbers = [];
|
||||
const codeInlineLineNumbers = [];
|
||||
for (const codeBlock of filterByTypes(tokens, [ "codeIndented" ])) {
|
||||
addRangeToSet(codeBlockLineNumbers, codeBlock.startLine, codeBlock.endLine);
|
||||
}
|
||||
const listItemLineNumbers = new Set();
|
||||
if (listItemEmptyLines) {
|
||||
for (const listBlock of filterByTypes(tokens, [ "listOrdered", "listUnordered" ])) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
const paragraphLineNumbers = new Set();
|
||||
const codeInlineLineNumbers = new Set();
|
||||
if (strict) {
|
||||
filterTokens(params, "paragraph_open", (token) => {
|
||||
for (let i = token.map[0]; i < token.map[1] - 1; i++) {
|
||||
paragraphLineNumbers.push(i + 1);
|
||||
}
|
||||
});
|
||||
const addLineNumberRange = (start, end) => {
|
||||
for (let i = start; i < end; i++) {
|
||||
codeInlineLineNumbers.push(i);
|
||||
}
|
||||
};
|
||||
filterTokens(params, "inline", (token) => {
|
||||
let start = 0;
|
||||
for (const child of token.children) {
|
||||
if (start > 0) {
|
||||
addLineNumberRange(start, child.lineNumber);
|
||||
start = 0;
|
||||
}
|
||||
if (child.type === "code_inline") {
|
||||
start = child.lineNumber;
|
||||
}
|
||||
}
|
||||
if (start > 0) {
|
||||
addLineNumberRange(start, token.map[1]);
|
||||
}
|
||||
});
|
||||
for (const paragraph of filterByTypes(tokens, [ "paragraph" ])) {
|
||||
addRangeToSet(paragraphLineNumbers, paragraph.startLine, paragraph.endLine - 1);
|
||||
}
|
||||
for (const codeText of filterByTypes(tokens, [ "codeText" ])) {
|
||||
addRangeToSet(codeInlineLineNumbers, codeText.startLine, codeText.endLine - 1);
|
||||
}
|
||||
}
|
||||
const expected = (brSpaces < 2) ? 0 : brSpaces;
|
||||
forEachLine(lineMetadata(), (line, lineIndex, inCode) => {
|
||||
for (let lineIndex = 0; lineIndex < params.lines.length; lineIndex++) {
|
||||
const line = params.lines[lineIndex];
|
||||
const lineNumber = lineIndex + 1;
|
||||
const trailingSpaces = line.length - line.trimEnd().length;
|
||||
if (
|
||||
trailingSpaces &&
|
||||
!inCode &&
|
||||
!includesSorted(listItemLineNumbers, lineNumber) &&
|
||||
!codeBlockLineNumbers.has(lineNumber) &&
|
||||
!listItemLineNumbers.has(lineNumber) &&
|
||||
(
|
||||
(expected !== trailingSpaces) ||
|
||||
(strict &&
|
||||
(!includesSorted(paragraphLineNumbers, lineNumber) ||
|
||||
includesSorted(codeInlineLineNumbers, lineNumber)))
|
||||
(!paragraphLineNumbers.has(lineNumber) ||
|
||||
codeInlineLineNumbers.has(lineNumber)))
|
||||
)
|
||||
) {
|
||||
const column = line.length - trailingSpaces + 1;
|
||||
|
@ -3827,9 +3835,10 @@ module.exports = {
|
|||
{
|
||||
"editColumn": column,
|
||||
"deleteCount": trailingSpaces
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue