From 2294f65b11fdfae35661d4e87ce3eb58004d072c Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Tue, 30 May 2023 16:32:08 +0200 Subject: [PATCH] Update types for changes in `micromark-util-types` --- .gitignore | 1 + dev/index.d.ts | 82 +++++++++++++++++++++++++++++++++++ dev/index.js | 7 +-- dev/lib/factory-attributes.js | 41 ++++++++++-------- dev/lib/factory-label.js | 7 +-- dev/lib/factory-name.js | 3 +- dev/lib/html.js | 43 +++++++----------- tsconfig.json | 2 +- 8 files changed, 130 insertions(+), 56 deletions(-) create mode 100644 dev/index.d.ts diff --git a/.gitignore b/.gitignore index b957bb5..a136532 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ node_modules/ /lib/ /index.js yarn.lock +!dev/index.d.ts diff --git a/dev/index.d.ts b/dev/index.d.ts new file mode 100644 index 0000000..3f979f0 --- /dev/null +++ b/dev/index.d.ts @@ -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[] + } +} diff --git a/dev/index.js b/dev/index.js index e540120..f290efe 100644 --- a/dev/index.js +++ b/dev/index.js @@ -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' diff --git a/dev/lib/factory-attributes.js b/dev/lib/factory-attributes.js index c592196..d39cf76 100644 --- a/dev/lib/factory-attributes.js +++ b/dev/lib/factory-attributes.js @@ -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,9 +53,9 @@ export function factoryAttributes( attributeValueData, disallowEol ) { - /** @type {string} */ + /** @type {TokenType} */ let type - /** @type {Code|undefined} */ + /** @type {Code | undefined} */ let marker return start @@ -101,11 +102,13 @@ export function factoryAttributes( /** @type {State} */ function shortcutStart(code) { + // Assume it’s 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 it’s 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 it’s registered. + const valueType = /** @type {TokenType} */ (type + 'Value') + effects.exit(valueType) effects.exit(type) effects.exit(attributeType) return between(code) diff --git a/dev/lib/factory-label.js b/dev/lib/factory-label.js index 7d1d3d2..76030dc 100644 --- a/dev/lib/factory-label.js +++ b/dev/lib/factory-label.js @@ -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 diff --git a/dev/lib/factory-name.js b/dev/lib/factory-name.js index f0f4144..375b2a7 100644 --- a/dev/lib/factory-name.js +++ b/dev/lib/factory-name.js @@ -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 diff --git a/dev/lib/html.js b/dev/lib/html.js index 2f09af5..7086528 100644 --- a/dev/lib/html.js +++ b/dev/lib/html.js @@ -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} */ - // @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} */ - // @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 can’t exist. - /** @type {Array} */ - // @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} */ - // @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} */ - // @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} */ diff --git a/tsconfig.json b/tsconfig.json index 397ca4a..bbc8fb2 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,5 @@ { - "include": ["**/*.js"], + "include": ["**/*.js", "dev/index.d.ts"], "exclude": ["coverage/", "lib/", "node_modules/", "index.js"], "compilerOptions": { "customConditions": ["development"],