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,22 +11,28 @@ module.exports = {
"names": [ "MD018", "no-missing-space-atx" ],
"description": "No space after hash on atx style heading",
"tags": [ "headings", "atx", "spaces" ],
"parser": "none",
"parser": "micromark",
"function": function MD018(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) &&
/^#+[^# \t]/.test(line) &&
!/#\s*$/.test(line) &&
!line.startsWith("#️⃣")) {
!line.startsWith("#️⃣")
) {
// @ts-ignore
const hashCount = /^#+/.exec(line)[0].length;
addErrorContext(
onError,
lineIndex + 1,
line.trim(),
null,
null,
undefined,
undefined,
[ 1, hashCount + 1 ],
{
"editColumn": hashCount + 1,
@ -34,6 +40,6 @@ module.exports = {
}
);
}
});
}
}
};