|
5 | 5 | * @typedef {import('micromark-util-types').Token} Token |
6 | 6 | * @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext |
7 | 7 | * @typedef {import('micromark-util-types').Value} Value |
8 | | - * @typedef {Root|Root['children'][number]} Node |
9 | | - * @typedef {Extract<Node, import('unist').Parent>} Parent |
| 8 | + * @typedef {import('unist').Parent} UnistParent |
10 | 9 | * @typedef {import('unist').Point} Point |
| 10 | + * @typedef {import('mdast').PhrasingContent} PhrasingContent |
| 11 | + * @typedef {import('mdast').Content} Content |
| 12 | + * @typedef {Root|Content} Node |
| 13 | + * @typedef {Extract<Node, UnistParent>} Parent |
11 | 14 | * @typedef {import('mdast').Break} Break |
12 | 15 | * @typedef {import('mdast').Blockquote} Blockquote |
13 | 16 | * @typedef {import('mdast').Code} Code |
|
27 | 30 | * @typedef {import('mdast').Strong} Strong |
28 | 31 | * @typedef {import('mdast').Text} Text |
29 | 32 | * @typedef {import('mdast').ThematicBreak} ThematicBreak |
30 | | - * @typedef {import('mdast').PhrasingContent} PhrasingContent |
31 | 33 | * |
32 | | - * @typedef {Parent & {type: 'fragment', children: PhrasingContent[]}} Fragment |
| 34 | + * @typedef {UnistParent & {type: 'fragment', children: PhrasingContent[]}} Fragment |
33 | 35 | */ |
34 | 36 |
|
35 | 37 | /** |
|
54 | 56 | * |
55 | 57 | * @typedef CompileContext |
56 | 58 | * mdast compiler context |
57 | | - * @property {Array.<Node>} stack |
| 59 | + * @property {Array.<Node | Fragment>} stack |
58 | 60 | * @property {Array.<Token>} tokenStack |
59 | 61 | * @property {(key: string, value?: unknown) => void} setData |
60 | 62 | * Set data into the key-value store. |
@@ -528,7 +530,6 @@ function compiler(options = {}) { |
528 | 530 |
|
529 | 531 | /** @type {CompileContext['buffer']} */ |
530 | 532 | function buffer() { |
531 | | - // @ts-expect-error: Custom node type to collect text. |
532 | 533 | this.stack.push({type: 'fragment', children: []}) |
533 | 534 | } |
534 | 535 |
|
@@ -575,6 +576,7 @@ function compiler(options = {}) { |
575 | 576 | function exit(token) { |
576 | 577 | const node = this.stack.pop() |
577 | 578 | assert(node, 'expected `node`') |
| 579 | + assert(node.type !== 'fragment', 'unexpected fragment `exit`ed') |
578 | 580 | const open = this.tokenStack.pop() |
579 | 581 |
|
580 | 582 | if (!open) { |
@@ -702,8 +704,19 @@ function compiler(options = {}) { |
702 | 704 | function onexitatxheadingsequence(token) { |
703 | 705 | const node = /** @type {Heading} */ (this.stack[this.stack.length - 1]) |
704 | 706 | if (!node.depth) { |
705 | | - // @ts-expect-error: assume valid depth. |
706 | | - node.depth = this.sliceSerialize(token).length |
| 707 | + const depth = this.sliceSerialize(token).length |
| 708 | + |
| 709 | + assert( |
| 710 | + depth === 1 || |
| 711 | + depth === 2 || |
| 712 | + depth === 3 || |
| 713 | + depth === 4 || |
| 714 | + depth === 5 || |
| 715 | + depth === 6, |
| 716 | + 'expected `depth` between `1` and `6`' |
| 717 | + ) |
| 718 | + |
| 719 | + node.depth = depth |
707 | 720 | } |
708 | 721 | } |
709 | 722 |
|
@@ -736,7 +749,7 @@ function compiler(options = {}) { |
736 | 749 | tail = text() |
737 | 750 | // @ts-expect-error: we’ll add `end` later. |
738 | 751 | tail.position = {start: point(token.start)} |
739 | | - // @ts-expect-error: Assume `text` can be added to `parent`. |
| 752 | + // @ts-expect-error: Assume `parent` accepts `text`. |
740 | 753 | parent.children.push(tail) |
741 | 754 | } |
742 | 755 |
|
|
0 commit comments