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

View file

@ -3,36 +3,36 @@
"use strict";
const { addError } = require("../helpers");
const { getSiblingTokens } = require("../helpers/micromark.cjs");
const { filterByTypes } = require("../helpers/micromark.cjs");
const ignoreTypes = new Set([ "lineEnding", "listItemIndent", "linePrefix" ]);
module.exports = {
"names": [ "MD028", "no-blanks-blockquote" ],
"description": "Blank line inside blockquote",
"tags": [ "blockquote", "whitespace" ],
"function": function MD028(params, onError) {
for (const siblings of getSiblingTokens(params.parsers.micromark.tokens)) {
let errorLineNumbers = null;
for (const sibling of siblings) {
switch (sibling.type) {
case "blockQuote":
for (const lineNumber of (errorLineNumbers || [])) {
addError(onError, lineNumber);
}
errorLineNumbers = [];
break;
case "lineEnding":
case "linePrefix":
case "listItemIndent":
// Ignore
break;
case "lineEndingBlank":
if (errorLineNumbers) {
errorLineNumbers.push(sibling.startLine);
}
break;
default:
errorLineNumbers = null;
break;
const { tokens } = params.parsers.micromark;
for (const token of filterByTypes(tokens, [ "blockQuote" ])) {
const errorLineNumbers = [];
const siblings = token.parent?.children || tokens;
for (let i = siblings.indexOf(token) + 1; i < siblings.length; i++) {
const sibling = siblings[i];
const { startLine, type } = sibling;
if (type === "lineEndingBlank") {
// Possible blank between blockquotes
errorLineNumbers.push(startLine);
} else if (ignoreTypes.has(type)) {
// Ignore invisible formatting
} else if (type === "blockQuote") {
// Blockquote followed by blockquote
for (const lineNumber of errorLineNumbers) {
addError(onError, lineNumber);
}
break;
} else {
// Blockquote not followed by blockquote
break;
}
}
}