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

@ -392,6 +392,36 @@ const withinAnyRange = (ranges, lineIndex, index, length) => (
);
module.exports.withinAnyRange = withinAnyRange;
/**
* Defines a range within a file (start line/column to end line/column, subset of MicromarkToken).
*
* @typedef {Object} FileRange
* @property {number} startLine Start line (1-based).
* @property {number} startColumn Start column (1-based).
* @property {number} endLine End line (1-based).
* @property {number} endColumn End column (1-based).
*/
const positionLessThanOrEqual = (lineA, columnA, lineB, columnB) => (
(lineA < lineB) ||
((lineA === lineB) && (columnA <= columnB))
);
/**
* Returns whether two ranges (or MicromarkTokens) overlap anywhere.
*
* @param {FileRange|import("../lib/markdownlint.js").MicromarkToken} rangeA Range A.
* @param {FileRange|import("../lib/markdownlint.js").MicromarkToken} rangeB Range B.
* @returns {boolean} Whether the two ranges overlap.
*/
const hasOverlap = (rangeA, rangeB) => {
const lte = positionLessThanOrEqual(rangeA.startLine, rangeA.startColumn, rangeB.startLine, rangeB.startColumn);
const first = lte ? rangeA : rangeB;
const second = lte ? rangeB : rangeA;
return positionLessThanOrEqual(second.startLine, second.startColumn, first.endLine, first.endColumn);
};
module.exports.hasOverlap = hasOverlap;
// Determines if the front matter includes a title
module.exports.frontMatterHasTitle =
function frontMatterHasTitle(frontMatterLines, frontMatterTitlePattern) {

View file

@ -335,31 +335,6 @@ function getDescendantsByType(parent, typePath) {
return tokens;
}
// eslint-disable-next-line jsdoc/valid-types
/** @typedef {readonly string[]} ReadonlyStringArray */
/**
* Gets the line/column/length exclusions for a Micromark token.
*
* @param {ReadonlyStringArray} lines File/string lines.
* @param {Token} token Micromark token.
* @returns {number[][]} Exclusions (line number, start column, length).
*/
function getExclusionsForToken(lines, token) {
const exclusions = [];
const { endColumn, endLine, startColumn, startLine } = token;
for (let lineNumber = startLine; lineNumber <= endLine; lineNumber++) {
const start = (lineNumber === startLine) ? startColumn : 1;
const end = (lineNumber === endLine) ? endColumn : lines[lineNumber - 1].length;
exclusions.push([
lineNumber,
start,
end - start + 1
]);
}
return exclusions;
}
/**
* Gets the heading level of a Micromark heading tokan.
*
@ -482,7 +457,6 @@ module.exports = {
filterByPredicate,
filterByTypes,
getDescendantsByType,
getExclusionsForToken,
getHeadingLevel,
getHeadingStyle,
getHeadingText,