Refactor types

This commit is contained in:
Titus Wormer 2023-03-30 14:56:52 +02:00
parent 174ebb977a
commit 34c7cf81fe
No known key found for this signature in database
GPG key ID: E6E581152ED04E2E
5 changed files with 27 additions and 23 deletions

View file

@ -2,8 +2,8 @@
* @typedef {import('micromark-util-types').Construct} Construct * @typedef {import('micromark-util-types').Construct} Construct
* @typedef {import('micromark-util-types').State} State * @typedef {import('micromark-util-types').State} State
* @typedef {import('micromark-util-types').Token} Token * @typedef {import('micromark-util-types').Token} Token
* @typedef {import('micromark-util-types').Tokenizer} Tokenizer
* @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext * @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext
* @typedef {import('micromark-util-types').Tokenizer} Tokenizer
*/ */
import {ok as assert} from 'uvu/assert' import {ok as assert} from 'uvu/assert'

View file

@ -2,8 +2,8 @@
* @typedef {import('micromark-util-types').Construct} Construct * @typedef {import('micromark-util-types').Construct} Construct
* @typedef {import('micromark-util-types').Previous} Previous * @typedef {import('micromark-util-types').Previous} Previous
* @typedef {import('micromark-util-types').State} State * @typedef {import('micromark-util-types').State} State
* @typedef {import('micromark-util-types').Tokenizer} Tokenizer
* @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext * @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext
* @typedef {import('micromark-util-types').Tokenizer} Tokenizer
*/ */
import {ok as assert} from 'uvu/assert' import {ok as assert} from 'uvu/assert'

View file

@ -1,7 +1,7 @@
/** /**
* @typedef {import('micromark-util-types').Code} Code
* @typedef {import('micromark-util-types').Effects} Effects * @typedef {import('micromark-util-types').Effects} Effects
* @typedef {import('micromark-util-types').State} State * @typedef {import('micromark-util-types').State} State
* @typedef {import('micromark-util-types').Code} Code
*/ */
import {ok as assert} from 'uvu/assert' import {ok as assert} from 'uvu/assert'

View file

@ -1,7 +1,7 @@
/** /**
* @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext
* @typedef {import('micromark-util-types').Effects} Effects * @typedef {import('micromark-util-types').Effects} Effects
* @typedef {import('micromark-util-types').State} State * @typedef {import('micromark-util-types').State} State
* @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext
*/ */
import {asciiAlpha, asciiAlphanumeric} from 'micromark-util-character' import {asciiAlpha, asciiAlphanumeric} from 'micromark-util-character'

View file

@ -1,7 +1,7 @@
/** /**
* @typedef {import('micromark-util-types').HtmlExtension} HtmlExtension
* @typedef {import('micromark-util-types').Handle} _Handle
* @typedef {import('micromark-util-types').CompileContext} CompileContext * @typedef {import('micromark-util-types').CompileContext} CompileContext
* @typedef {import('micromark-util-types').Handle} _Handle
* @typedef {import('micromark-util-types').HtmlExtension} HtmlExtension
*/ */
/** /**
@ -11,12 +11,15 @@
* @typedef Directive * @typedef Directive
* @property {DirectiveType} type * @property {DirectiveType} type
* @property {string} name * @property {string} name
* @property {string} [label] * @property {string | undefined} [label]
* @property {Record<string, string>} [attributes] * @property {Record<string, string> | undefined} [attributes]
* @property {string} [content] * @property {string | undefined} [content]
* @property {number} [_fenceCount] * @property {number | undefined} [_fenceCount]
* *
* @typedef {(this: CompileContext, directive: Directive) => boolean|void} Handle * @callback Handle
* @param {CompileContext} this
* @param {Directive} directive
* @returns {boolean | void}
* *
* @typedef {Record<string, Handle>} HtmlOptions * @typedef {Record<string, Handle>} HtmlOptions
*/ */
@ -27,10 +30,11 @@ import {parseEntities} from 'parse-entities'
const own = {}.hasOwnProperty const own = {}.hasOwnProperty
/** /**
* @param {HtmlOptions} [options] * @param {HtmlOptions | null | undefined} [options]
* @returns {HtmlExtension} * @returns {HtmlExtension}
*/ */
export function directiveHtml(options = {}) { export function directiveHtml(options) {
const options_ = options || {}
return { return {
enter: { enter: {
directiveContainer() { directiveContainer() {
@ -143,7 +147,7 @@ export function directiveHtml(options = {}) {
* @type {_Handle} * @type {_Handle}
*/ */
function exitAttributeIdValue(token) { function exitAttributeIdValue(token) {
/** @type {Attribute[]} */ /** @type {Array<Attribute>} */
// @ts-expect-error // @ts-expect-error
const attributes = this.getData('directiveAttributes') const attributes = this.getData('directiveAttributes')
attributes.push([ attributes.push([
@ -159,7 +163,7 @@ export function directiveHtml(options = {}) {
* @type {_Handle} * @type {_Handle}
*/ */
function exitAttributeClassValue(token) { function exitAttributeClassValue(token) {
/** @type {Attribute[]} */ /** @type {Array<Attribute>} */
// @ts-expect-error // @ts-expect-error
const attributes = this.getData('directiveAttributes') const attributes = this.getData('directiveAttributes')
@ -178,7 +182,7 @@ export function directiveHtml(options = {}) {
function exitAttributeName(token) { function exitAttributeName(token) {
// Attribute names in CommonMark are significantly limited, so character // Attribute names in CommonMark are significantly limited, so character
// references cant exist. // references cant exist.
/** @type {Attribute[]} */ /** @type {Array<Attribute>} */
// @ts-expect-error // @ts-expect-error
const attributes = this.getData('directiveAttributes') const attributes = this.getData('directiveAttributes')
@ -190,7 +194,7 @@ export function directiveHtml(options = {}) {
* @type {_Handle} * @type {_Handle}
*/ */
function exitAttributeValue(token) { function exitAttributeValue(token) {
/** @type {Attribute[]} */ /** @type {Array<Attribute>} */
// @ts-expect-error // @ts-expect-error
const attributes = this.getData('directiveAttributes') const attributes = this.getData('directiveAttributes')
attributes[attributes.length - 1][1] = parseEntities( attributes[attributes.length - 1][1] = parseEntities(
@ -207,7 +211,7 @@ export function directiveHtml(options = {}) {
/** @type {Directive[]} */ /** @type {Directive[]} */
// @ts-expect-error // @ts-expect-error
const stack = this.getData('directiveStack') const stack = this.getData('directiveStack')
/** @type {Attribute[]} */ /** @type {Array<Attribute>} */
// @ts-expect-error // @ts-expect-error
const attributes = this.getData('directiveAttributes') const attributes = this.getData('directiveAttributes')
/** @type {Directive['attributes']} */ /** @type {Directive['attributes']} */
@ -272,13 +276,13 @@ export function directiveHtml(options = {}) {
assert(directive.name, 'expected `name`') assert(directive.name, 'expected `name`')
if (own.call(options, directive.name)) { if (own.call(options_, directive.name)) {
result = options[directive.name].call(this, directive) result = options_[directive.name].call(this, directive)
found = result !== false found = result !== false
} }
if (!found && own.call(options, '*')) { if (!found && own.call(options_, '*')) {
result = options['*'].call(this, directive) result = options_['*'].call(this, directive)
found = result !== false found = result !== false
} }