Refactor to remove forEachLine and getLineMetadata helpers, reimplement MD012/MD018/MD020/MD031 using micromark tokens.

This commit is contained in:
David Anson 2024-08-17 17:58:16 -07:00
parent 7efc2605b1
commit c8fd9eb4b3
13 changed files with 198 additions and 308 deletions

View file

@ -2,8 +2,8 @@
"use strict";
const { addErrorContext, forEachLine } = require("../helpers");
const { lineMetadata } = require("./cache");
const { addErrorContext } = require("../helpers");
const { addRangeToSet, filterByTypes } = require("../helpers/micromark.cjs");
// eslint-disable-next-line jsdoc/valid-types
/** @type import("./markdownlint").Rule */
@ -11,11 +11,15 @@ module.exports = {
"names": [ "MD020", "no-missing-space-closed-atx" ],
"description": "No space inside hashes on closed atx style heading",
"tags": [ "headings", "atx_closed", "spaces" ],
"parser": "none",
"parser": "micromark",
"function": function MD020(params, onError) {
forEachLine(lineMetadata(), (line, lineIndex, inCode, inFence, inTable, inItem, inBreak, inHtml) => {
if (!inCode &&
!inHtml) {
const { lines, parsers } = params;
const ignoreBlockLineNumbers = new Set();
for (const ignoreBlock of filterByTypes(parsers.micromark.tokens, [ "codeIndented", "codeFenced", "htmlFlow" ])) {
addRangeToSet(ignoreBlockLineNumbers, ignoreBlock.startLine, ignoreBlock.endLine);
}
for (const [ lineIndex, line ] of lines.entries()) {
if (!ignoreBlockLineNumbers.has(lineIndex + 1)) {
const match =
/^(#+)([ \t]*)([^#]*?[^#\\])([ \t]*)((?:\\#)?)(#+)(\s*)$/.exec(line);
if (match) {
@ -61,6 +65,6 @@ module.exports = {
}
}
}
});
}
}
};