mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
Add tests for previous commit to avoid invoking Micromark parser when not needed, improve type definitions slightly (closes #1564).
This commit is contained in:
parent
759c31760e
commit
92cd6d51cb
4 changed files with 112 additions and 6 deletions
|
@ -3,14 +3,19 @@
|
||||||
import { getReferenceLinkImageData as helpersGetReferenceLinkImageData } from "../helpers/helpers.cjs";
|
import { getReferenceLinkImageData as helpersGetReferenceLinkImageData } from "../helpers/helpers.cjs";
|
||||||
import { filterByTypes } from "../helpers/micromark-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>} */
|
/** @type {Map<string, object>} */
|
||||||
const map = new Map();
|
const map = new Map();
|
||||||
|
/** @type {RuleParams | undefined} */
|
||||||
let params = undefined;
|
let params = undefined;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes (resets) the cache.
|
* Initializes (resets) the cache.
|
||||||
*
|
*
|
||||||
* @param {import("markdownlint").RuleParams} [p] Rule parameters object.
|
* @param {RuleParams} [p] Rule parameters object.
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
export function initialize(p) {
|
export function initialize(p) {
|
||||||
|
@ -18,6 +23,15 @@ export function initialize(p) {
|
||||||
params = 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.
|
* 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.
|
* 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.
|
* @param {boolean} [htmlFlow] Whether to include htmlFlow content.
|
||||||
* @returns {import("markdownlint").MicromarkToken[]} Filtered tokens.
|
* @returns {MicromarkToken[]} Filtered tokens.
|
||||||
*/
|
*/
|
||||||
export function filterByTypesCached(types, htmlFlow) {
|
export function filterByTypesCached(types, htmlFlow) {
|
||||||
return getCached(
|
return getCached(
|
||||||
// eslint-disable-next-line prefer-rest-params
|
// eslint-disable-next-line prefer-rest-params
|
||||||
JSON.stringify(arguments),
|
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() {
|
export function getReferenceLinkImageData() {
|
||||||
return getCached(
|
return getCached(
|
||||||
getReferenceLinkImageData.name,
|
getReferenceLinkImageData.name,
|
||||||
() => helpersGetReferenceLinkImageData(params.parsers.micromark.tokens)
|
() => helpersGetReferenceLinkImageData(micromarkTokens())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -82,6 +82,23 @@ export function applyFixes(input: string, errors: RuleOnErrorInfo[]): string;
|
||||||
* @returns {string} SemVer string.
|
* @returns {string} SemVer string.
|
||||||
*/
|
*/
|
||||||
export function getVersion(): 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.
|
* Function to implement rule logic.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -269,6 +269,15 @@ function getEffectiveConfig(ruleList, config, aliasToRuleNames) {
|
||||||
return effectiveConfig;
|
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.
|
* Create a mapping of enabled rules per line.
|
||||||
*
|
*
|
||||||
|
@ -280,7 +289,7 @@ function getEffectiveConfig(ruleList, config, aliasToRuleNames) {
|
||||||
* @param {ConfigurationParser[] | undefined} configParsers Configuration parsers.
|
* @param {ConfigurationParser[] | undefined} configParsers Configuration parsers.
|
||||||
* @param {Object.<string, string[]>} aliasToRuleNames Map of alias to rule
|
* @param {Object.<string, string[]>} aliasToRuleNames Map of alias to rule
|
||||||
* names.
|
* names.
|
||||||
* @returns {Object} Effective configuration and enabled rules per line number.
|
* @returns {EnabledRulesPerLineNumberResult} Effective configuration and enabled rules per line number.
|
||||||
*/
|
*/
|
||||||
function getEnabledRulesPerLineNumber(
|
function getEnabledRulesPerLineNumber(
|
||||||
ruleList,
|
ruleList,
|
||||||
|
|
|
@ -17,6 +17,7 @@ import { getVersion } from "markdownlint";
|
||||||
import { lint as lintAsync } from "markdownlint/async";
|
import { lint as lintAsync } from "markdownlint/async";
|
||||||
import { lint as lintPromise } from "markdownlint/promise";
|
import { lint as lintPromise } from "markdownlint/promise";
|
||||||
import { lint as lintSync } from "markdownlint/sync";
|
import { lint as lintSync } from "markdownlint/sync";
|
||||||
|
import * as cache from "../lib/cache.mjs";
|
||||||
import * as constants from "../lib/constants.mjs";
|
import * as constants from "../lib/constants.mjs";
|
||||||
import rules from "../lib/rules.mjs";
|
import rules from "../lib/rules.mjs";
|
||||||
import customRules from "./rules/rules.cjs";
|
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) => {
|
test("markdownItPluginsSingle", (t) => new Promise((resolve) => {
|
||||||
t.plan(4);
|
t.plan(4);
|
||||||
lintAsync({
|
lintAsync({
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue