Update types for changes in micromark-util-types

This commit is contained in:
Titus Wormer 2023-05-30 16:32:08 +02:00
parent 5facf31692
commit 2294f65b11
No known key found for this signature in database
GPG key ID: E6E581152ED04E2E
8 changed files with 130 additions and 56 deletions

1
.gitignore vendored
View file

@ -6,3 +6,4 @@ node_modules/
/lib/
/index.js
yarn.lock
!dev/index.d.ts

82
dev/index.d.ts vendored Normal file
View file

@ -0,0 +1,82 @@
import type {Attribute, Directive} from './lib/html.js'
export {directive} from './lib/syntax.js'
export {
directiveHtml,
type Directive,
type Handle,
type HtmlOptions
} from './lib/html.js'
declare module 'micromark-util-types' {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface TokenTypeMap {
directiveContainer: 'directiveContainer'
directiveContainerAttributes: 'directiveContainerAttributes'
directiveContainerAttributesMarker: 'directiveContainerAttributesMarker'
directiveContainerAttribute: 'directiveContainerAttribute'
directiveContainerAttributeId: 'directiveContainerAttributeId'
directiveContainerAttributeIdValue: 'directiveContainerAttributeIdValue'
directiveContainerAttributeClass: 'directiveContainerAttributeClass'
directiveContainerAttributeClassValue: 'directiveContainerAttributeClassValue'
directiveContainerAttributeName: 'directiveContainerAttributeName'
directiveContainerAttributeInitializerMarker: 'directiveContainerAttributeInitializerMarker'
directiveContainerAttributeValueLiteral: 'directiveContainerAttributeValueLiteral'
directiveContainerAttributeValue: 'directiveContainerAttributeValue'
directiveContainerAttributeValueMarker: 'directiveContainerAttributeValueMarker'
directiveContainerAttributeValueData: 'directiveContainerAttributeValueData'
directiveContainerContent: 'directiveContainerContent'
directiveContainerFence: 'directiveContainerFence'
directiveContainerLabel: 'directiveContainerLabel'
directiveContainerLabelMarker: 'directiveContainerLabelMarker'
directiveContainerLabelString: 'directiveContainerLabelString'
directiveContainerName: 'directiveContainerName'
directiveContainerSequence: 'directiveContainerSequence'
directiveLeaf: 'directiveLeaf'
directiveLeafAttributes: 'directiveLeafAttributes'
directiveLeafAttributesMarker: 'directiveLeafAttributesMarker'
directiveLeafAttribute: 'directiveLeafAttribute'
directiveLeafAttributeId: 'directiveLeafAttributeId'
directiveLeafAttributeIdValue: 'directiveLeafAttributeIdValue'
directiveLeafAttributeClass: 'directiveLeafAttributeClass'
directiveLeafAttributeClassValue: 'directiveLeafAttributeClassValue'
directiveLeafAttributeName: 'directiveLeafAttributeName'
directiveLeafAttributeInitializerMarker: 'directiveLeafAttributeInitializerMarker'
directiveLeafAttributeValueLiteral: 'directiveLeafAttributeValueLiteral'
directiveLeafAttributeValue: 'directiveLeafAttributeValue'
directiveLeafAttributeValueMarker: 'directiveLeafAttributeValueMarker'
directiveLeafAttributeValueData: 'directiveLeafAttributeValueData'
directiveLeafLabel: 'directiveLeafLabel'
directiveLeafLabelMarker: 'directiveLeafLabelMarker'
directiveLeafLabelString: 'directiveLeafLabelString'
directiveLeafName: 'directiveLeafName'
directiveLeafSequence: 'directiveLeafSequence'
directiveText: 'directiveText'
directiveTextAttributes: 'directiveTextAttributes'
directiveTextAttributesMarker: 'directiveTextAttributesMarker'
directiveTextAttribute: 'directiveTextAttribute'
directiveTextAttributeId: 'directiveTextAttributeId'
directiveTextAttributeIdValue: 'directiveTextAttributeIdValue'
directiveTextAttributeClass: 'directiveTextAttributeClass'
directiveTextAttributeClassValue: 'directiveTextAttributeClassValue'
directiveTextAttributeName: 'directiveTextAttributeName'
directiveTextAttributeInitializerMarker: 'directiveTextAttributeInitializerMarker'
directiveTextAttributeValueLiteral: 'directiveTextAttributeValueLiteral'
directiveTextAttributeValue: 'directiveTextAttributeValue'
directiveTextAttributeValueMarker: 'directiveTextAttributeValueMarker'
directiveTextAttributeValueData: 'directiveTextAttributeValueData'
directiveTextLabel: 'directiveTextLabel'
directiveTextLabelMarker: 'directiveTextLabelMarker'
directiveTextLabelString: 'directiveTextLabelString'
directiveTextMarker: 'directiveTextMarker'
directiveTextName: 'directiveTextName'
}
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface CompileData {
directiveAttributes?: Attribute[]
directiveStack?: Directive[]
}
}

