Re-implement MD011/no-reversed-links for better accuracy (range and fixInfo are now always valid) (fixes #398).

This commit is contained in:
David Anson 2021-06-17 21:50:03 -07:00
parent 646a67b8bd
commit 706f48bd25
6 changed files with 165 additions and 91 deletions

View file

@ -2,8 +2,9 @@
"use strict";
const { addErrorDetailIf, bareUrlRe, escapeForRegExp, forEachLine, linkRe,
linkReferenceRe, newLineRe, forEachInlineCodeSpan } = require("../helpers");
const { addErrorDetailIf, bareUrlRe, escapeForRegExp, forEachLine,
inlineCodeSpanRanges, overlapsAnyRange, linkRe, linkReferenceRe } =
require("../helpers");
const { lineMetadata } = require("./cache");
module.exports = {
@ -36,19 +37,7 @@ module.exports = {
}
});
if (!includeCodeBlocks) {
forEachInlineCodeSpan(
params.lines.join("\n"),
(code, lineIndex, columnIndex) => {
const codeLines = code.split(newLineRe);
// eslint-disable-next-line unicorn/no-for-loop
for (let i = 0; i < codeLines.length; i++) {
exclusions.push(
[ lineIndex + i, columnIndex, codeLines[i].length ]
);
columnIndex = 0;
}
}
);
exclusions.push(...inlineCodeSpanRanges(params.lines));
}
for (const name of names) {
const escapedName = escapeForRegExp(name);
@ -64,13 +53,7 @@ module.exports = {
const [ , leftMatch, nameMatch ] = match;
const index = match.index + leftMatch.length;
const length = nameMatch.length;
if (
exclusions.every((span) => (
(lineIndex !== span[0]) ||
(index + length < span[1]) ||
(index > span[1] + span[2])
))
) {
if (!overlapsAnyRange(exclusions, lineIndex, index, length)) {
addErrorDetailIf(
onError,
lineIndex + 1,