mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 22:10:13 +01:00
Refactor to remove micromark helper getExclusionsForToken.
This commit is contained in:
parent
cfcb5fbd57
commit
5e568d0da9
6 changed files with 231 additions and 124 deletions
30
lib/md010.js
30
lib/md010.js
|
|
@ -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 ],
|
||||
|
|
|
|||
47
lib/md011.js
47
lib/md011.js
|
|
@ -2,8 +2,8 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
const { addError, withinAnyRange } = require("../helpers");
|
||||
const { addRangeToSet, getExclusionsForToken } = require("../helpers/micromark.cjs");
|
||||
const { addError, hasOverlap } = require("../helpers");
|
||||
const { addRangeToSet } = require("../helpers/micromark.cjs");
|
||||
const { filterByTypesCached } = require("./cache");
|
||||
|
||||
const reversedLinkRe =
|
||||
|
|
@ -21,34 +21,35 @@ module.exports = {
|
|||
for (const codeBlock of filterByTypesCached([ "codeFenced", "codeIndented" ])) {
|
||||
addRangeToSet(codeBlockLineNumbers, codeBlock.startLine, codeBlock.endLine);
|
||||
}
|
||||
const exclusions = [];
|
||||
for (const codeText of filterByTypesCached([ "codeText" ])) {
|
||||
exclusions.push(...getExclusionsForToken(params.lines, codeText));
|
||||
}
|
||||
const codeTexts = filterByTypesCached([ "codeText" ]);
|
||||
for (const [ lineIndex, line ] of params.lines.entries()) {
|
||||
if (!codeBlockLineNumbers.has(lineIndex + 1)) {
|
||||
const lineNumber = lineIndex + 1;
|
||||
if (!codeBlockLineNumbers.has(lineNumber)) {
|
||||
let match = null;
|
||||
while ((match = reversedLinkRe.exec(line)) !== null) {
|
||||
const [ reversedLink, preChar, linkText, linkDestination ] = match;
|
||||
const index = match.index + preChar.length;
|
||||
const length = match[0].length - preChar.length;
|
||||
if (
|
||||
!linkText.endsWith("\\") &&
|
||||
!linkDestination.endsWith("\\") &&
|
||||
!withinAnyRange(exclusions, lineIndex + 1, index, length)
|
||||
!linkDestination.endsWith("\\")
|
||||
) {
|
||||
addError(
|
||||
onError,
|
||||
lineIndex + 1,
|
||||
reversedLink.slice(preChar.length),
|
||||
undefined,
|
||||
[ index + 1, length ],
|
||||
{
|
||||
"editColumn": index + 1,
|
||||
"deleteCount": length,
|
||||
"insertText": `[${linkText}](${linkDestination})`
|
||||
}
|
||||
);
|
||||
const column = match.index + preChar.length + 1;
|
||||
const length = match[0].length - preChar.length;
|
||||
/** @type {import("../helpers").FileRange} */
|
||||
const range = { "startLine": lineNumber, "startColumn": column, "endLine": lineNumber, "endColumn": column + length - 1 };
|
||||
if (!codeTexts.some((codeText) => hasOverlap(codeText, range))) {
|
||||
addError(
|
||||
onError,
|
||||
lineNumber,
|
||||
reversedLink.slice(preChar.length),
|
||||
undefined,
|
||||
[ column, length ],
|
||||
{
|
||||
"editColumn": column,
|
||||
"deleteCount": length,
|
||||
"insertText": `[${linkText}](${linkDestination})`
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue