Refactor helpers slightly for consistency.

This commit is contained in:
David Anson 2024-09-28 15:55:26 -07:00
parent 124b7e2276
commit 97effd921e
6 changed files with 36 additions and 50 deletions

View file

@ -406,7 +406,7 @@ const positionLessThanOrEqual = (lineA, columnA, lineB, columnB) => (
* *
* @param {FileRange|import("../lib/markdownlint.js").MicromarkToken} rangeA Range A. * @param {FileRange|import("../lib/markdownlint.js").MicromarkToken} rangeA Range A.
* @param {FileRange|import("../lib/markdownlint.js").MicromarkToken} rangeB Range B. * @param {FileRange|import("../lib/markdownlint.js").MicromarkToken} rangeB Range B.
* @returns {boolean} Whether the two ranges overlap. * @returns {boolean} True iff the two ranges overlap.
*/ */
const hasOverlap = (rangeA, rangeB) => { const hasOverlap = (rangeA, rangeB) => {
const lte = positionLessThanOrEqual(rangeA.startLine, rangeA.startColumn, rangeB.startLine, rangeB.startColumn); const lte = positionLessThanOrEqual(rangeA.startLine, rangeA.startColumn, rangeB.startLine, rangeB.startColumn);
@ -476,7 +476,7 @@ function getReferenceLinkImageData(tokens) {
duplicateDefinitions.push([ reference, token.startLine - 1 ]); duplicateDefinitions.push([ reference, token.startLine - 1 ]);
} else { } else {
const parent = const parent =
micromark.getTokenParentOfType(token, [ "definition" ]); micromark.getParentOfType(token, [ "definition" ]);
const destinationString = parent && const destinationString = parent &&
micromark.getDescendantsByType(parent, [ "definitionDestination", "definitionDestinationRaw", "definitionDestinationString" ])[0]?.text; micromark.getDescendantsByType(parent, [ "definitionDestination", "definitionDestinationRaw", "definitionDestinationString" ])[0]?.text;
definitions.set( definitions.set(
@ -867,16 +867,12 @@ module.exports = {
// @ts-ignore const { directive, gfmAutolinkLiteral, gfmFootnote, gfmTable, math, parse, postprocess, preprocess } =
const { __webpack_require__(/*! markdownlint-micromark */ "markdownlint-micromark");
directive, gfmAutolinkLiteral, gfmFootnote, gfmTable, math,
parse, postprocess, preprocess
// @ts-ignore
} = __webpack_require__(/*! markdownlint-micromark */ "markdownlint-micromark");
const { newLineRe } = __webpack_require__(/*! ./shared.js */ "../helpers/shared.js"); const { newLineRe } = __webpack_require__(/*! ./shared.js */ "../helpers/shared.js");
const flatTokensSymbol = Symbol("flat-tokens"); const flatTokensSymbol = Symbol("flat-tokens");
const reparseSymbol = Symbol("reparse"); const htmlFlowSymbol = Symbol("html-flow");
/** @typedef {import("markdownlint-micromark").Event} Event */ /** @typedef {import("markdownlint-micromark").Event} Event */
/** @typedef {import("markdownlint-micromark").ParseOptions} ParseOptions */ /** @typedef {import("markdownlint-micromark").ParseOptions} ParseOptions */
@ -884,13 +880,13 @@ const reparseSymbol = Symbol("reparse");
/** @typedef {import("../lib/markdownlint.js").MicromarkToken} Token */ /** @typedef {import("../lib/markdownlint.js").MicromarkToken} Token */
/** /**
* Determines if a Micromark token is within an htmlFlow. * Determines if a Micromark token is within an htmlFlow type.
* *
* @param {Token} token Micromark token. * @param {Token} token Micromark token.
* @returns {boolean} True iff the token is within an htmlFlow. * @returns {boolean} True iff the token is within an htmlFlow type.
*/ */
function inHtmlFlow(token) { function inHtmlFlow(token) {
return token[reparseSymbol]; return Boolean(token[htmlFlowSymbol]);
} }
/** /**
@ -1022,7 +1018,7 @@ function micromarkParseWithOffset(
"parent": ((previous === root) ? (ancestor || null) : previous) "parent": ((previous === root) ? (ancestor || null) : previous)
}; };
if (ancestor) { if (ancestor) {
Object.defineProperty(current, reparseSymbol, { "value": true }); Object.defineProperty(current, htmlFlowSymbol, { "value": true });
} }
previous.children.push(current); previous.children.push(current);
flatTokens.push(current); flatTokens.push(current);
@ -1248,10 +1244,7 @@ function getHeadingStyle(heading) {
* @returns {string} Heading text. * @returns {string} Heading text.
*/ */
function getHeadingText(heading) { function getHeadingText(heading) {
const headingTexts = filterByTypes( const headingTexts = getDescendantsByType(heading, [ [ "atxHeadingText", "setextHeadingText" ] ]);
heading.children,
[ "atxHeadingText", "setextHeadingText" ]
);
return headingTexts[0]?.text.replace(/[\r\n]+/g, " ") || ""; return headingTexts[0]?.text.replace(/[\r\n]+/g, " ") || "";
} }
@ -1292,7 +1285,7 @@ function getHtmlTagInfo(token) {
* @param {TokenType[]} types Types to allow. * @param {TokenType[]} types Types to allow.
* @returns {Token | null} Parent token. * @returns {Token | null} Parent token.
*/ */
function getTokenParentOfType(token, types) { function getParentOfType(token, types) {
/** @type {Token | null} */ /** @type {Token | null} */
let current = token; let current = token;
while ((current = current.parent) && !types.includes(current.type)) { while ((current = current.parent) && !types.includes(current.type)) {
@ -1326,8 +1319,8 @@ module.exports = {
getHeadingStyle, getHeadingStyle,
getHeadingText, getHeadingText,
getHtmlTagInfo, getHtmlTagInfo,
getParentOfType,
getMicromarkEvents, getMicromarkEvents,
getTokenParentOfType,
inHtmlFlow, inHtmlFlow,
isHtmlFlowComment, isHtmlFlowComment,
nonContentTokens nonContentTokens
@ -3231,7 +3224,7 @@ module.exports = {
const { addErrorDetailIf } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"); const { addErrorDetailIf } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { getDescendantsByType, getTokenParentOfType } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs"); const { getDescendantsByType, getParentOfType } = __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 markerToStyle = { const markerToStyle = {
@ -3274,7 +3267,7 @@ module.exports = {
/** @type {import("../helpers/micromark.cjs").Token | null} */ /** @type {import("../helpers/micromark.cjs").Token | null} */
let parent = listUnordered; let parent = listUnordered;
// @ts-ignore // @ts-ignore
while ((parent = getTokenParentOfType(parent, [ "listOrdered", "listUnordered" ]))) { while ((parent = getParentOfType(parent, [ "listOrdered", "listUnordered" ]))) {
nesting++; nesting++;
} }
} }
@ -3411,7 +3404,7 @@ module.exports = {
const { addErrorDetailIf } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"); const { addErrorDetailIf } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { getTokenParentOfType } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs"); const { getParentOfType } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
const { filterByTypesCached } = __webpack_require__(/*! ./cache */ "../lib/cache.js"); const { filterByTypesCached } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
// eslint-disable-next-line jsdoc/valid-types // eslint-disable-next-line jsdoc/valid-types
@ -3447,7 +3440,7 @@ module.exports = {
let current = token; let current = token;
while ( while (
// @ts-ignore // @ts-ignore
(current = getTokenParentOfType(current, unorderedParentTypes)) (current = getParentOfType(current, unorderedParentTypes))
) { ) {
if (current.type === "listUnordered") { if (current.type === "listUnordered") {
nesting++; nesting++;
@ -4840,7 +4833,7 @@ module.exports = {
const { addErrorContext, isBlankLine } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"); const { addErrorContext, isBlankLine } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { getTokenParentOfType } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs"); const { getParentOfType } = __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 codeFencePrefixRe = /^(.*?)[`~]/; const codeFencePrefixRe = /^(.*?)[`~]/;
@ -4889,7 +4882,7 @@ module.exports = {
const includeListItems = (listItems === undefined) ? true : !!listItems; const includeListItems = (listItems === undefined) ? true : !!listItems;
const { lines } = params; const { lines } = params;
for (const codeBlock of filterByTypesCached([ "codeFenced" ])) { for (const codeBlock of filterByTypesCached([ "codeFenced" ])) {
if (includeListItems || !(getTokenParentOfType(codeBlock, [ "listOrdered", "listUnordered" ]))) { if (includeListItems || !(getParentOfType(codeBlock, [ "listOrdered", "listUnordered" ]))) {
if (!isBlankLine(lines[codeBlock.startLine - 2])) { if (!isBlankLine(lines[codeBlock.startLine - 2])) {
addError(onError, lines, codeBlock.startLine, true); addError(onError, lines, codeBlock.startLine, true);
} }

View file

@ -394,7 +394,7 @@ const positionLessThanOrEqual = (lineA, columnA, lineB, columnB) => (
* *
* @param {FileRange|import("../lib/markdownlint.js").MicromarkToken} rangeA Range A. * @param {FileRange|import("../lib/markdownlint.js").MicromarkToken} rangeA Range A.
* @param {FileRange|import("../lib/markdownlint.js").MicromarkToken} rangeB Range B. * @param {FileRange|import("../lib/markdownlint.js").MicromarkToken} rangeB Range B.
* @returns {boolean} Whether the two ranges overlap. * @returns {boolean} True iff the two ranges overlap.
*/ */
const hasOverlap = (rangeA, rangeB) => { const hasOverlap = (rangeA, rangeB) => {
const lte = positionLessThanOrEqual(rangeA.startLine, rangeA.startColumn, rangeB.startLine, rangeB.startColumn); const lte = positionLessThanOrEqual(rangeA.startLine, rangeA.startColumn, rangeB.startLine, rangeB.startColumn);
@ -464,7 +464,7 @@ function getReferenceLinkImageData(tokens) {
duplicateDefinitions.push([ reference, token.startLine - 1 ]); duplicateDefinitions.push([ reference, token.startLine - 1 ]);
} else { } else {
const parent = const parent =
micromark.getTokenParentOfType(token, [ "definition" ]); micromark.getParentOfType(token, [ "definition" ]);
const destinationString = parent && const destinationString = parent &&
micromark.getDescendantsByType(parent, [ "definitionDestination", "definitionDestinationRaw", "definitionDestinationString" ])[0]?.text; micromark.getDescendantsByType(parent, [ "definitionDestination", "definitionDestinationRaw", "definitionDestinationString" ])[0]?.text;
definitions.set( definitions.set(

View file

@ -2,16 +2,12 @@
"use strict"; "use strict";
// @ts-ignore const { directive, gfmAutolinkLiteral, gfmFootnote, gfmTable, math, parse, postprocess, preprocess } =
const { require("markdownlint-micromark");
directive, gfmAutolinkLiteral, gfmFootnote, gfmTable, math,
parse, postprocess, preprocess
// @ts-ignore
} = require("markdownlint-micromark");
const { newLineRe } = require("./shared.js"); const { newLineRe } = require("./shared.js");
const flatTokensSymbol = Symbol("flat-tokens"); const flatTokensSymbol = Symbol("flat-tokens");
const reparseSymbol = Symbol("reparse"); const htmlFlowSymbol = Symbol("html-flow");
/** @typedef {import("markdownlint-micromark").Event} Event */ /** @typedef {import("markdownlint-micromark").Event} Event */
/** @typedef {import("markdownlint-micromark").ParseOptions} ParseOptions */ /** @typedef {import("markdownlint-micromark").ParseOptions} ParseOptions */
@ -19,13 +15,13 @@ const reparseSymbol = Symbol("reparse");
/** @typedef {import("../lib/markdownlint.js").MicromarkToken} Token */ /** @typedef {import("../lib/markdownlint.js").MicromarkToken} Token */
/** /**
* Determines if a Micromark token is within an htmlFlow. * Determines if a Micromark token is within an htmlFlow type.
* *
* @param {Token} token Micromark token. * @param {Token} token Micromark token.
* @returns {boolean} True iff the token is within an htmlFlow. * @returns {boolean} True iff the token is within an htmlFlow type.
*/ */
function inHtmlFlow(token) { function inHtmlFlow(token) {
return token[reparseSymbol]; return Boolean(token[htmlFlowSymbol]);
} }
/** /**
@ -157,7 +153,7 @@ function micromarkParseWithOffset(
"parent": ((previous === root) ? (ancestor || null) : previous) "parent": ((previous === root) ? (ancestor || null) : previous)
}; };
if (ancestor) { if (ancestor) {
Object.defineProperty(current, reparseSymbol, { "value": true }); Object.defineProperty(current, htmlFlowSymbol, { "value": true });
} }
previous.children.push(current); previous.children.push(current);
flatTokens.push(current); flatTokens.push(current);
@ -383,10 +379,7 @@ function getHeadingStyle(heading) {
* @returns {string} Heading text. * @returns {string} Heading text.
*/ */
function getHeadingText(heading) { function getHeadingText(heading) {
const headingTexts = filterByTypes( const headingTexts = getDescendantsByType(heading, [ [ "atxHeadingText", "setextHeadingText" ] ]);
heading.children,
[ "atxHeadingText", "setextHeadingText" ]
);
return headingTexts[0]?.text.replace(/[\r\n]+/g, " ") || ""; return headingTexts[0]?.text.replace(/[\r\n]+/g, " ") || "";
} }
@ -427,7 +420,7 @@ function getHtmlTagInfo(token) {
* @param {TokenType[]} types Types to allow. * @param {TokenType[]} types Types to allow.
* @returns {Token | null} Parent token. * @returns {Token | null} Parent token.
*/ */
function getTokenParentOfType(token, types) { function getParentOfType(token, types) {
/** @type {Token | null} */ /** @type {Token | null} */
let current = token; let current = token;
while ((current = current.parent) && !types.includes(current.type)) { while ((current = current.parent) && !types.includes(current.type)) {
@ -461,8 +454,8 @@ module.exports = {
getHeadingStyle, getHeadingStyle,
getHeadingText, getHeadingText,
getHtmlTagInfo, getHtmlTagInfo,
getParentOfType,
getMicromarkEvents, getMicromarkEvents,
getTokenParentOfType,
inHtmlFlow, inHtmlFlow,
isHtmlFlowComment, isHtmlFlowComment,
nonContentTokens nonContentTokens

View file

@ -3,7 +3,7 @@
"use strict"; "use strict";
const { addErrorDetailIf } = require("../helpers"); const { addErrorDetailIf } = require("../helpers");
const { getDescendantsByType, getTokenParentOfType } = require("../helpers/micromark.cjs"); const { getDescendantsByType, getParentOfType } = require("../helpers/micromark.cjs");
const { filterByTypesCached } = require("./cache"); const { filterByTypesCached } = require("./cache");
const markerToStyle = { const markerToStyle = {
@ -46,7 +46,7 @@ module.exports = {
/** @type {import("../helpers/micromark.cjs").Token | null} */ /** @type {import("../helpers/micromark.cjs").Token | null} */
let parent = listUnordered; let parent = listUnordered;
// @ts-ignore // @ts-ignore
while ((parent = getTokenParentOfType(parent, [ "listOrdered", "listUnordered" ]))) { while ((parent = getParentOfType(parent, [ "listOrdered", "listUnordered" ]))) {
nesting++; nesting++;
} }
} }

View file

@ -3,7 +3,7 @@
"use strict"; "use strict";
const { addErrorDetailIf } = require("../helpers"); const { addErrorDetailIf } = require("../helpers");
const { getTokenParentOfType } = require("../helpers/micromark.cjs"); const { getParentOfType } = require("../helpers/micromark.cjs");
const { filterByTypesCached } = require("./cache"); const { filterByTypesCached } = require("./cache");
// eslint-disable-next-line jsdoc/valid-types // eslint-disable-next-line jsdoc/valid-types
@ -39,7 +39,7 @@ module.exports = {
let current = token; let current = token;
while ( while (
// @ts-ignore // @ts-ignore
(current = getTokenParentOfType(current, unorderedParentTypes)) (current = getParentOfType(current, unorderedParentTypes))
) { ) {
if (current.type === "listUnordered") { if (current.type === "listUnordered") {
nesting++; nesting++;

View file

@ -3,7 +3,7 @@
"use strict"; "use strict";
const { addErrorContext, isBlankLine } = require("../helpers"); const { addErrorContext, isBlankLine } = require("../helpers");
const { getTokenParentOfType } = require("../helpers/micromark.cjs"); const { getParentOfType } = require("../helpers/micromark.cjs");
const { filterByTypesCached } = require("./cache"); const { filterByTypesCached } = require("./cache");
const codeFencePrefixRe = /^(.*?)[`~]/; const codeFencePrefixRe = /^(.*?)[`~]/;
@ -52,7 +52,7 @@ module.exports = {
const includeListItems = (listItems === undefined) ? true : !!listItems; const includeListItems = (listItems === undefined) ? true : !!listItems;
const { lines } = params; const { lines } = params;
for (const codeBlock of filterByTypesCached([ "codeFenced" ])) { for (const codeBlock of filterByTypesCached([ "codeFenced" ])) {
if (includeListItems || !(getTokenParentOfType(codeBlock, [ "listOrdered", "listUnordered" ]))) { if (includeListItems || !(getParentOfType(codeBlock, [ "listOrdered", "listUnordered" ]))) {
if (!isBlankLine(lines[codeBlock.startLine - 2])) { if (!isBlankLine(lines[codeBlock.startLine - 2])) {
addError(onError, lines, codeBlock.startLine, true); addError(onError, lines, codeBlock.startLine, true);
} }