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

@ -523,6 +523,47 @@ module.exports.addErrorContext = function addErrorContext(
addError(onError, lineNumber, null, context, range, fixInfo);
};
/**
* Returns an array of code span ranges.
*
* @param {string[]} lines Lines to scan for code span ranges.
* @returns {number[][]} Array of ranges (line, index, length).
*/
module.exports.inlineCodeSpanRanges = (lines) => {
const exclusions = [];
forEachInlineCodeSpan(
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;
}
}
);
return exclusions;
};
/**
* Determines whether the specified range overlaps another range.
*
* @param {number[][]} ranges Array of ranges (line, index, length).
* @param {number} lineIndex Line index to check.
* @param {number} index Index to check.
* @param {number} length Length to check.
* @returns {boolean} True iff the specified range overlaps.
*/
module.exports.overlapsAnyRange = (ranges, lineIndex, index, length) => (
!ranges.every((span) => (
(lineIndex !== span[0]) ||
(index + length < span[1]) ||
(index > span[1] + span[2])
))
);
// Returns a range object for a line by applying a RegExp
module.exports.rangeFromRegExp = function rangeFromRegExp(line, regexp) {
let range = null;