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

View file

@ -82,6 +82,23 @@ export function applyFixes(input: string, errors: RuleOnErrorInfo[]): string;
* @returns {string} SemVer string.
*/
export function getVersion(): string;
/**
* Result object for getEnabledRulesPerLineNumber.
*/
export type EnabledRulesPerLineNumberResult = {
/**
* Effective configuration.
*/
effectiveConfig: Configuration;
/**
* Enabled rules per line number.
*/
enabledRulesPerLineNumber: any[];
/**
* Enabled rule list.
*/
enabledRuleList: Rule[];
};
/**
* Function to implement rule logic.
*/

View file

@ -269,6 +269,15 @@ function getEffectiveConfig(ruleList, config, aliasToRuleNames) {
return effectiveConfig;
}
/**
* Result object for getEnabledRulesPerLineNumber.
*
* @typedef {Object} EnabledRulesPerLineNumberResult
* @property {Configuration} effectiveConfig Effective configuration.
* @property {any[]} enabledRulesPerLineNumber Enabled rules per line number.
* @property {Rule[]} enabledRuleList Enabled rule list.
*/
/**
* Create a mapping of enabled rules per line.
*
@ -280,7 +289,7 @@ function getEffectiveConfig(ruleList, config, aliasToRuleNames) {
* @param {ConfigurationParser[] | undefined} configParsers Configuration parsers.
* @param {Object.<string, string[]>} aliasToRuleNames Map of alias to rule
* names.
* @returns {Object} Effective configuration and enabled rules per line number.
* @returns {EnabledRulesPerLineNumberResult} Effective configuration and enabled rules per line number.
*/
function getEnabledRulesPerLineNumber(
ruleList,