Refactor micromark helper getTokenTextByType to be more efficient, remove tokenIfType helper for being redundant.

This commit is contained in:
David Anson 2024-09-16 20:50:54 -07:00
parent b749d9fbb4
commit 2ea3f95fd1
6 changed files with 82 additions and 120 deletions

View file

@ -3,7 +3,7 @@
"use strict";
const { addErrorDetailIf, fencedCodeBlockStyleFor } = require("../helpers");
const { tokenIfType } = require("../helpers/micromark.cjs");
const { getDescendantsByType } = require("../helpers/micromark.cjs");
const { filterByTypesCached } = require("./cache");
// eslint-disable-next-line jsdoc/valid-types
@ -18,22 +18,18 @@ module.exports = {
let expectedStyle = style;
const codeFenceds = filterByTypesCached([ "codeFenced" ]);
for (const codeFenced of codeFenceds) {
const codeFencedFence = tokenIfType(codeFenced.children[0], "codeFencedFence");
if (codeFencedFence) {
const codeFencedFenceSequence = tokenIfType(codeFencedFence.children[0], "codeFencedFenceSequence");
if (codeFencedFenceSequence) {
const { startLine, text } = codeFencedFenceSequence;
if (expectedStyle === "consistent") {
expectedStyle = fencedCodeBlockStyleFor(text);
}
addErrorDetailIf(
onError,
startLine,
expectedStyle,
fencedCodeBlockStyleFor(text)
);
}
const codeFencedFenceSequence =
getDescendantsByType(codeFenced, [ "codeFencedFence", "codeFencedFenceSequence" ])[0];
const { startLine, text } = codeFencedFenceSequence;
if (expectedStyle === "consistent") {
expectedStyle = fencedCodeBlockStyleFor(text);
}
addErrorDetailIf(
onError,
startLine,
expectedStyle,
fencedCodeBlockStyleFor(text)
);
}
}
};