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

@ -277,7 +277,7 @@ module.exports.fencedCodeBlockStyleFor =
* Return the string representation of a emphasis or strong markup character.
*
* @param {string} markup Emphasis or strong string.
* @returns {string} String representation.
* @returns {"asterisk" | "underscore"} String representation.
*/
module.exports.emphasisOrStrongStyleFor =
function emphasisOrStrongStyleFor(markup) {
@ -316,7 +316,7 @@ module.exports.headingStyleFor = function headingStyleFor(token) {
* Return the string representation of an unordered list marker.
*
* @param {Object} token MarkdownItToken instance.
* @returns {string} String representation.
* @returns {"asterisk" | "dash" | "plus"} String representation.
*/
module.exports.unorderedListStyleFor = function unorderedListStyleFor(token) {
switch (token.markup) {

View file

@ -12,11 +12,21 @@ const { newLineRe } = require("./shared.js");
const flatTokensSymbol = Symbol("flat-tokens");
// Reference all micromark extensions from markdownlint-micromark so types like TokenType get merged
/** @type {import("micromark-extension-directive")} */
/** @type {import("micromark-extension-gfm-autolink-literal")} */
/** @type {import("micromark-extension-gfm-footnote")} */
/** @type {import("micromark-extension-gfm-table")} */
/** @type {import("micromark-extension-math")} */
/**
* @typedef {import("micromark-util-types").TokenType} TokenType
*/
/**
* Markdown token.
*
* @typedef {Object} Token
* @property {string} type Token type.
* @property {TokenType} type Token type.
* @property {number} startLine Start line (1-based).
* @property {number} startColumn Start column (1-based).
* @property {number} endLine End line (1-based).
@ -119,7 +129,7 @@ function micromarkParseWithOffset(
let flatTokens = [];
/** @type {Token} */
const root = {
"type": "ROOT",
"type": "data",
"startLine": -1,
"startColumn": -1,
"endLine": -1,
@ -283,7 +293,7 @@ function filterByPredicate(tokens, allowed, transformChildren) {
* Filter a list of Micromark tokens by type.
*
* @param {Token[]} tokens Micromark tokens.
* @param {string[]} types Types to allow.
* @param {TokenType[]} types Types to allow.
* @returns {Token[]} Filtered tokens.
*/
function filterByTypes(tokens, types) {
@ -342,7 +352,7 @@ function getHtmlTagInfo(token) {
* Gets the nearest parent of the specified type for a Micromark token.
*
* @param {Token} token Micromark token.
* @param {string[]} types Types to allow.
* @param {TokenType[]} types Types to allow.
* @returns {Token | null} Parent token.
*/
function getTokenParentOfType(token, types) {
@ -358,7 +368,7 @@ function getTokenParentOfType(token, types) {
* Get the text of the first match from a list of Micromark tokens by type.
*
* @param {Token[]} tokens Micromark tokens.
* @param {string} type Type to match.
* @param {TokenType} type Type to match.
* @returns {string | null} Text of token.
*/
function getTokenTextByType(tokens, type) {
@ -405,7 +415,7 @@ function matchAndGetTokensByType(tokens, matchTypes, resultTypes) {
* Returns the specified token iff it is of the desired type.
*
* @param {Token} token Micromark token candidate.
* @param {string} type Desired type.
* @param {TokenType} type Desired type.
* @returns {Token | null} Token instance.
*/
function tokenIfType(token, type) {