Update MD044/proper-names to return early for empty names array and use htmlFlowChildren to avoid re-parsing htmlFlow content.

This commit is contained in:
David Anson 2023-08-27 23:35:00 -07:00
parent d1f7f147dc
commit 3099a98b51
2 changed files with 44 additions and 95 deletions

View file

@ -2,7 +2,7 @@
"use strict";
const { addErrorDetailIf, escapeForRegExp, newLineRe, withinAnyRange } =
const { addErrorDetailIf, escapeForRegExp, withinAnyRange } =
require("../helpers");
const { filterByPredicate, filterByTypes, parse } =
require("../helpers/micromark.cjs");
@ -19,6 +19,10 @@ module.exports = {
let names = params.config.names;
names = Array.isArray(names) ? names : [];
names.sort((a, b) => (b.length - a.length) || a.localeCompare(b));
if (names.length === 0) {
// Nothing to check; avoid doing any work
return;
}
const codeBlocks = params.config.code_blocks;
const includeCodeBlocks =
(codeBlocks === undefined) ? true : !!codeBlocks;
@ -30,32 +34,19 @@ module.exports = {
scannedTypes.add("codeFlowValue");
scannedTypes.add("codeTextData");
}
const tokenAdjustments = new Map();
const contentTokens =
filterByPredicate(
params.parsers.micromark.tokens,
(token) => scannedTypes.has(token.type),
(token) => {
let { children } = token;
const { startLine, text } = token;
if (!includeHtmlElements && (token.type === "htmlFlow")) {
if (text.startsWith("<!--")) {
const { htmlFlowChildren, text, type } = token;
if (!includeHtmlElements && (type === "htmlFlow")) {
children = text.startsWith("<!--") ?
// Remove comment content
children = [];
} else {
// Re-parse to get htmlText elements for detailed tokenization
const htmlTextLines =
`<md044>\n${text}\n</md044>`.split(newLineRe);
children = parse(htmlTextLines.join(""));
const reTokens = [ ...children ];
for (const reToken of reTokens) {
tokenAdjustments.set(reToken, {
htmlTextLines,
startLine
});
reTokens.push(...reToken.children);
}
}
[] :
// Examine htmlText content
htmlFlowChildren;
}
return children.filter((t) => !ignoredChildTypes.has(t.type));
}
@ -96,22 +87,10 @@ module.exports = {
autoLinked.add(token);
}
if (!withinAnyRange(urlRanges, lineIndex, index, length)) {
let lineNumber = token.startLine;
let column = index;
if (tokenAdjustments.has(token)) {
const { htmlTextLines, startLine } =
tokenAdjustments.get(token);
let lineDelta = 0;
while (htmlTextLines[lineDelta].length <= column) {
column -= htmlTextLines[lineDelta].length;
lineDelta++;
}
lineNumber = startLine + lineDelta - 1;
}
column++;
const column = index + 1;
addErrorDetailIf(
onError,
lineNumber,
token.startLine,
name,
nameMatch,
null,