Replace helpers.overlapsAnyRange with .withinAnyRange, update code/tests for resulting behavior.

This commit is contained in:
David Anson 2022-06-02 22:17:32 -07:00
parent cb943a8718
commit 6718944b0f
10 changed files with 70 additions and 53 deletions

View file

@ -44,7 +44,7 @@ const inlineCommentStartRe =
/(<!--\s*markdownlint-(disable|enable|capture|restore|disable-file|enable-file|disable-line|disable-next-line|configure-file))(?:\s|-->)/ig;
module.exports.inlineCommentStartRe = inlineCommentStartRe;
// Regular expression for matching HTML elements
const htmlElementRe = /<(([A-Za-z][A-Za-z0-9-]*)(?:\s[^>]*)?)\/?>/g;
const htmlElementRe = /<(([A-Za-z][A-Za-z0-9-]*)(?:\s[^`>]*)?)\/?>/g;
module.exports.htmlElementRe = htmlElementRe;
// Regular expressions for range matching
module.exports.bareUrlRe = /(?:http|ftp)s?:\/\/[^\s\]"']*(?:\/|[^\s\]"'\W])/ig;
@ -632,18 +632,6 @@ module.exports.htmlElementRanges = (params, lineMetadata) => {
});
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]))));
/**
* Determines whether the specified range is within another range.
*
@ -656,6 +644,7 @@ module.exports.overlapsAnyRange = (ranges, lineIndex, index, length) => (!ranges
const withinAnyRange = (ranges, lineIndex, index, length) => (!ranges.every((span) => ((lineIndex !== span[0]) ||
(index < span[1]) ||
(index + length > span[1] + span[2]))));
module.exports.withinAnyRange = withinAnyRange;
// Returns a range object for a line by applying a RegExp
module.exports.rangeFromRegExp = function rangeFromRegExp(line, regexp) {
let range = null;
@ -2715,7 +2704,7 @@ module.exports = {
"use strict";
// @ts-check
const { addError, filterTokens, forEachLine, overlapsAnyRange } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { addError, filterTokens, forEachLine, withinAnyRange } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { codeBlockAndSpanRanges, lineMetadata } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
const tabRe = /\t+/g;
module.exports = {
@ -2747,7 +2736,7 @@ module.exports = {
const { index } = match;
const column = index + 1;
const length = match[0].length;
if (!overlapsAnyRange(exclusions, lineIndex, index, length)) {
if (!withinAnyRange(exclusions, lineIndex, index, length)) {
addError(onError, lineIndex + 1, "Column: " + column, null, [column, length], {
"editColumn": column,
"deleteCount": length,
@ -2772,7 +2761,7 @@ module.exports = {
"use strict";
// @ts-check
const { addError, forEachLine, overlapsAnyRange } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { addError, forEachLine, withinAnyRange } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { codeBlockAndSpanRanges, lineMetadata } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
const reversedLinkRe = /(^|[^\\])\(([^)]+)\)\[([^\]^][^\]]*)](?!\()/g;
module.exports = {
@ -2790,7 +2779,7 @@ module.exports = {
const length = match[0].length - preChar.length;
if (!linkText.endsWith("\\") &&
!linkDestination.endsWith("\\") &&
!overlapsAnyRange(exclusions, lineIndex, index, length)) {
!withinAnyRange(exclusions, lineIndex, index, length)) {
addError(onError, lineIndex + 1, reversedLink.slice(preChar.length), null, [index + 1, length], {
"editColumn": index + 1,
"deleteCount": length,
@ -3660,7 +3649,7 @@ module.exports = {
"use strict";
// @ts-check
const { addError, forEachLine, htmlElementRe, overlapsAnyRange, unescapeMarkdown } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { addError, forEachLine, htmlElementRe, withinAnyRange, unescapeMarkdown } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { codeBlockAndSpanRanges, lineMetadata } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
const linkDestinationRe = /]\(\s*$/;
// See https://spec.commonmark.org/0.29/#autolinks
@ -3684,7 +3673,7 @@ module.exports = {
if (!allowedElements.includes(element.toLowerCase()) &&
!tag.endsWith("\\>") &&
!emailAddressRe.test(content) &&
!overlapsAnyRange(exclusions, lineIndex, match.index, match[0].length)) {
!withinAnyRange(exclusions, lineIndex, match.index, match[0].length)) {
const prefix = line.substring(0, match.index);
if (!linkDestinationRe.test(prefix)) {
const unescaped = unescapeMarkdown(prefix + "<", "_");
@ -4366,7 +4355,7 @@ module.exports = {
"use strict";
// @ts-check
const { addErrorDetailIf, bareUrlRe, escapeForRegExp, forEachLine, forEachLink, overlapsAnyRange, linkReferenceDefinitionRe } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { addErrorDetailIf, bareUrlRe, escapeForRegExp, forEachLine, forEachLink, withinAnyRange, linkReferenceDefinitionRe } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { codeBlockAndSpanRanges, htmlElementRanges, lineMetadata } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
module.exports = {
"names": ["MD044", "proper-names"],
@ -4416,7 +4405,7 @@ module.exports = {
const [, leftMatch, nameMatch] = match;
const index = match.index + leftMatch.length;
const length = nameMatch.length;
if (!overlapsAnyRange(exclusions, lineIndex, index, length) &&
if (!withinAnyRange(exclusions, lineIndex, index, length) &&
!names.includes(nameMatch)) {
addErrorDetailIf(onError, lineIndex + 1, name, nameMatch, null, null, [index + 1, length], {
"editColumn": index + 1,

View file

@ -19,7 +19,7 @@ const inlineCommentStartRe =
module.exports.inlineCommentStartRe = inlineCommentStartRe;
// Regular expression for matching HTML elements
const htmlElementRe = /<(([A-Za-z][A-Za-z0-9-]*)(?:\s[^>]*)?)\/?>/g;
const htmlElementRe = /<(([A-Za-z][A-Za-z0-9-]*)(?:\s[^`>]*)?)\/?>/g;
module.exports.htmlElementRe = htmlElementRe;
// Regular expressions for range matching
@ -642,23 +642,6 @@ module.exports.htmlElementRanges = (params, lineMetadata) => {
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])
))
);
/**
* Determines whether the specified range is within another range.
*
@ -675,6 +658,7 @@ const withinAnyRange = (ranges, lineIndex, index, length) => (
(index + length > span[1] + span[2])
))
);
module.exports.withinAnyRange = withinAnyRange;
// Returns a range object for a line by applying a RegExp
module.exports.rangeFromRegExp = function rangeFromRegExp(line, regexp) {

View file

@ -2,7 +2,7 @@
"use strict";
const { addError, filterTokens, forEachLine, overlapsAnyRange } =
const { addError, filterTokens, forEachLine, withinAnyRange } =
require("../helpers");
const { codeBlockAndSpanRanges, lineMetadata } = require("./cache");
@ -39,7 +39,7 @@ module.exports = {
const { index } = match;
const column = index + 1;
const length = match[0].length;
if (!overlapsAnyRange(exclusions, lineIndex, index, length)) {
if (!withinAnyRange(exclusions, lineIndex, index, length)) {
addError(
onError,
lineIndex + 1,

View file

@ -2,7 +2,7 @@
"use strict";
const { addError, forEachLine, overlapsAnyRange } = require("../helpers");
const { addError, forEachLine, withinAnyRange } = require("../helpers");
const { codeBlockAndSpanRanges, lineMetadata } = require("./cache");
const reversedLinkRe =
@ -24,7 +24,7 @@ module.exports = {
if (
!linkText.endsWith("\\") &&
!linkDestination.endsWith("\\") &&
!overlapsAnyRange(exclusions, lineIndex, index, length)
!withinAnyRange(exclusions, lineIndex, index, length)
) {
addError(
onError,

View file

@ -3,7 +3,7 @@
"use strict";
const {
addError, forEachLine, htmlElementRe, overlapsAnyRange, unescapeMarkdown
addError, forEachLine, htmlElementRe, withinAnyRange, unescapeMarkdown
} = require("../helpers");
const { codeBlockAndSpanRanges, lineMetadata } = require("./cache");
@ -31,7 +31,7 @@ module.exports = {
!allowedElements.includes(element.toLowerCase()) &&
!tag.endsWith("\\>") &&
!emailAddressRe.test(content) &&
!overlapsAnyRange(exclusions, lineIndex, match.index, match[0].length)
!withinAnyRange(exclusions, lineIndex, match.index, match[0].length)
) {
const prefix = line.substring(0, match.index);
if (!linkDestinationRe.test(prefix)) {

View file

@ -3,7 +3,7 @@
"use strict";
const { addErrorDetailIf, bareUrlRe, escapeForRegExp, forEachLine,
forEachLink, overlapsAnyRange, linkReferenceDefinitionRe } =
forEachLink, withinAnyRange, linkReferenceDefinitionRe } =
require("../helpers");
const { codeBlockAndSpanRanges, htmlElementRanges, lineMetadata } =
require("./cache");
@ -61,7 +61,7 @@ module.exports = {
const index = match.index + leftMatch.length;
const length = nameMatch.length;
if (
!overlapsAnyRange(exclusions, lineIndex, index, length) &&
!withinAnyRange(exclusions, lineIndex, index, length) &&
!names.includes(nameMatch)
) {
addErrorDetailIf(

View file

@ -109,8 +109,10 @@ text
Text `code <element> code` text
Text `code <element ` `attribute="value"/> code` text {MD038}
```lang
code {MD046:112}
code {MD046:114}
<element>
```

View file

@ -18,4 +18,4 @@ javascript {MD044}
<javascript/>
<code>javascript</code>
<code>javascript</code> {MD044}

View file

@ -19934,7 +19934,7 @@ Generated by [AVA](https://avajs.dev).
9,
],
fixInfo: null,
lineNumber: 120,
lineNumber: 122,
ruleDescription: 'Inline HTML',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md033',
ruleNames: [
@ -19942,6 +19942,26 @@ Generated by [AVA](https://avajs.dev).
'no-inline-html',
],
},
{
errorContext: '`code <element `',
errorDetail: null,
errorRange: [
6,
16,
],
fixInfo: {
deleteCount: 14,
editColumn: 7,
insertText: 'code <element',
},
lineNumber: 112,
ruleDescription: 'Spaces inside code span elements',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md038',
ruleNames: [
'MD038',
'no-space-in-code',
],
},
{
errorContext: null,
errorDetail: 'Expected: indented; Actual: fenced',
@ -19960,7 +19980,7 @@ Generated by [AVA](https://avajs.dev).
errorDetail: 'Expected: indented; Actual: fenced',
errorRange: null,
fixInfo: null,
lineNumber: 112,
lineNumber: 114,
ruleDescription: 'Code block style',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md046',
ruleNames: [
@ -20080,8 +20100,10 @@ Generated by [AVA](https://avajs.dev).
Text \`code <element> code\` text␊
Text \`code <element\` \`attribute="value"/> code\` text {MD038}␊
\`\`\`lang␊
code {MD046:112}␊
code {MD046:114}␊
<element>
\`\`\`␊
@ -31791,6 +31813,26 @@ Generated by [AVA](https://avajs.dev).
'proper-names',
],
},
{
errorContext: null,
errorDetail: 'Expected: JavaScript; Actual: javascript',
errorRange: [
7,
10,
],
fixInfo: {
deleteCount: 10,
editColumn: 7,
insertText: 'JavaScript',
},
lineNumber: 21,
ruleDescription: 'Proper names should have the correct capitalization',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md044',
ruleNames: [
'MD044',
'proper-names',
],
},
],
fixed: `# Proper Names No HTML␊
@ -31812,7 +31854,7 @@ Generated by [AVA](https://avajs.dev).
<javascript/>
<code>javascript</code>
<code>JavaScript</code> {MD044}
`,
}