Refactor to remove micromark helper getExclusionsForToken.

This commit is contained in:
David Anson 2024-09-27 23:58:40 -07:00
parent cfcb5fbd57
commit 5e568d0da9
6 changed files with 231 additions and 124 deletions

View file

@ -2,8 +2,8 @@
"use strict";
const { addError, withinAnyRange } = require("../helpers");
const { getDescendantsByType, getExclusionsForToken } = require("../helpers/micromark.cjs");
const { addError, hasOverlap } = require("../helpers");
const { getDescendantsByType } = require("../helpers/micromark.cjs");
const { filterByTypesCached } = require("./cache");
const tabRe = /\t+/g;
@ -26,7 +26,6 @@ module.exports = {
const spaceMultiplier = (spacesPerTab === undefined) ?
1 :
Math.max(0, Number(spacesPerTab));
const exclusions = [];
// eslint-disable-next-line jsdoc/valid-types
/** @type import("../helpers/micromark.cjs").TokenType[] */
const exclusionTypes = [];
@ -44,24 +43,29 @@ module.exports = {
}
return true;
});
for (const codeToken of codeTokens) {
const exclusionsForToken = getExclusionsForToken(params.lines, codeToken);
if (codeToken.type === "codeFenced") {
exclusionsForToken.pop();
exclusionsForToken.shift();
}
exclusions.push(...exclusionsForToken);
}
const codeRanges = codeTokens.map((token) => {
const { type, startLine, startColumn, endLine, endColumn } = token;
const codeFenced = (type === "codeFenced");
return {
"startLine": startLine + (codeFenced ? 1 : 0),
"startColumn": codeFenced ? 0 : startColumn,
"endLine": endLine - (codeFenced ? 1 : 0),
"endColumn": codeFenced ? Number.MAX_SAFE_INTEGER : endColumn
};
});
for (let lineIndex = 0; lineIndex < params.lines.length; lineIndex++) {
const line = params.lines[lineIndex];
let match = null;
while ((match = tabRe.exec(line)) !== null) {
const lineNumber = lineIndex + 1;
const column = match.index + 1;
const length = match[0].length;
if (!withinAnyRange(exclusions, lineIndex + 1, column, length)) {
/** @type {import("../helpers").FileRange} */
const range = { "startLine": lineNumber, "startColumn": column, "endLine": lineNumber, "endColumn": column + length - 1 };
if (!codeRanges.some((codeRange) => hasOverlap(codeRange, range))) {
addError(
onError,
lineIndex + 1,
lineNumber,
"Column: " + column,
undefined,
[ column, length ],