From 108af2192b4a0f72dca76f18d725aded0c975536 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Fri, 10 Feb 2023 18:31:03 +0100 Subject: [PATCH] Fix types for TS 4.9 --- dev/lib/directive-container.js | 26 ++++++++++++--- dev/lib/directive-leaf.js | 16 +++++++-- dev/lib/directive-text.js | 21 +++++++++--- dev/lib/html.js | 60 +++++++++++++++++++++++++++------- test/index.js | 16 +++++++-- 5 files changed, 112 insertions(+), 27 deletions(-) diff --git a/dev/lib/directive-container.js b/dev/lib/directive-container.js index 32cc53a..7e04f90 100644 --- a/dev/lib/directive-container.js +++ b/dev/lib/directive-container.js @@ -3,6 +3,7 @@ * @typedef {import('micromark-util-types').Tokenizer} Tokenizer * @typedef {import('micromark-util-types').State} State * @typedef {import('micromark-util-types').Token} Token + * @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext */ import {ok as assert} from 'uvu/assert' @@ -25,7 +26,10 @@ const label = {tokenize: tokenizeLabel, partial: true} const attributes = {tokenize: tokenizeAttributes, partial: true} const nonLazyLine = {tokenize: tokenizeNonLazyLine, partial: true} -/** @type {Tokenizer} */ +/** + * @this {TokenizeContext} + * @type {Tokenizer} + */ function tokenizeDirectiveContainer(effects, ok, nok) { const self = this const tail = self.events[self.events.length - 1] @@ -193,7 +197,10 @@ function tokenizeDirectiveContainer(effects, ok, nok) { return ok(code) } - /** @type {Tokenizer} */ + /** + * @this {TokenizeContext} + * @type {Tokenizer} + */ function tokenizeClosingFence(effects, ok, nok) { let size = 0 @@ -236,7 +243,10 @@ function tokenizeDirectiveContainer(effects, ok, nok) { } } -/** @type {Tokenizer} */ +/** + * @this {TokenizeContext} + * @type {Tokenizer} + */ function tokenizeLabel(effects, ok, nok) { // Always a `[` return factoryLabel( @@ -250,7 +260,10 @@ function tokenizeLabel(effects, ok, nok) { ) } -/** @type {Tokenizer} */ +/** + * @this {TokenizeContext} + * @type {Tokenizer} + */ function tokenizeAttributes(effects, ok, nok) { // Always a `{` return factoryAttributes( @@ -272,7 +285,10 @@ function tokenizeAttributes(effects, ok, nok) { ) } -/** @type {Tokenizer} */ +/** + * @this {TokenizeContext} + * @type {Tokenizer} + */ function tokenizeNonLazyLine(effects, ok, nok) { const self = this diff --git a/dev/lib/directive-leaf.js b/dev/lib/directive-leaf.js index 3a1b2a8..34f3c87 100644 --- a/dev/lib/directive-leaf.js +++ b/dev/lib/directive-leaf.js @@ -1,5 +1,6 @@ /** * @typedef {import('micromark-util-types').Construct} Construct + * @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext * @typedef {import('micromark-util-types').Tokenizer} Tokenizer * @typedef {import('micromark-util-types').State} State */ @@ -19,7 +20,10 @@ export const directiveLeaf = {tokenize: tokenizeDirectiveLeaf} const label = {tokenize: tokenizeLabel, partial: true} const attributes = {tokenize: tokenizeAttributes, partial: true} -/** @type {Tokenizer} */ +/** + * @this {TokenizeContext} + * @type {Tokenizer} + */ function tokenizeDirectiveLeaf(effects, ok, nok) { const self = this @@ -81,7 +85,10 @@ function tokenizeDirectiveLeaf(effects, ok, nok) { } } -/** @type {Tokenizer} */ +/** + * @this {TokenizeContext} + * @type {Tokenizer} + */ function tokenizeLabel(effects, ok, nok) { // Always a `[` return factoryLabel( @@ -95,7 +102,10 @@ function tokenizeLabel(effects, ok, nok) { ) } -/** @type {Tokenizer} */ +/** + * @this {TokenizeContext} + * @type {Tokenizer} + */ function tokenizeAttributes(effects, ok, nok) { // Always a `{` return factoryAttributes( diff --git a/dev/lib/directive-text.js b/dev/lib/directive-text.js index 1c04c55..0889acf 100644 --- a/dev/lib/directive-text.js +++ b/dev/lib/directive-text.js @@ -1,5 +1,6 @@ /** * @typedef {import('micromark-util-types').Construct} Construct + * @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext * @typedef {import('micromark-util-types').Tokenizer} Tokenizer * @typedef {import('micromark-util-types').Previous} Previous * @typedef {import('micromark-util-types').State} State @@ -21,7 +22,10 @@ export const directiveText = { const label = {tokenize: tokenizeLabel, partial: true} const attributes = {tokenize: tokenizeAttributes, partial: true} -/** @type {Previous} */ +/** + * @this {TokenizeContext} + * @type {Previous} + */ function previous(code) { // If there is a previous code, there will always be a tail. return ( @@ -30,7 +34,10 @@ function previous(code) { ) } -/** @type {Tokenizer} */ +/** + * @this {TokenizeContext} + * @type {Tokenizer} + */ function tokenizeDirectiveText(effects, ok, nok) { const self = this @@ -70,7 +77,10 @@ function tokenizeDirectiveText(effects, ok, nok) { } } -/** @type {Tokenizer} */ +/** + * @this {TokenizeContext} + * @type {Tokenizer} + */ function tokenizeLabel(effects, ok, nok) { // Always a `[` return factoryLabel( @@ -83,7 +93,10 @@ function tokenizeLabel(effects, ok, nok) { ) } -/** @type {Tokenizer} */ +/** + * @this {TokenizeContext} + * @type {Tokenizer} + */ function tokenizeAttributes(effects, ok, nok) { // Always a `{` return factoryAttributes( diff --git a/dev/lib/html.js b/dev/lib/html.js index 656a450..cf8e71d 100644 --- a/dev/lib/html.js +++ b/dev/lib/html.js @@ -98,7 +98,10 @@ export function directiveHtml(options = {}) { stack.push({type, name: ''}) } - /** @type {_Handle} */ + /** + * @this {CompileContext} + * @type {_Handle} + */ function exitName(token) { /** @type {Directive[]} */ // @ts-expect-error @@ -106,12 +109,18 @@ export function directiveHtml(options = {}) { stack[stack.length - 1].name = this.sliceSerialize(token) } - /** @type {_Handle} */ + /** + * @this {CompileContext} + * @type {_Handle} + */ function enterLabel() { this.buffer() } - /** @type {_Handle} */ + /** + * @this {CompileContext} + * @type {_Handle} + */ function exitLabel() { const data = this.resume() /** @type {Directive[]} */ @@ -120,13 +129,19 @@ export function directiveHtml(options = {}) { stack[stack.length - 1].label = data } - /** @type {_Handle} */ + /** + * @this {CompileContext} + * @type {_Handle} + */ function enterAttributes() { this.buffer() this.setData('directiveAttributes', []) } - /** @type {_Handle} */ + /** + * @this {CompileContext} + * @type {_Handle} + */ function exitAttributeIdValue(token) { /** @type {Attribute[]} */ // @ts-expect-error @@ -139,7 +154,10 @@ export function directiveHtml(options = {}) { ]) } - /** @type {_Handle} */ + /** + * @this {CompileContext} + * @type {_Handle} + */ function exitAttributeClassValue(token) { /** @type {Attribute[]} */ // @ts-expect-error @@ -153,7 +171,10 @@ export function directiveHtml(options = {}) { ]) } - /** @type {_Handle} */ + /** + * @this {CompileContext} + * @type {_Handle} + */ function exitAttributeName(token) { // Attribute names in CommonMark are significantly limited, so character // references can’t exist. @@ -164,7 +185,10 @@ export function directiveHtml(options = {}) { attributes.push([this.sliceSerialize(token), '']) } - /** @type {_Handle} */ + /** + * @this {CompileContext} + * @type {_Handle} + */ function exitAttributeValue(token) { /** @type {Attribute[]} */ // @ts-expect-error @@ -175,7 +199,10 @@ export function directiveHtml(options = {}) { ) } - /** @type {_Handle} */ + /** + * @this {CompileContext} + * @type {_Handle} + */ function exitAttributes() { /** @type {Directive[]} */ // @ts-expect-error @@ -204,7 +231,10 @@ export function directiveHtml(options = {}) { stack[stack.length - 1].attributes = cleaned } - /** @type {_Handle} */ + /** + * @this {CompileContext} + * @type {_Handle} + */ function exitContainerContent() { const data = this.resume() /** @type {Directive[]} */ @@ -213,7 +243,10 @@ export function directiveHtml(options = {}) { stack[stack.length - 1].content = data } - /** @type {_Handle} */ + /** + * @this {CompileContext} + * @type {_Handle} + */ function exitContainerFence() { /** @type {Directive[]} */ // @ts-expect-error @@ -224,7 +257,10 @@ export function directiveHtml(options = {}) { if (directive._fenceCount === 1) this.setData('slurpOneLineEnding', true) } - /** @type {_Handle} */ + /** + * @this {CompileContext} + * @type {_Handle} + */ function exit() { /** @type {Directive} */ // @ts-expect-error diff --git a/test/index.js b/test/index.js index 86ff9f1..dcfa5f5 100644 --- a/test/index.js +++ b/test/index.js @@ -1,4 +1,5 @@ /** + * @typedef {import('micromark-util-types').CompileContext} CompileContext * @typedef {import('../dev/index.js').HtmlOptions} HtmlOptions * @typedef {import('../dev/index.js').Handle} Handle */ @@ -1476,7 +1477,10 @@ test('content', (t) => { t.end() }) -/** @type {Handle} */ +/** + * @this {CompileContext} + * @type {Handle} + */ function abbr(d) { if (d.type !== 'textDirective') return false @@ -1491,7 +1495,10 @@ function abbr(d) { this.tag('') } -/** @type {Handle} */ +/** + * @this {CompileContext} + * @type {Handle} + */ function youtube(d) { const attrs = d.attributes || {} const v = attrs.v @@ -1526,7 +1533,10 @@ function youtube(d) { this.tag('') } -/** @type {Handle} */ +/** + * @this {CompileContext} + * @type {Handle} + */ function h(d) { const content = d.content || d.label const attrs = d.attributes || {}