Refactor to remove micromark helper getSiblingTokens for simpler code and better performance.

This commit is contained in:
David Anson 2024-02-26 21:07:24 -08:00
parent 4436d84b55
commit 996d88a9b4
4 changed files with 88 additions and 140 deletions

View file

@ -3,35 +3,32 @@
"use strict";
const { addErrorContext } = require("../helpers");
const { getSiblingTokens } = require("../helpers/micromark.cjs");
const { filterByTypes } = require("../helpers/micromark.cjs");
module.exports = {
"names": ["MD027", "no-multiple-space-blockquote"],
"description": "Multiple spaces after blockquote symbol",
"tags": ["blockquote", "whitespace", "indentation"],
"function": function MD027(params, onError) {
for (const siblings of getSiblingTokens(params.parsers.micromark.tokens)) {
let previousType = null;
for (const token of siblings) {
const { type } = token;
if ((type === "linePrefix") && (previousType === "blockQuotePrefix")) {
const { startColumn, startLine, text } = token;
const { length } = text;
const line = params.lines[startLine - 1];
addErrorContext(
onError,
startLine,
line,
null,
null,
[ startColumn, length ],
{
"editColumn": startColumn,
"deleteCount": length
}
);
}
previousType = type;
const { tokens } = params.parsers.micromark;
for (const token of filterByTypes(tokens, [ "linePrefix" ])) {
const siblings = token.parent?.children || tokens;
if (siblings[siblings.indexOf(token) - 1]?.type === "blockQuotePrefix") {
const { startColumn, startLine, text } = token;
const { length } = text;
const line = params.lines[startLine - 1];
addErrorContext(
onError,
startLine,
line,
null,
null,
[ startColumn, length ],
{
"editColumn": startColumn,
"deleteCount": length
}
);
}
}
}