Update MD005, MD007, MD022, MD037, MD038 to not report violations within "htmlFlow" context (fixes #999).

This commit is contained in:
David Anson 2023-10-21 22:03:11 -07:00
parent 2a56f130c1
commit 63325edc97
15 changed files with 511 additions and 149 deletions

View file

@ -281,11 +281,11 @@ function filterByPredicate(tokens, allowed, transformChildren) {
* Filter a list of Micromark tokens by type.
*
* @param {Token[]} tokens Micromark tokens.
* @param {string[]} allowed Types to allow.
* @param {string[]} types Types to allow.
* @returns {Token[]} Filtered tokens.
*/
function filterByTypes(tokens, allowed) {
const predicate = (token) => allowed.includes(token.type);
function filterByTypes(tokens, types) {
const predicate = (token) => types.includes(token.type);
const flatTokens = tokens[flatTokensSymbol];
if (flatTokens) {
return flatTokens.filter(predicate);
@ -336,6 +336,22 @@ function getHtmlTagInfo(token) {
return null;
}
/**
* Gets the nearest parent of the specified type for a Micromark token.
*
* @param {Token} token Micromark token.
* @param {string[]} types Types to allow.
* @returns {Token | null} Parent token.
*/
function getTokenParentOfType(token, types) {
/** @type {Token | null} */
let current = token;
while ((current = current.parent) && !types.includes(current.type)) {
// Empty
}
return current;
}
/**
* Get the text of a single token from a list of Micromark tokens by type.
*
@ -348,6 +364,16 @@ function getTokenTextByType(tokens, type) {
return (filtered.length === 1) ? filtered[0].text : null;
}
/**
* Determines if a Micromark token has an htmlFlow-type parent.
*
* @param {Token} token Micromark token.
* @returns {boolean} True iff the token has an htmlFlow-type parent.
*/
function inHtmlFlow(token) {
return getTokenParentOfType(token, [ "htmlFlow" ]) !== null;
}
/**
* Determines a list of Micromark tokens matches and returns a subset.
*
@ -391,7 +417,9 @@ module.exports = {
getHeadingLevel,
getHtmlTagInfo,
getMicromarkEvents,
getTokenParentOfType,
getTokenTextByType,
inHtmlFlow,
matchAndGetTokensByType,
tokenIfType
};