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,40 +2,41 @@
"use strict";
const { addError, forEachInlineChild, unescapeMarkdown } =
const { addError, forEachLine, inlineCodeSpanRanges, overlapsAnyRange } =
require("../helpers");
const { lineMetadata } = require("./cache");
const reversedLinkRe = /\(([^)]+)\)\[([^\]^][^\]]*)]/g;
const reversedLinkRe =
/(?<![\\\]])\(([^)]+)(?<!\\)\)\[([^\]^][^\]]*)(?<!\\)](?!\()/g;
module.exports = {
"names": [ "MD011", "no-reversed-links" ],
"description": "Reversed link syntax",
"tags": [ "links" ],
"function": function MD011(params, onError) {
forEachInlineChild(params, "text", (token) => {
const { lineNumber, content } = token;
let match = null;
while ((match = reversedLinkRe.exec(content)) !== null) {
const [ reversedLink, linkText, linkDestination ] = match;
const line = params.lines[lineNumber - 1];
const column = unescapeMarkdown(line).indexOf(reversedLink) + 1;
const length = reversedLink.length;
const range = column ? [ column, length ] : null;
const fixInfo = column ?
{
"editColumn": column,
"deleteCount": length,
"insertText": `[${linkText}](${linkDestination})`
} :
null;
addError(
onError,
lineNumber,
reversedLink,
null,
range,
fixInfo
);
const exclusions = inlineCodeSpanRanges(params.lines);
forEachLine(lineMetadata(), (line, lineIndex, inCode, onFence) => {
if (!inCode && !onFence) {
let match = null;
while ((match = reversedLinkRe.exec(line)) !== null) {
const [ reversedLink, linkText, linkDestination ] = match;
const index = match.index;
const length = match[0].length;
if (!overlapsAnyRange(exclusions, lineIndex, index, length)) {
addError(
onError,
lineIndex + 1,
reversedLink,
null,
[ index + 1, length ],
{
"editColumn": index + 1,
"deleteCount": length,
"insertText": `[${linkText}](${linkDestination})`
}
);
}
}
}
});
}

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,