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

View file

@ -19,7 +19,7 @@ const inlineCommentStartRe =
module.exports.inlineCommentStartRe = inlineCommentStartRe; module.exports.inlineCommentStartRe = inlineCommentStartRe;
// Regular expression for matching HTML elements // 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; module.exports.htmlElementRe = htmlElementRe;
// Regular expressions for range matching // Regular expressions for range matching
@ -642,23 +642,6 @@ module.exports.htmlElementRanges = (params, lineMetadata) => {
return exclusions; 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. * 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]) (index + length > span[1] + span[2])
)) ))
); );
module.exports.withinAnyRange = withinAnyRange;
// Returns a range object for a line by applying a RegExp // Returns a range object for a line by applying a RegExp
module.exports.rangeFromRegExp = function rangeFromRegExp(line, regexp) { module.exports.rangeFromRegExp = function rangeFromRegExp(line, regexp) {

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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