View file

@ -1,8 +1,3 @@
/**
* @typedef {import('./lib/html.js').Directive} Directive
* @typedef {import('./lib/html.js').Handle} Handle
* @typedef {import('./lib/html.js').HtmlOptions} HtmlOptions
*/
// Note: more types exported from `index.d.ts`.
export {directive} from './lib/syntax.js'
export {directiveHtml} from './lib/html.js'

View file

@ -2,6 +2,7 @@
* @typedef {import('micromark-util-types').Code} Code
* @typedef {import('micromark-util-types').Effects} Effects
* @typedef {import('micromark-util-types').State} State
* @typedef {import('micromark-util-types').TokenType} TokenType
*/
import {ok as assert} from 'uvu/assert'
@ -21,17 +22,17 @@ import {types} from 'micromark-util-symbol/types.js'
* @param {Effects} effects
* @param {State} ok
* @param {State} nok
* @param {string} attributesType
* @param {string} attributesMarkerType
* @param {string} attributeType
* @param {string} attributeIdType
* @param {string} attributeClassType
* @param {string} attributeNameType
* @param {string} attributeInitializerType
* @param {string} attributeValueLiteralType
* @param {string} attributeValueType
* @param {string} attributeValueMarker
* @param {string} attributeValueData
* @param {TokenType} attributesType
* @param {TokenType} attributesMarkerType
* @param {TokenType} attributeType
* @param {TokenType} attributeIdType
* @param {TokenType} attributeClassType
* @param {TokenType} attributeNameType
* @param {TokenType} attributeInitializerType
* @param {TokenType} attributeValueLiteralType
* @param {TokenType} attributeValueType
* @param {TokenType} attributeValueMarker
* @param {TokenType} attributeValueData
* @param {boolean} [disallowEol=false]
*/
/* eslint-disable-next-line max-params */
@ -52,7 +53,7 @@ export function factoryAttributes(
attributeValueData,
disallowEol
) {
/** @type {string} */
/** @type {TokenType} */
let type
/** @type {Code | undefined} */
let marker
@ -101,11 +102,13 @@ export function factoryAttributes(
/** @type {State} */
function shortcutStart(code) {
// Assume its registered.
const markerType = /** @type {TokenType} */ (type + 'Marker')
effects.enter(attributeType)
effects.enter(type)
effects.enter(type + 'Marker')
effects.enter(markerType)
effects.consume(code)
effects.exit(type + 'Marker')
effects.exit(markerType)
return shortcutStartAfter
}
@ -127,7 +130,9 @@ export function factoryAttributes(
return nok(code)
}
effects.enter(type + 'Value')
// Assume its registered.
const valueType = /** @type {TokenType} */ (type + 'Value')
effects.enter(valueType)
effects.consume(code)
return shortcut
}
@ -152,7 +157,9 @@ export function factoryAttributes(
code === codes.rightCurlyBrace ||
markdownLineEndingOrSpace(code)
) {
effects.exit(type + 'Value')
// Assume its registered.
const valueType = /** @type {TokenType} */ (type + 'Value')
effects.exit(valueType)
effects.exit(type)
effects.exit(attributeType)
return between(code)

View file

@ -2,6 +2,7 @@
* @typedef {import('micromark-util-types').Effects} Effects
* @typedef {import('micromark-util-types').State} State
* @typedef {import('micromark-util-types').Token} Token
* @typedef {import('micromark-util-types').TokenType} TokenType
*/
import {ok as assert} from 'uvu/assert'
@ -19,9 +20,9 @@ import {types} from 'micromark-util-symbol/types.js'
* @param {Effects} effects
* @param {State} ok
* @param {State} nok
* @param {string} type
* @param {string} markerType
* @param {string} stringType
* @param {TokenType} type
* @param {TokenType} markerType
* @param {TokenType} stringType
* @param {boolean} [disallowEol=false]
*/
// eslint-disable-next-line max-params

View file

@ -2,6 +2,7 @@
* @typedef {import('micromark-util-types').Effects} Effects
* @typedef {import('micromark-util-types').State} State
* @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext
* @typedef {import('micromark-util-types').TokenType} TokenType
*/
import {asciiAlpha, asciiAlphanumeric} from 'micromark-util-character'
@ -12,7 +13,7 @@ import {codes} from 'micromark-util-symbol/codes.js'
* @param {Effects} effects
* @param {State} ok
* @param {State} nok
* @param {string} type
* @param {TokenType} type
*/
export function factoryName(effects, ok, nok, type) {
const self = this

View file

@ -122,8 +122,6 @@ export function directiveHtml(options) {
* @param {DirectiveType} type
*/
function enter(type) {
/** @type {Directive[]} */
// @ts-expect-error
let stack = this.getData('directiveStack')
if (!stack) this.setData('directiveStack', (stack = []))
stack.push({type, name: ''})
@ -134,9 +132,8 @@ export function directiveHtml(options) {
* @type {_Handle}
*/
function exitName(token) {
/** @type {Directive[]} */
// @ts-expect-error
const stack = this.getData('directiveStack')
assert(stack, 'expected directive stack')
stack[stack.length - 1].name = this.sliceSerialize(token)
}
@ -154,9 +151,8 @@ export function directiveHtml(options) {
*/
function exitLabel() {
const data = this.resume()
/** @type {Directive[]} */
// @ts-expect-error
const stack = this.getData('directiveStack')
assert(stack, 'expected directive stack')
stack[stack.length - 1].label = data
}
@ -174,9 +170,8 @@ export function directiveHtml(options) {
* @type {_Handle}
*/
function exitAttributeIdValue(token) {
/** @type {Array<Attribute>} */
// @ts-expect-error
const attributes = this.getData('directiveAttributes')
assert(attributes, 'expected attributes')
attributes.push([
'id',
parseEntities(this.sliceSerialize(token), {
@ -190,9 +185,8 @@ export function directiveHtml(options) {
* @type {_Handle}
*/
function exitAttributeClassValue(token) {
/** @type {Array<Attribute>} */
// @ts-expect-error
const attributes = this.getData('directiveAttributes')
assert(attributes, 'expected attributes')
attributes.push([
'class',
@ -209,9 +203,8 @@ export function directiveHtml(options) {
function exitAttributeName(token) {
// Attribute names in CommonMark are significantly limited, so character
// references cant exist.
/** @type {Array<Attribute>} */
// @ts-expect-error
const attributes = this.getData('directiveAttributes')
assert(attributes, 'expected attributes')
attributes.push([this.sliceSerialize(token), ''])
}
@ -221,9 +214,8 @@ export function directiveHtml(options) {
* @type {_Handle}
*/
function exitAttributeValue(token) {
/** @type {Array<Attribute>} */
// @ts-expect-error
const attributes = this.getData('directiveAttributes')
assert(attributes, 'expected attributes')
attributes[attributes.length - 1][1] = parseEntities(
this.sliceSerialize(token),
{attribute: true}
@ -235,20 +227,16 @@ export function directiveHtml(options) {
* @type {_Handle}
*/
function exitAttributes() {
/** @type {Directive[]} */
// @ts-expect-error
const stack = this.getData('directiveStack')
/** @type {Array<Attribute>} */
// @ts-expect-error
assert(stack, 'expected directive stack')
const attributes = this.getData('directiveAttributes')
assert(attributes, 'expected attributes')
/** @type {Directive['attributes']} */
const cleaned = {}
/** @type {Attribute} */
let attribute
let index = -1
while (++index < attributes.length) {
attribute = attributes[index]
const attribute = attributes[index]
if (attribute[0] === 'class' && cleaned.class) {
cleaned.class += ' ' + attribute[1]
@ -268,9 +256,8 @@ export function directiveHtml(options) {
*/
function exitContainerContent() {
const data = this.resume()
/** @type {Directive[]} */
// @ts-expect-error
const stack = this.getData('directiveStack')
assert(stack, 'expected directive stack')
stack[stack.length - 1].content = data
}
@ -279,9 +266,8 @@ export function directiveHtml(options) {
* @type {_Handle}
*/
function exitContainerFence() {
/** @type {Directive[]} */
// @ts-expect-error
const stack = this.getData('directiveStack')
assert(stack, 'expected directive stack')
const directive = stack[stack.length - 1]
if (!directive._fenceCount) directive._fenceCount = 0
directive._fenceCount++
@ -293,9 +279,10 @@ export function directiveHtml(options) {
* @type {_Handle}
*/
function exit() {
/** @type {Directive} */
// @ts-expect-error
const directive = this.getData('directiveStack').pop()
const stack = this.getData('directiveStack')
assert(stack, 'expected directive stack')
const directive = stack.pop()
assert(directive, 'expected directive')
/** @type {boolean|undefined} */
let found
/** @type {boolean|void} */

View file

@ -1,5 +1,5 @@
{
"include": ["**/*.js"],
"include": ["**/*.js", "dev/index.d.ts"],
"exclude": ["coverage/", "lib/", "node_modules/", "index.js"],
"compilerOptions": {
"customConditions": ["development"],