Improve type safety by strongly-typing the micromark token type property everywhere.

This commit is contained in:
David Anson 2024-02-29 23:05:27 -08:00
parent 828ae3541a
commit 28e1b23955
7 changed files with 106 additions and 50 deletions

View file

@ -6,12 +6,12 @@ const { addErrorDetailIf } = require("../helpers");
const { filterByTypes, getTokenParentOfType, inHtmlFlow } =
require("../helpers/micromark.cjs");
/**
* @typedef {import("../helpers/micromark.cjs").Token} Token
*/
// eslint-disable-next-line jsdoc/valid-types
/** @type import("micromark-util-types").TokenType[] */
const unorderedListTypes =
[ "blockQuotePrefix", "listItemPrefix", "listUnordered" ];
// eslint-disable-next-line jsdoc/valid-types
/** @type import("micromark-util-types").TokenType[] */
const unorderedParentTypes =
[ "blockQuote", "listOrdered", "listUnordered" ];
@ -39,7 +39,7 @@ module.exports = {
lastBlockQuotePrefix = token;
} else if (type === "listUnordered") {
let nesting = 0;
/** @type {Token | null} */
/** @type {import("../helpers/micromark.cjs").Token | null} */
let current = token;
while (
(current = getTokenParentOfType(current, unorderedParentTypes))

View file

@ -7,19 +7,32 @@ const { filterByPredicate, tokenIfType } = require("../helpers/micromark.cjs");
const intrawordRe = /^\w$/;
/**
* @param {import("./markdownlint").RuleParams} params Rule parameters.
* @param {import("./markdownlint").RuleOnError} onError Error-reporting callback.
* @param {import("micromark-util-types").TokenType} type Token type.
* @param {import("micromark-util-types").TokenType} typeSequence Token sequence type.
* @param {"*" | "**"} asterisk Asterisk kind.
* @param {"_" | "__"} underline Underline kind.
* @param {"asterisk" | "consistent" | "underscore"} style Style string.
*/
const impl =
(params, onError, type, asterisk, underline, style = "consistent") => {
const { lines, parsers } = params;
(params, onError, type, typeSequence, asterisk, underline, style = "consistent") => {
// eslint-disable-next-line jsdoc/valid-types
/** @type import("../helpers/micromark.cjs").Token[] */
const micromarkTokens =
// @ts-ignore
params.parsers.micromark.tokens;
const { lines } = params;
const emphasisTokens = filterByPredicate(
parsers.micromark.tokens,
micromarkTokens,
(token) => token.type === type,
(token) => ((token.type === "htmlFlow") ? [] : token.children)
);
for (const token of emphasisTokens) {
const { children } = token;
const childType = `${type}Sequence`;
const startSequence = tokenIfType(children[0], childType);
const endSequence = tokenIfType(children[children.length - 1], childType);
const startSequence = tokenIfType(children[0], typeSequence);
const endSequence = tokenIfType(children[children.length - 1], typeSequence);
if (startSequence && endSequence) {
const markupStyle = emphasisOrStrongStyleFor(startSequence.text);
if (style === "consistent") {
@ -55,6 +68,8 @@ const impl =
}
};
// eslint-disable-next-line jsdoc/valid-types
/** @type import("./markdownlint").Rule[] */
module.exports = [
{
"names": [ "MD049", "emphasis-style" ],
@ -65,6 +80,7 @@ module.exports = [
params,
onError,
"emphasis",
"emphasisSequence",
"*",
"_",
params.config.style || undefined
@ -80,6 +96,7 @@ module.exports = [
params,
onError,
"strong",
"strongSequence",
"**",
"__",
params.config.style || undefined

View file

@ -19,15 +19,11 @@ const tokensInclude = new Set(
[ "characterEscapeValue", "codeTextData", "data" ]
);
/**
* @typedef {import("../helpers/micromark.cjs").Token} Token
*/
/**
* Converts a Markdown heading into an HTML fragment according to the rules
* used by GitHub.
*
* @param {Token} headingText Heading text token.
* @param {import("../helpers/micromark.cjs").Token} headingText Heading text token.
* @returns {string} Fragment string for heading.
*/
function convertHeadingToHTMLFragment(headingText) {
@ -56,7 +52,7 @@ function convertHeadingToHTMLFragment(headingText) {
/**
* Unescapes the text of a String-type micromark Token.
*
* @param {Token} token String-type micromark Token.
* @param {import("../helpers/micromark.cjs").Token} token String-type micromark Token.
* @returns {string} Unescaped token text.
*/
function unescapeStringTokenText(token) {
@ -112,6 +108,8 @@ module.exports = {
}
// Process link and definition fragments
// eslint-disable-next-line jsdoc/valid-types
/** @type import("../helpers/micromark.cjs").TokenType[][] */
const parentChilds = [
[ "link", "resourceDestinationString" ],
[ "definition", "definitionDestinationString" ]