Add tests for previous commit to avoid invoking Micromark parser when not needed, improve type definitions slightly (closes #1564).

This commit is contained in:
David Anson 2025-04-20 04:57:35 +00:00
parent 759c31760e
commit 92cd6d51cb
4 changed files with 112 additions and 6 deletions

View file

@ -3,14 +3,19 @@
import { getReferenceLinkImageData as helpersGetReferenceLinkImageData } from "../helpers/helpers.cjs";
import { filterByTypes } from "../helpers/micromark-helpers.cjs";
/** @typedef {import("markdownlint").RuleParams} RuleParams */
/** @typedef {import("markdownlint").MicromarkToken} MicromarkToken */
/** @typedef {import("markdownlint").MicromarkTokenType} MicromarkTokenType */
/** @type {Map<string, object>} */
const map = new Map();
/** @type {RuleParams | undefined} */
let params = undefined;
/**
* Initializes (resets) the cache.
*
* @param {import("markdownlint").RuleParams} [p] Rule parameters object.
* @param {RuleParams} [p] Rule parameters object.
* @returns {void}
*/
export function initialize(p) {
@ -18,6 +23,15 @@ export function initialize(p) {
params = p;
}
/**
* Gets the cached Micromark token array (for testing).
*
* @returns {MicromarkToken[]} Micromark tokens.
*/
export function micromarkTokens() {
return params?.parsers.micromark.tokens || [];
}
/**
* Gets a cached object value - computes it and caches it.
*
@ -37,15 +51,15 @@ function getCached(name, getValue) {
/**
* Filters a list of Micromark tokens by type and caches the result.
*
* @param {import("markdownlint").MicromarkTokenType[]} types Types to allow.
* @param {MicromarkTokenType[]} types Types to allow.
* @param {boolean} [htmlFlow] Whether to include htmlFlow content.
* @returns {import("markdownlint").MicromarkToken[]} Filtered tokens.
* @returns {MicromarkToken[]} Filtered tokens.
*/
export function filterByTypesCached(types, htmlFlow) {
return getCached(
// eslint-disable-next-line prefer-rest-params
JSON.stringify(arguments),
() => filterByTypes(params.parsers.micromark.tokens, types, htmlFlow)
() => filterByTypes(micromarkTokens(), types, htmlFlow)
);
}
@ -57,6 +71,6 @@ export function filterByTypesCached(types, htmlFlow) {
export function getReferenceLinkImageData() {
return getCached(
getReferenceLinkImageData.name,
() => helpersGetReferenceLinkImageData(params.parsers.micromark.tokens)
() => helpersGetReferenceLinkImageData(micromarkTokens())
);
}