mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 14:30:12 +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
|
|
@ -404,6 +404,36 @@ const withinAnyRange = (ranges, lineIndex, index, length) => (
|
||||||
);
|
);
|
||||||
module.exports.withinAnyRange = withinAnyRange;
|
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
|
// Determines if the front matter includes a title
|
||||||
module.exports.frontMatterHasTitle =
|
module.exports.frontMatterHasTitle =
|
||||||
function frontMatterHasTitle(frontMatterLines, frontMatterTitlePattern) {
|
function frontMatterHasTitle(frontMatterLines, frontMatterTitlePattern) {
|
||||||
|
|
@ -1188,31 +1218,6 @@ function getDescendantsByType(parent, typePath) {
|
||||||
return tokens;
|
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.
|
* Gets the heading level of a Micromark heading tokan.
|
||||||
*
|
*
|
||||||
|
|
@ -1335,7 +1340,6 @@ module.exports = {
|
||||||
filterByPredicate,
|
filterByPredicate,
|
||||||
filterByTypes,
|
filterByTypes,
|
||||||
getDescendantsByType,
|
getDescendantsByType,
|
||||||
getExclusionsForToken,
|
|
||||||
getHeadingLevel,
|
getHeadingLevel,
|
||||||
getHeadingStyle,
|
getHeadingStyle,
|
||||||
getHeadingText,
|
getHeadingText,
|
||||||
|
|
@ -3629,8 +3633,8 @@ module.exports = {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const { addError, withinAnyRange } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
|
const { addError, hasOverlap } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
|
||||||
const { getDescendantsByType, getExclusionsForToken } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
|
const { getDescendantsByType } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
|
||||||
const { filterByTypesCached } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
|
const { filterByTypesCached } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
|
||||||
|
|
||||||
const tabRe = /\t+/g;
|
const tabRe = /\t+/g;
|
||||||
|
|
@ -3653,7 +3657,6 @@ module.exports = {
|
||||||
const spaceMultiplier = (spacesPerTab === undefined) ?
|
const spaceMultiplier = (spacesPerTab === undefined) ?
|
||||||
1 :
|
1 :
|
||||||
Math.max(0, Number(spacesPerTab));
|
Math.max(0, Number(spacesPerTab));
|
||||||
const exclusions = [];
|
|
||||||
// eslint-disable-next-line jsdoc/valid-types
|
// eslint-disable-next-line jsdoc/valid-types
|
||||||
/** @type import("../helpers/micromark.cjs").TokenType[] */
|
/** @type import("../helpers/micromark.cjs").TokenType[] */
|
||||||
const exclusionTypes = [];
|
const exclusionTypes = [];
|
||||||
|
|
@ -3671,24 +3674,29 @@ module.exports = {
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
for (const codeToken of codeTokens) {
|
const codeRanges = codeTokens.map((token) => {
|
||||||
const exclusionsForToken = getExclusionsForToken(params.lines, codeToken);
|
const { type, startLine, startColumn, endLine, endColumn } = token;
|
||||||
if (codeToken.type === "codeFenced") {
|
const codeFenced = (type === "codeFenced");
|
||||||
exclusionsForToken.pop();
|
return {
|
||||||
exclusionsForToken.shift();
|
"startLine": startLine + (codeFenced ? 1 : 0),
|
||||||
}
|
"startColumn": codeFenced ? 0 : startColumn,
|
||||||
exclusions.push(...exclusionsForToken);
|
"endLine": endLine - (codeFenced ? 1 : 0),
|
||||||
}
|
"endColumn": codeFenced ? Number.MAX_SAFE_INTEGER : endColumn
|
||||||
|
};
|
||||||
|
});
|
||||||
for (let lineIndex = 0; lineIndex < params.lines.length; lineIndex++) {
|
for (let lineIndex = 0; lineIndex < params.lines.length; lineIndex++) {
|
||||||
const line = params.lines[lineIndex];
|
const line = params.lines[lineIndex];
|
||||||
let match = null;
|
let match = null;
|
||||||
while ((match = tabRe.exec(line)) !== null) {
|
while ((match = tabRe.exec(line)) !== null) {
|
||||||
|
const lineNumber = lineIndex + 1;
|
||||||
const column = match.index + 1;
|
const column = match.index + 1;
|
||||||
const length = match[0].length;
|
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(
|
addError(
|
||||||
onError,
|
onError,
|
||||||
lineIndex + 1,
|
lineNumber,
|
||||||
"Column: " + column,
|
"Column: " + column,
|
||||||
undefined,
|
undefined,
|
||||||
[ column, length ],
|
[ column, length ],
|
||||||
|
|
@ -3718,8 +3726,8 @@ module.exports = {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const { addError, withinAnyRange } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
|
const { addError, hasOverlap } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
|
||||||
const { addRangeToSet, getExclusionsForToken } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
|
const { addRangeToSet } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
|
||||||
const { filterByTypesCached } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
|
const { filterByTypesCached } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
|
||||||
|
|
||||||
const reversedLinkRe =
|
const reversedLinkRe =
|
||||||
|
|
@ -3737,30 +3745,30 @@ module.exports = {
|
||||||
for (const codeBlock of filterByTypesCached([ "codeFenced", "codeIndented" ])) {
|
for (const codeBlock of filterByTypesCached([ "codeFenced", "codeIndented" ])) {
|
||||||
addRangeToSet(codeBlockLineNumbers, codeBlock.startLine, codeBlock.endLine);
|
addRangeToSet(codeBlockLineNumbers, codeBlock.startLine, codeBlock.endLine);
|
||||||
}
|
}
|
||||||
const exclusions = [];
|
const codeTexts = filterByTypesCached([ "codeText" ]);
|
||||||
for (const codeText of filterByTypesCached([ "codeText" ])) {
|
|
||||||
exclusions.push(...getExclusionsForToken(params.lines, codeText));
|
|
||||||
}
|
|
||||||
for (const [ lineIndex, line ] of params.lines.entries()) {
|
for (const [ lineIndex, line ] of params.lines.entries()) {
|
||||||
if (!codeBlockLineNumbers.has(lineIndex + 1)) {
|
const lineNumber = lineIndex + 1;
|
||||||
|
if (!codeBlockLineNumbers.has(lineNumber)) {
|
||||||
let match = null;
|
let match = null;
|
||||||
while ((match = reversedLinkRe.exec(line)) !== null) {
|
while ((match = reversedLinkRe.exec(line)) !== null) {
|
||||||
const [ reversedLink, preChar, linkText, linkDestination ] = match;
|
const [ reversedLink, preChar, linkText, linkDestination ] = match;
|
||||||
const index = match.index + preChar.length;
|
|
||||||
const length = match[0].length - preChar.length;
|
|
||||||
if (
|
if (
|
||||||
!linkText.endsWith("\\") &&
|
!linkText.endsWith("\\") &&
|
||||||
!linkDestination.endsWith("\\") &&
|
!linkDestination.endsWith("\\")
|
||||||
!withinAnyRange(exclusions, lineIndex + 1, index, length)
|
|
||||||
) {
|
) {
|
||||||
|
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(
|
addError(
|
||||||
onError,
|
onError,
|
||||||
lineIndex + 1,
|
lineNumber,
|
||||||
reversedLink.slice(preChar.length),
|
reversedLink.slice(preChar.length),
|
||||||
undefined,
|
undefined,
|
||||||
[ index + 1, length ],
|
[ column, length ],
|
||||||
{
|
{
|
||||||
"editColumn": index + 1,
|
"editColumn": column,
|
||||||
"deleteCount": length,
|
"deleteCount": length,
|
||||||
"insertText": `[${linkText}](${linkDestination})`
|
"insertText": `[${linkText}](${linkDestination})`
|
||||||
}
|
}
|
||||||
|
|
@ -3770,6 +3778,7 @@ module.exports = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -392,6 +392,36 @@ const withinAnyRange = (ranges, lineIndex, index, length) => (
|
||||||
);
|
);
|
||||||
module.exports.withinAnyRange = withinAnyRange;
|
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
|
// Determines if the front matter includes a title
|
||||||
module.exports.frontMatterHasTitle =
|
module.exports.frontMatterHasTitle =
|
||||||
function frontMatterHasTitle(frontMatterLines, frontMatterTitlePattern) {
|
function frontMatterHasTitle(frontMatterLines, frontMatterTitlePattern) {
|
||||||
|
|
|
||||||
|
|
@ -335,31 +335,6 @@ function getDescendantsByType(parent, typePath) {
|
||||||
return tokens;
|
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.
|
* Gets the heading level of a Micromark heading tokan.
|
||||||
*
|
*
|
||||||
|
|
@ -482,7 +457,6 @@ module.exports = {
|
||||||
filterByPredicate,
|
filterByPredicate,
|
||||||
filterByTypes,
|
filterByTypes,
|
||||||
getDescendantsByType,
|
getDescendantsByType,
|
||||||
getExclusionsForToken,
|
|
||||||
getHeadingLevel,
|
getHeadingLevel,
|
||||||
getHeadingStyle,
|
getHeadingStyle,
|
||||||
getHeadingText,
|
getHeadingText,
|
||||||
|
|
|
||||||
30
lib/md010.js
30
lib/md010.js
|
|
@ -2,8 +2,8 @@
|
||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const { addError, withinAnyRange } = require("../helpers");
|
const { addError, hasOverlap } = require("../helpers");
|
||||||
const { getDescendantsByType, getExclusionsForToken } = require("../helpers/micromark.cjs");
|
const { getDescendantsByType } = require("../helpers/micromark.cjs");
|
||||||
const { filterByTypesCached } = require("./cache");
|
const { filterByTypesCached } = require("./cache");
|
||||||
|
|
||||||
const tabRe = /\t+/g;
|
const tabRe = /\t+/g;
|
||||||
|
|
@ -26,7 +26,6 @@ module.exports = {
|
||||||
const spaceMultiplier = (spacesPerTab === undefined) ?
|
const spaceMultiplier = (spacesPerTab === undefined) ?
|
||||||
1 :
|
1 :
|
||||||
Math.max(0, Number(spacesPerTab));
|
Math.max(0, Number(spacesPerTab));
|
||||||
const exclusions = [];
|
|
||||||
// eslint-disable-next-line jsdoc/valid-types
|
// eslint-disable-next-line jsdoc/valid-types
|
||||||
/** @type import("../helpers/micromark.cjs").TokenType[] */
|
/** @type import("../helpers/micromark.cjs").TokenType[] */
|
||||||
const exclusionTypes = [];
|
const exclusionTypes = [];
|
||||||
|
|
@ -44,24 +43,29 @@ module.exports = {
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
for (const codeToken of codeTokens) {
|
const codeRanges = codeTokens.map((token) => {
|
||||||
const exclusionsForToken = getExclusionsForToken(params.lines, codeToken);
|
const { type, startLine, startColumn, endLine, endColumn } = token;
|
||||||
if (codeToken.type === "codeFenced") {
|
const codeFenced = (type === "codeFenced");
|
||||||
exclusionsForToken.pop();
|
return {
|
||||||
exclusionsForToken.shift();
|
"startLine": startLine + (codeFenced ? 1 : 0),
|
||||||
}
|
"startColumn": codeFenced ? 0 : startColumn,
|
||||||
exclusions.push(...exclusionsForToken);
|
"endLine": endLine - (codeFenced ? 1 : 0),
|
||||||
}
|
"endColumn": codeFenced ? Number.MAX_SAFE_INTEGER : endColumn
|
||||||
|
};
|
||||||
|
});
|
||||||
for (let lineIndex = 0; lineIndex < params.lines.length; lineIndex++) {
|
for (let lineIndex = 0; lineIndex < params.lines.length; lineIndex++) {
|
||||||
const line = params.lines[lineIndex];
|
const line = params.lines[lineIndex];
|
||||||
let match = null;
|
let match = null;
|
||||||
while ((match = tabRe.exec(line)) !== null) {
|
while ((match = tabRe.exec(line)) !== null) {
|
||||||
|
const lineNumber = lineIndex + 1;
|
||||||
const column = match.index + 1;
|
const column = match.index + 1;
|
||||||
const length = match[0].length;
|
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(
|
addError(
|
||||||
onError,
|
onError,
|
||||||
lineIndex + 1,
|
lineNumber,
|
||||||
"Column: " + column,
|
"Column: " + column,
|
||||||
undefined,
|
undefined,
|
||||||
[ column, length ],
|
[ column, length ],
|
||||||
|
|
|
||||||
29
lib/md011.js
29
lib/md011.js
|
|
@ -2,8 +2,8 @@
|
||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const { addError, withinAnyRange } = require("../helpers");
|
const { addError, hasOverlap } = require("../helpers");
|
||||||
const { addRangeToSet, getExclusionsForToken } = require("../helpers/micromark.cjs");
|
const { addRangeToSet } = require("../helpers/micromark.cjs");
|
||||||
const { filterByTypesCached } = require("./cache");
|
const { filterByTypesCached } = require("./cache");
|
||||||
|
|
||||||
const reversedLinkRe =
|
const reversedLinkRe =
|
||||||
|
|
@ -21,30 +21,30 @@ module.exports = {
|
||||||
for (const codeBlock of filterByTypesCached([ "codeFenced", "codeIndented" ])) {
|
for (const codeBlock of filterByTypesCached([ "codeFenced", "codeIndented" ])) {
|
||||||
addRangeToSet(codeBlockLineNumbers, codeBlock.startLine, codeBlock.endLine);
|
addRangeToSet(codeBlockLineNumbers, codeBlock.startLine, codeBlock.endLine);
|
||||||
}
|
}
|
||||||
const exclusions = [];
|
const codeTexts = filterByTypesCached([ "codeText" ]);
|
||||||
for (const codeText of filterByTypesCached([ "codeText" ])) {
|
|
||||||
exclusions.push(...getExclusionsForToken(params.lines, codeText));
|
|
||||||
}
|
|
||||||
for (const [ lineIndex, line ] of params.lines.entries()) {
|
for (const [ lineIndex, line ] of params.lines.entries()) {
|
||||||
if (!codeBlockLineNumbers.has(lineIndex + 1)) {
|
const lineNumber = lineIndex + 1;
|
||||||
|
if (!codeBlockLineNumbers.has(lineNumber)) {
|
||||||
let match = null;
|
let match = null;
|
||||||
while ((match = reversedLinkRe.exec(line)) !== null) {
|
while ((match = reversedLinkRe.exec(line)) !== null) {
|
||||||
const [ reversedLink, preChar, linkText, linkDestination ] = match;
|
const [ reversedLink, preChar, linkText, linkDestination ] = match;
|
||||||
const index = match.index + preChar.length;
|
|
||||||
const length = match[0].length - preChar.length;
|
|
||||||
if (
|
if (
|
||||||
!linkText.endsWith("\\") &&
|
!linkText.endsWith("\\") &&
|
||||||
!linkDestination.endsWith("\\") &&
|
!linkDestination.endsWith("\\")
|
||||||
!withinAnyRange(exclusions, lineIndex + 1, index, length)
|
|
||||||
) {
|
) {
|
||||||
|
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(
|
addError(
|
||||||
onError,
|
onError,
|
||||||
lineIndex + 1,
|
lineNumber,
|
||||||
reversedLink.slice(preChar.length),
|
reversedLink.slice(preChar.length),
|
||||||
undefined,
|
undefined,
|
||||||
[ index + 1, length ],
|
[ column, length ],
|
||||||
{
|
{
|
||||||
"editColumn": index + 1,
|
"editColumn": column,
|
||||||
"deleteCount": length,
|
"deleteCount": length,
|
||||||
"insertText": `[${linkText}](${linkDestination})`
|
"insertText": `[${linkText}](${linkDestination})`
|
||||||
}
|
}
|
||||||
|
|
@ -54,4 +54,5 @@ module.exports = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -968,3 +968,92 @@ test("ellipsify", (t) => {
|
||||||
t.is(helpers.ellipsify("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", false, true), "...wxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
|
t.is(helpers.ellipsify("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", false, true), "...wxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
|
||||||
t.is(helpers.ellipsify("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", true, true), "abcdefghijklmno...LMNOPQRSTUVWXYZ");
|
t.is(helpers.ellipsify("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", true, true), "abcdefghijklmno...LMNOPQRSTUVWXYZ");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("hasOverlap", (t) => {
|
||||||
|
t.plan(32);
|
||||||
|
/** @type {import("../helpers").FileRange[][]} */
|
||||||
|
const trueTestCases = [
|
||||||
|
// Same line
|
||||||
|
[
|
||||||
|
{ "startLine": 1, "endLine": 1, "startColumn": 1, "endColumn": 1 },
|
||||||
|
{ "startLine": 1, "endLine": 1, "startColumn": 1, "endColumn": 1 }
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{ "startLine": 1, "endLine": 1, "startColumn": 1, "endColumn": 2 },
|
||||||
|
{ "startLine": 1, "endLine": 1, "startColumn": 2, "endColumn": 2 }
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{ "startLine": 1, "endLine": 1, "startColumn": 1, "endColumn": 3 },
|
||||||
|
{ "startLine": 1, "endLine": 1, "startColumn": 2, "endColumn": 2 }
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{ "startLine": 1, "endLine": 1, "startColumn": 1, "endColumn": 2 },
|
||||||
|
{ "startLine": 1, "endLine": 1, "startColumn": 2, "endColumn": 3 }
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{ "startLine": 1, "endLine": 1, "startColumn": 1, "endColumn": 3 },
|
||||||
|
{ "startLine": 1, "endLine": 1, "startColumn": 2, "endColumn": 3 }
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{ "startLine": 1, "endLine": 1, "startColumn": 1, "endColumn": 4 },
|
||||||
|
{ "startLine": 1, "endLine": 1, "startColumn": 2, "endColumn": 3 }
|
||||||
|
],
|
||||||
|
// Common line
|
||||||
|
[
|
||||||
|
{ "startLine": 1, "endLine": 2, "startColumn": 1, "endColumn": 2 },
|
||||||
|
{ "startLine": 2, "endLine": 2, "startColumn": 2, "endColumn": 4 }
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{ "startLine": 1, "endLine": 2, "startColumn": 1, "endColumn": 2 },
|
||||||
|
{ "startLine": 2, "endLine": 2, "startColumn": 1, "endColumn": 1 }
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{ "startLine": 1, "endLine": 1, "startColumn": 1, "endColumn": 2 },
|
||||||
|
{ "startLine": 1, "endLine": 2, "startColumn": 2, "endColumn": 4 }
|
||||||
|
],
|
||||||
|
// Common lines
|
||||||
|
[
|
||||||
|
{ "startLine": 1, "endLine": 3, "startColumn": 1, "endColumn": 2 },
|
||||||
|
{ "startLine": 2, "endLine": 4, "startColumn": 3, "endColumn": 4 }
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{ "startLine": 1, "endLine": 4, "startColumn": 1, "endColumn": 2 },
|
||||||
|
{ "startLine": 2, "endLine": 3, "startColumn": 3, "endColumn": 4 }
|
||||||
|
]
|
||||||
|
];
|
||||||
|
for (const trueTestCase of trueTestCases) {
|
||||||
|
const [ rangeA, rangeB ] = trueTestCase;
|
||||||
|
t.true(helpers.hasOverlap(rangeA, rangeB), JSON.stringify({ rangeA, rangeB }));
|
||||||
|
t.true(helpers.hasOverlap(rangeB, rangeA), JSON.stringify({ rangeB, rangeA }));
|
||||||
|
}
|
||||||
|
const falseTestCases = [
|
||||||
|
// Same line
|
||||||
|
[
|
||||||
|
{ "startLine": 1, "endLine": 1, "startColumn": 1, "endColumn": 1 },
|
||||||
|
{ "startLine": 1, "endLine": 1, "startColumn": 2, "endColumn": 2 }
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{ "startLine": 1, "endLine": 1, "startColumn": 1, "endColumn": 2 },
|
||||||
|
{ "startLine": 1, "endLine": 1, "startColumn": 3, "endColumn": 4 }
|
||||||
|
],
|
||||||
|
// Common line
|
||||||
|
[
|
||||||
|
{ "startLine": 1, "endLine": 2, "startColumn": 1, "endColumn": 2 },
|
||||||
|
{ "startLine": 2, "endLine": 3, "startColumn": 3, "endColumn": 4 }
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{ "startLine": 1, "endLine": 2, "startColumn": 4, "endColumn": 2 },
|
||||||
|
{ "startLine": 2, "endLine": 3, "startColumn": 4, "endColumn": 2 }
|
||||||
|
],
|
||||||
|
// No common lines
|
||||||
|
[
|
||||||
|
{ "startLine": 1, "endLine": 2, "startColumn": 1, "endColumn": 4 },
|
||||||
|
{ "startLine": 3, "endLine": 4, "startColumn": 2, "endColumn": 3 }
|
||||||
|
]
|
||||||
|
];
|
||||||
|
for (const falseTestCase of falseTestCases) {
|
||||||
|
const [ rangeA, rangeB ] = falseTestCase;
|
||||||
|
t.false(helpers.hasOverlap(rangeA, rangeB), JSON.stringify({ rangeA, rangeB }));
|
||||||
|
t.false(helpers.hasOverlap(rangeB, rangeA), JSON.stringify({ rangeB, rangeA }));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue