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

@ -13,7 +13,5 @@ module.exports.clear = () => map.clear();
module.exports.flattenedLists =
() => map.get("flattenedLists");
module.exports.lineMetadata =
() => map.get("lineMetadata");
module.exports.referenceLinkImageData =
() => map.get("referenceLinkImageData");

View file

@ -594,15 +594,12 @@ function lintContent(
"lines": Object.freeze(lines),
"frontMatterLines": Object.freeze(frontMatterLines)
};
const lineMetadata =
helpers.getLineMetadata(paramsBase);
const flattenedLists =
helpers.flattenLists(markdownitTokens);
const referenceLinkImageData =
helpers.getReferenceLinkImageData(micromarkTokens);
cache.set({
flattenedLists,
lineMetadata,
referenceLinkImageData
});
// Function to run for each rule

View file

@ -2,8 +2,8 @@
"use strict";
const { addErrorDetailIf, forEachLine } = require("../helpers");
const { lineMetadata } = require("./cache");
const { addErrorDetailIf } = require("../helpers");
const { addRangeToSet, filterByTypes } = require("../helpers/micromark.cjs");
// eslint-disable-next-line jsdoc/valid-types
/** @type import("./markdownlint").Rule */
@ -11,11 +11,17 @@ module.exports = {
"names": [ "MD012", "no-multiple-blanks" ],
"description": "Multiple consecutive blank lines",
"tags": [ "whitespace", "blank_lines" ],
"parser": "none",
"parser": "micromark",
"function": function MD012(params, onError) {
const maximum = Number(params.config.maximum || 1);
const { lines, parsers } = params;
const codeBlockLineNumbers = new Set();
for (const codeBlock of filterByTypes(parsers.micromark.tokens, [ "codeFenced", "codeIndented" ])) {
addRangeToSet(codeBlockLineNumbers, codeBlock.startLine, codeBlock.endLine);
}
let count = 0;
forEachLine(lineMetadata(), (line, lineIndex, inCode) => {
for (const [ lineIndex, line ] of lines.entries()) {
const inCode = codeBlockLineNumbers.has(lineIndex + 1);
count = (inCode || (line.trim().length > 0)) ? 0 : count + 1;
if (maximum < count) {
addErrorDetailIf(
@ -23,13 +29,14 @@ module.exports = {
lineIndex + 1,
maximum,
count,
null,
null,
null,
undefined,
undefined,
undefined,
{
"deleteCount": -1
});
}
);
}
});
}
}
};

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 = {
}
);
}
});
}
}
};

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 = {
}
}
}
});
}
}
};

View file

@ -2,42 +2,61 @@
"use strict";
const { addErrorContext, forEachLine, isBlankLine } = require("../helpers");
const { lineMetadata } = require("./cache");
const { addErrorContext, isBlankLine } = require("../helpers");
const { filterByTypes, getTokenParentOfType } = require("../helpers/micromark.cjs");
const codeFencePrefixRe = /^(.*?)[`~]/;
// eslint-disable-next-line jsdoc/valid-types
/** @typedef {readonly string[]} ReadonlyStringArray */
/**
* Adds an error for the top or bottom of a code fence.
*
* @param {import("./markdownlint").RuleOnError} onError Error-reporting callback.
* @param {ReadonlyStringArray} lines Lines of Markdown content.
* @param {number} lineNumber Line number.
* @param {boolean} top True iff top fence.
* @returns {void}
*/
function addError(onError, lines, lineNumber, top) {
const line = lines[lineNumber - 1];
const [ , prefix ] = line.match(codeFencePrefixRe) || [];
const fixInfo = (prefix === undefined) ? null : {
"lineNumber": lineNumber + (top ? 0 : 1),
"insertText": `${prefix.replace(/[^>]/g, " ").trim()}\n`
};
addErrorContext(
onError,
lineNumber,
line.trim(),
undefined,
undefined,
undefined,
fixInfo
);
}
// eslint-disable-next-line jsdoc/valid-types
/** @type import("./markdownlint").Rule */
module.exports = {
"names": [ "MD031", "blanks-around-fences" ],
"description": "Fenced code blocks should be surrounded by blank lines",
"tags": [ "code", "blank_lines" ],
"parser": "none",
"parser": "micromark",
"function": function MD031(params, onError) {
const listItems = params.config.list_items;
const includeListItems = (listItems === undefined) ? true : !!listItems;
const { lines } = params;
forEachLine(lineMetadata(), (line, i, inCode, onFence, inTable, inItem) => {
const onTopFence = (onFence > 0);
const onBottomFence = (onFence < 0);
if ((includeListItems || !inItem) &&
((onTopFence && !isBlankLine(lines[i - 1])) ||
(onBottomFence && !isBlankLine(lines[i + 1])))) {
const [ , prefix ] = line.match(codeFencePrefixRe) || [];
const fixInfo = (prefix === undefined) ? null : {
"lineNumber": i + (onTopFence ? 1 : 2),
"insertText": `${prefix.replace(/[^>]/g, " ").trim()}\n`
};
addErrorContext(
onError,
i + 1,
lines[i].trim(),
null,
null,
null,
fixInfo);
const { lines, parsers } = params;
for (const codeBlock of filterByTypes(parsers.micromark.tokens, [ "codeFenced" ])) {
if (includeListItems || !(getTokenParentOfType(codeBlock, [ "listOrdered", "listUnordered" ]))) {
if (!isBlankLine(lines[codeBlock.startLine - 2])) {
addError(onError, lines, codeBlock.startLine, true);
}
if (!isBlankLine(lines[codeBlock.endLine])) {
addError(onError, lines, codeBlock.endLine, false);
}
}
});
}
}
};