diff --git a/lib/cache.mjs b/lib/cache.mjs index 9d19b0a4..c05b9927 100644 --- a/lib/cache.mjs +++ b/lib/cache.mjs @@ -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} */ 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()) ); } diff --git a/lib/markdownlint.d.mts b/lib/markdownlint.d.mts index fa56b00c..d720a3f9 100644 --- a/lib/markdownlint.d.mts +++ b/lib/markdownlint.d.mts @@ -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. */ diff --git a/lib/markdownlint.mjs b/lib/markdownlint.mjs index ffa1b0da..77cc2221 100644 --- a/lib/markdownlint.mjs +++ b/lib/markdownlint.mjs @@ -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.} 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, diff --git a/test/markdownlint-test.mjs b/test/markdownlint-test.mjs index 7c3a9048..b78671b3 100644 --- a/test/markdownlint-test.mjs +++ b/test/markdownlint-test.mjs @@ -17,6 +17,7 @@ import { getVersion } from "markdownlint"; import { lint as lintAsync } from "markdownlint/async"; import { lint as lintPromise } from "markdownlint/promise"; import { lint as lintSync } from "markdownlint/sync"; +import * as cache from "../lib/cache.mjs"; import * as constants from "../lib/constants.mjs"; import rules from "../lib/rules.mjs"; import customRules from "./rules/rules.cjs"; @@ -1063,6 +1064,71 @@ test("someCustomRulesHaveValidUrl", (t) => { } }); +test("coverageForCacheMicromarkTokensWhenUndefined", (t) => { + t.plan(1); + cache.initialize(undefined); + t.is(cache.micromarkTokens().length, 0); +}); + +test("micromarkParseCalledWhenNeeded", (t) => new Promise((resolve) => { + t.plan(3); + /** @type {import("markdownlint").Rule} */ + const markdownItRule = { + "names": [ "markdown-it-rule" ], + "description": "markdown-it rule", + "tags": [ "test" ], + "parser": "markdownit", + "function": () => { + t.true(cache.micromarkTokens().length > 0); + } + }; + lintAsync({ + "strings": { + "string": "# Heading\n\nText\n" + }, + "config": { + "markdown-it-rule": true + }, + "customRules": [ markdownItRule ], + "markdownItFactory": getMarkdownItFactory([]) + }, function callback(err, actual) { + t.falsy(err); + const expected = { "string": [] }; + t.deepEqual(actual, expected, "Unexpected issues."); + resolve(); + }); +})); + +test("micromarkParseSkippedWhenNotNeeded", (t) => new Promise((resolve) => { + t.plan(3); + /** @type {import("markdownlint").Rule} */ + const markdownItRule = { + "names": [ "markdown-it-rule" ], + "description": "markdown-it rule", + "tags": [ "test" ], + "parser": "markdownit", + "function": () => { + t.true(cache.micromarkTokens().length === 0); + } + }; + lintAsync({ + "strings": { + "string": "# Heading\n\nText\n" + }, + "config": { + "default": false, + "markdown-it-rule": true + }, + "customRules": [ markdownItRule ], + "markdownItFactory": getMarkdownItFactory([]) + }, function callback(err, actual) { + t.falsy(err); + const expected = { "string": [] }; + t.deepEqual(actual, expected, "Unexpected issues."); + resolve(); + }); +})); + test("markdownItPluginsSingle", (t) => new Promise((resolve) => { t.plan(4); lintAsync({