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

@ -1473,28 +1473,6 @@ function filterByTypes(tokens, types) {
return filterByPredicate(tokens, predicate); return filterByPredicate(tokens, predicate);
} }
/**
* Gets all sibling token groups for a list of Micromark tokens.
*
* @param {Token[]} tokens Micromark tokens.
* @returns {Token[][]} Sibling token groups.
*/
function getSiblingTokens(tokens) {
const result = [];
const queue = [ tokens ];
// eslint-disable-next-line init-declarations
let current;
while ((current = queue.shift())) {
result.push(current);
for (const token of current) {
if (token.children.length > 0) {
queue.push(token.children);
}
}
}
return result;
}
/** /**
* Gets the heading level of a Micromark heading tokan. * Gets the heading level of a Micromark heading tokan.
* *
@ -1619,7 +1597,6 @@ module.exports = {
getHeadingLevel, getHeadingLevel,
getHtmlTagInfo, getHtmlTagInfo,
getMicromarkEvents, getMicromarkEvents,
getSiblingTokens,
getTokenParentOfType, getTokenParentOfType,
getTokenTextByType, getTokenTextByType,
inHtmlFlow, inHtmlFlow,
@ -4544,18 +4521,17 @@ module.exports = {
const { addErrorContext } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"); const { addErrorContext } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { getSiblingTokens } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs"); const { filterByTypes } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
module.exports = { module.exports = {
"names": ["MD027", "no-multiple-space-blockquote"], "names": ["MD027", "no-multiple-space-blockquote"],
"description": "Multiple spaces after blockquote symbol", "description": "Multiple spaces after blockquote symbol",
"tags": ["blockquote", "whitespace", "indentation"], "tags": ["blockquote", "whitespace", "indentation"],
"function": function MD027(params, onError) { "function": function MD027(params, onError) {
for (const siblings of getSiblingTokens(params.parsers.micromark.tokens)) { const { tokens } = params.parsers.micromark;
let previousType = null; for (const token of filterByTypes(tokens, [ "linePrefix" ])) {
for (const token of siblings) { const siblings = token.parent?.children || tokens;
const { type } = token; if (siblings[siblings.indexOf(token) - 1]?.type === "blockQuotePrefix") {
if ((type === "linePrefix") && (previousType === "blockQuotePrefix")) {
const { startColumn, startLine, text } = token; const { startColumn, startLine, text } = token;
const { length } = text; const { length } = text;
const line = params.lines[startLine - 1]; const line = params.lines[startLine - 1];
@ -4572,8 +4548,6 @@ module.exports = {
} }
); );
} }
previousType = type;
}
} }
} }
}; };
@ -4593,35 +4567,35 @@ module.exports = {
const { addError } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"); const { addError } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { getSiblingTokens } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs"); const { filterByTypes } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
const ignoreTypes = new Set([ "lineEnding", "listItemIndent", "linePrefix" ]);
module.exports = { module.exports = {
"names": [ "MD028", "no-blanks-blockquote" ], "names": [ "MD028", "no-blanks-blockquote" ],
"description": "Blank line inside blockquote", "description": "Blank line inside blockquote",
"tags": [ "blockquote", "whitespace" ], "tags": [ "blockquote", "whitespace" ],
"function": function MD028(params, onError) { "function": function MD028(params, onError) {
for (const siblings of getSiblingTokens(params.parsers.micromark.tokens)) { const { tokens } = params.parsers.micromark;
let errorLineNumbers = null; for (const token of filterByTypes(tokens, [ "blockQuote" ])) {
for (const sibling of siblings) { const errorLineNumbers = [];
switch (sibling.type) { const siblings = token.parent?.children || tokens;
case "blockQuote": for (let i = siblings.indexOf(token) + 1; i < siblings.length; i++) {
for (const lineNumber of (errorLineNumbers || [])) { 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); addError(onError, lineNumber);
} }
errorLineNumbers = [];
break; break;
case "lineEnding": } else {
case "linePrefix": // Blockquote not followed by blockquote
case "listItemIndent":
// Ignore
break;
case "lineEndingBlank":
if (errorLineNumbers) {
errorLineNumbers.push(sibling.startLine);
}
break;
default:
errorLineNumbers = null;
break; break;
} }
} }

View file

@ -295,28 +295,6 @@ function filterByTypes(tokens, types) {
return filterByPredicate(tokens, predicate); return filterByPredicate(tokens, predicate);
} }
/**
* Gets all sibling token groups for a list of Micromark tokens.
*
* @param {Token[]} tokens Micromark tokens.
* @returns {Token[][]} Sibling token groups.
*/
function getSiblingTokens(tokens) {
const result = [];
const queue = [ tokens ];
// eslint-disable-next-line init-declarations
let current;
while ((current = queue.shift())) {
result.push(current);
for (const token of current) {
if (token.children.length > 0) {
queue.push(token.children);
}
}
}
return result;
}
/** /**
* Gets the heading level of a Micromark heading tokan. * Gets the heading level of a Micromark heading tokan.
* *
@ -441,7 +419,6 @@ module.exports = {
getHeadingLevel, getHeadingLevel,
getHtmlTagInfo, getHtmlTagInfo,
getMicromarkEvents, getMicromarkEvents,
getSiblingTokens,
getTokenParentOfType, getTokenParentOfType,
getTokenTextByType, getTokenTextByType,
inHtmlFlow, inHtmlFlow,

View file

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

View file

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