Update types for changes in micromark-util-types
This commit is contained in:
parent
5facf31692
commit
2294f65b11
8 changed files with 130 additions and 56 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -6,3 +6,4 @@ node_modules/
|
||||||
/lib/
|
/lib/
|
||||||
/index.js
|
/index.js
|
||||||
yarn.lock
|
yarn.lock
|
||||||
|
!dev/index.d.ts
|
||||||
|
|
82
dev/index.d.ts
vendored
Normal file
82
dev/index.d.ts
vendored
Normal 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[]
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,8 +1,3 @@
|
||||||
/**
|
// Note: more types exported from `index.d.ts`.
|
||||||
* @typedef {import('./lib/html.js').Directive} Directive
|
|
||||||
* @typedef {import('./lib/html.js').Handle} Handle
|
|
||||||
* @typedef {import('./lib/html.js').HtmlOptions} HtmlOptions
|
|
||||||
*/
|
|
||||||
|
|
||||||
export {directive} from './lib/syntax.js'
|
export {directive} from './lib/syntax.js'
|
||||||
export {directiveHtml} from './lib/html.js'
|
export {directiveHtml} from './lib/html.js'
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
* @typedef {import('micromark-util-types').Code} Code
|
* @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').TokenType} TokenType
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {ok as assert} from 'uvu/assert'
|
import {ok as assert} from 'uvu/assert'
|
||||||
|
@ -21,17 +22,17 @@ import {types} from 'micromark-util-symbol/types.js'
|
||||||
* @param {Effects} effects
|
* @param {Effects} effects
|
||||||
* @param {State} ok
|
* @param {State} ok
|
||||||
* @param {State} nok
|
* @param {State} nok
|
||||||
* @param {string} attributesType
|
* @param {TokenType} attributesType
|
||||||
* @param {string} attributesMarkerType
|
* @param {TokenType} attributesMarkerType
|
||||||
* @param {string} attributeType
|
* @param {TokenType} attributeType
|
||||||
* @param {string} attributeIdType
|
* @param {TokenType} attributeIdType
|
||||||
* @param {string} attributeClassType
|
* @param {TokenType} attributeClassType
|
||||||
* @param {string} attributeNameType
|
* @param {TokenType} attributeNameType
|
||||||
* @param {string} attributeInitializerType
|
* @param {TokenType} attributeInitializerType
|
||||||
* @param {string} attributeValueLiteralType
|
* @param {TokenType} attributeValueLiteralType
|
||||||
* @param {string} attributeValueType
|
* @param {TokenType} attributeValueType
|
||||||
* @param {string} attributeValueMarker
|
* @param {TokenType} attributeValueMarker
|
||||||
* @param {string} attributeValueData
|
* @param {TokenType} attributeValueData
|
||||||
* @param {boolean} [disallowEol=false]
|
* @param {boolean} [disallowEol=false]
|
||||||
*/
|
*/
|
||||||
/* eslint-disable-next-line max-params */
|
/* eslint-disable-next-line max-params */
|
||||||
|
@ -52,9 +53,9 @@ export function factoryAttributes(
|
||||||
attributeValueData,
|
attributeValueData,
|
||||||
disallowEol
|
disallowEol
|
||||||
) {
|
) {
|
||||||
/** @type {string} */
|
/** @type {TokenType} */
|
||||||
let type
|
let type
|
||||||
/** @type {Code|undefined} */
|
/** @type {Code | undefined} */
|
||||||
let marker
|
let marker
|
||||||
|
|
||||||
return start
|
return start
|
||||||
|
@ -101,11 +102,13 @@ export function factoryAttributes(
|
||||||
|
|
||||||
/** @type {State} */
|
/** @type {State} */
|
||||||
function shortcutStart(code) {
|
function shortcutStart(code) {
|
||||||
|
// Assume it’s registered.
|
||||||
|
const markerType = /** @type {TokenType} */ (type + 'Marker')
|
||||||
effects.enter(attributeType)
|
effects.enter(attributeType)
|
||||||
effects.enter(type)
|
effects.enter(type)
|
||||||
effects.enter(type + 'Marker')
|
effects.enter(markerType)
|
||||||
effects.consume(code)
|
effects.consume(code)
|
||||||
effects.exit(type + 'Marker')
|
effects.exit(markerType)
|
||||||
return shortcutStartAfter
|
return shortcutStartAfter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,7 +130,9 @@ export function factoryAttributes(
|
||||||
return nok(code)
|
return nok(code)
|
||||||
}
|
}
|
||||||
|
|
||||||
effects.enter(type + 'Value')
|
// Assume it’s registered.
|
||||||
|
const valueType = /** @type {TokenType} */ (type + 'Value')
|
||||||
|
effects.enter(valueType)
|
||||||
effects.consume(code)
|
effects.consume(code)
|
||||||
return shortcut
|
return shortcut
|
||||||
}
|
}
|
||||||
|
@ -152,7 +157,9 @@ export function factoryAttributes(
|
||||||
code === codes.rightCurlyBrace ||
|
code === codes.rightCurlyBrace ||
|
||||||
markdownLineEndingOrSpace(code)
|
markdownLineEndingOrSpace(code)
|
||||||
) {
|
) {
|
||||||
effects.exit(type + 'Value')
|
// Assume it’s registered.
|
||||||
|
const valueType = /** @type {TokenType} */ (type + 'Value')
|
||||||
|
effects.exit(valueType)
|
||||||
effects.exit(type)
|
effects.exit(type)
|
||||||
effects.exit(attributeType)
|
effects.exit(attributeType)
|
||||||
return between(code)
|
return between(code)
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
* @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').Token} Token
|
* @typedef {import('micromark-util-types').Token} Token
|
||||||
|
* @typedef {import('micromark-util-types').TokenType} TokenType
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {ok as assert} from 'uvu/assert'
|
import {ok as assert} from 'uvu/assert'
|
||||||
|
@ -19,9 +20,9 @@ import {types} from 'micromark-util-symbol/types.js'
|
||||||
* @param {Effects} effects
|
* @param {Effects} effects
|
||||||
* @param {State} ok
|
* @param {State} ok
|
||||||
* @param {State} nok
|
* @param {State} nok
|
||||||
* @param {string} type
|
* @param {TokenType} type
|
||||||
* @param {string} markerType
|
* @param {TokenType} markerType
|
||||||
* @param {string} stringType
|
* @param {TokenType} stringType
|
||||||
* @param {boolean} [disallowEol=false]
|
* @param {boolean} [disallowEol=false]
|
||||||
*/
|
*/
|
||||||
// eslint-disable-next-line max-params
|
// eslint-disable-next-line max-params
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
* @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
|
* @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext
|
||||||
|
* @typedef {import('micromark-util-types').TokenType} TokenType
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {asciiAlpha, asciiAlphanumeric} from 'micromark-util-character'
|
import {asciiAlpha, asciiAlphanumeric} from 'micromark-util-character'
|
||||||
|
@ -12,7 +13,7 @@ import {codes} from 'micromark-util-symbol/codes.js'
|
||||||
* @param {Effects} effects
|
* @param {Effects} effects
|
||||||
* @param {State} ok
|
* @param {State} ok
|
||||||
* @param {State} nok
|
* @param {State} nok
|
||||||
* @param {string} type
|
* @param {TokenType} type
|
||||||
*/
|
*/
|
||||||
export function factoryName(effects, ok, nok, type) {
|
export function factoryName(effects, ok, nok, type) {
|
||||||
const self = this
|
const self = this
|
||||||
|
|
|
@ -122,8 +122,6 @@ export function directiveHtml(options) {
|
||||||
* @param {DirectiveType} type
|
* @param {DirectiveType} type
|
||||||
*/
|
*/
|
||||||
function enter(type) {
|
function enter(type) {
|
||||||
/** @type {Directive[]} */
|
|
||||||
// @ts-expect-error
|
|
||||||
let stack = this.getData('directiveStack')
|
let stack = this.getData('directiveStack')
|
||||||
if (!stack) this.setData('directiveStack', (stack = []))
|
if (!stack) this.setData('directiveStack', (stack = []))
|
||||||
stack.push({type, name: ''})
|
stack.push({type, name: ''})
|
||||||
|
@ -134,9 +132,8 @@ export function directiveHtml(options) {
|
||||||
* @type {_Handle}
|
* @type {_Handle}
|
||||||
*/
|
*/
|
||||||
function exitName(token) {
|
function exitName(token) {
|
||||||
/** @type {Directive[]} */
|
|
||||||
// @ts-expect-error
|
|
||||||
const stack = this.getData('directiveStack')
|
const stack = this.getData('directiveStack')
|
||||||
|
assert(stack, 'expected directive stack')
|
||||||
stack[stack.length - 1].name = this.sliceSerialize(token)
|
stack[stack.length - 1].name = this.sliceSerialize(token)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -154,9 +151,8 @@ export function directiveHtml(options) {
|
||||||
*/
|
*/
|
||||||
function exitLabel() {
|
function exitLabel() {
|
||||||
const data = this.resume()
|
const data = this.resume()
|
||||||
/** @type {Directive[]} */
|
|
||||||
// @ts-expect-error
|
|
||||||
const stack = this.getData('directiveStack')
|
const stack = this.getData('directiveStack')
|
||||||
|
assert(stack, 'expected directive stack')
|
||||||
stack[stack.length - 1].label = data
|
stack[stack.length - 1].label = data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -174,9 +170,8 @@ export function directiveHtml(options) {
|
||||||
* @type {_Handle}
|
* @type {_Handle}
|
||||||
*/
|
*/
|
||||||
function exitAttributeIdValue(token) {
|
function exitAttributeIdValue(token) {
|
||||||
/** @type {Array<Attribute>} */
|
|
||||||
// @ts-expect-error
|
|
||||||
const attributes = this.getData('directiveAttributes')
|
const attributes = this.getData('directiveAttributes')
|
||||||
|
assert(attributes, 'expected attributes')
|
||||||
attributes.push([
|
attributes.push([
|
||||||
'id',
|
'id',
|
||||||
parseEntities(this.sliceSerialize(token), {
|
parseEntities(this.sliceSerialize(token), {
|
||||||
|
@ -190,9 +185,8 @@ export function directiveHtml(options) {
|
||||||
* @type {_Handle}
|
* @type {_Handle}
|
||||||
*/
|
*/
|
||||||
function exitAttributeClassValue(token) {
|
function exitAttributeClassValue(token) {
|
||||||
/** @type {Array<Attribute>} */
|
|
||||||
// @ts-expect-error
|
|
||||||
const attributes = this.getData('directiveAttributes')
|
const attributes = this.getData('directiveAttributes')
|
||||||
|
assert(attributes, 'expected attributes')
|
||||||
|
|
||||||
attributes.push([
|
attributes.push([
|
||||||
'class',
|
'class',
|
||||||
|
@ -209,9 +203,8 @@ 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 can’t exist.
|
// references can’t exist.
|
||||||
/** @type {Array<Attribute>} */
|
|
||||||
// @ts-expect-error
|
|
||||||
const attributes = this.getData('directiveAttributes')
|
const attributes = this.getData('directiveAttributes')
|
||||||
|
assert(attributes, 'expected attributes')
|
||||||
|
|
||||||
attributes.push([this.sliceSerialize(token), ''])
|
attributes.push([this.sliceSerialize(token), ''])
|
||||||
}
|
}
|
||||||
|
@ -221,9 +214,8 @@ export function directiveHtml(options) {
|
||||||
* @type {_Handle}
|
* @type {_Handle}
|
||||||
*/
|
*/
|
||||||
function exitAttributeValue(token) {
|
function exitAttributeValue(token) {
|
||||||
/** @type {Array<Attribute>} */
|
|
||||||
// @ts-expect-error
|
|
||||||
const attributes = this.getData('directiveAttributes')
|
const attributes = this.getData('directiveAttributes')
|
||||||
|
assert(attributes, 'expected attributes')
|
||||||
attributes[attributes.length - 1][1] = parseEntities(
|
attributes[attributes.length - 1][1] = parseEntities(
|
||||||
this.sliceSerialize(token),
|
this.sliceSerialize(token),
|
||||||
{attribute: true}
|
{attribute: true}
|
||||||
|
@ -235,20 +227,16 @@ export function directiveHtml(options) {
|
||||||
* @type {_Handle}
|
* @type {_Handle}
|
||||||
*/
|
*/
|
||||||
function exitAttributes() {
|
function exitAttributes() {
|
||||||
/** @type {Directive[]} */
|
|
||||||
// @ts-expect-error
|
|
||||||
const stack = this.getData('directiveStack')
|
const stack = this.getData('directiveStack')
|
||||||
/** @type {Array<Attribute>} */
|
assert(stack, 'expected directive stack')
|
||||||
// @ts-expect-error
|
|
||||||
const attributes = this.getData('directiveAttributes')
|
const attributes = this.getData('directiveAttributes')
|
||||||
|
assert(attributes, 'expected attributes')
|
||||||
/** @type {Directive['attributes']} */
|
/** @type {Directive['attributes']} */
|
||||||
const cleaned = {}
|
const cleaned = {}
|
||||||
/** @type {Attribute} */
|
|
||||||
let attribute
|
|
||||||
let index = -1
|
let index = -1
|
||||||
|
|
||||||
while (++index < attributes.length) {
|
while (++index < attributes.length) {
|
||||||
attribute = attributes[index]
|
const attribute = attributes[index]
|
||||||
|
|
||||||
if (attribute[0] === 'class' && cleaned.class) {
|
if (attribute[0] === 'class' && cleaned.class) {
|
||||||
cleaned.class += ' ' + attribute[1]
|
cleaned.class += ' ' + attribute[1]
|
||||||
|
@ -268,9 +256,8 @@ export function directiveHtml(options) {
|
||||||
*/
|
*/
|
||||||
function exitContainerContent() {
|
function exitContainerContent() {
|
||||||
const data = this.resume()
|
const data = this.resume()
|
||||||
/** @type {Directive[]} */
|
|
||||||
// @ts-expect-error
|
|
||||||
const stack = this.getData('directiveStack')
|
const stack = this.getData('directiveStack')
|
||||||
|
assert(stack, 'expected directive stack')
|
||||||
stack[stack.length - 1].content = data
|
stack[stack.length - 1].content = data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -279,9 +266,8 @@ export function directiveHtml(options) {
|
||||||
* @type {_Handle}
|
* @type {_Handle}
|
||||||
*/
|
*/
|
||||||
function exitContainerFence() {
|
function exitContainerFence() {
|
||||||
/** @type {Directive[]} */
|
|
||||||
// @ts-expect-error
|
|
||||||
const stack = this.getData('directiveStack')
|
const stack = this.getData('directiveStack')
|
||||||
|
assert(stack, 'expected directive stack')
|
||||||
const directive = stack[stack.length - 1]
|
const directive = stack[stack.length - 1]
|
||||||
if (!directive._fenceCount) directive._fenceCount = 0
|
if (!directive._fenceCount) directive._fenceCount = 0
|
||||||
directive._fenceCount++
|
directive._fenceCount++
|
||||||
|
@ -293,9 +279,10 @@ export function directiveHtml(options) {
|
||||||
* @type {_Handle}
|
* @type {_Handle}
|
||||||
*/
|
*/
|
||||||
function exit() {
|
function exit() {
|
||||||
/** @type {Directive} */
|
const stack = this.getData('directiveStack')
|
||||||
// @ts-expect-error
|
assert(stack, 'expected directive stack')
|
||||||
const directive = this.getData('directiveStack').pop()
|
const directive = stack.pop()
|
||||||
|
assert(directive, 'expected directive')
|
||||||
/** @type {boolean|undefined} */
|
/** @type {boolean|undefined} */
|
||||||
let found
|
let found
|
||||||
/** @type {boolean|void} */
|
/** @type {boolean|void} */
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
{
|
{
|
||||||
"include": ["**/*.js"],
|
"include": ["**/*.js", "dev/index.d.ts"],
|
||||||
"exclude": ["coverage/", "lib/", "node_modules/", "index.js"],
|
"exclude": ["coverage/", "lib/", "node_modules/", "index.js"],
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"customConditions": ["development"],
|
"customConditions": ["development"],
|
||||||
|
|
Loading…
Add table
Reference in a new issue