Skip to content

Commit 423a7ac

Browse files
committed
Refactor to improve types
1 parent fb8f87b commit 423a7ac

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

dev/lib/index.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
* @typedef {import('micromark-util-types').Token} Token
66
* @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext
77
* @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
109
* @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
1114
* @typedef {import('mdast').Break} Break
1215
* @typedef {import('mdast').Blockquote} Blockquote
1316
* @typedef {import('mdast').Code} Code
@@ -27,9 +30,8 @@
2730
* @typedef {import('mdast').Strong} Strong
2831
* @typedef {import('mdast').Text} Text
2932
* @typedef {import('mdast').ThematicBreak} ThematicBreak
30-
* @typedef {import('mdast').PhrasingContent} PhrasingContent
3133
*
32-
* @typedef {Parent & {type: 'fragment', children: PhrasingContent[]}} Fragment
34+
* @typedef {UnistParent & {type: 'fragment', children: PhrasingContent[]}} Fragment
3335
*/
3436

3537
/**
@@ -54,7 +56,7 @@
5456
*
5557
* @typedef CompileContext
5658
* mdast compiler context
57-
* @property {Array.<Node>} stack
59+
* @property {Array.<Node | Fragment>} stack
5860
* @property {Array.<Token>} tokenStack
5961
* @property {(key: string, value?: unknown) => void} setData
6062
* Set data into the key-value store.
@@ -528,7 +530,6 @@ function compiler(options = {}) {
528530

529531
/** @type {CompileContext['buffer']} */
530532
function buffer() {
531-
// @ts-expect-error: Custom node type to collect text.
532533
this.stack.push({type: 'fragment', children: []})
533534
}
534535

@@ -575,6 +576,7 @@ function compiler(options = {}) {
575576
function exit(token) {
576577
const node = this.stack.pop()
577578
assert(node, 'expected `node`')
579+
assert(node.type !== 'fragment', 'unexpected fragment `exit`ed')
578580
const open = this.tokenStack.pop()
579581

580582
if (!open) {
@@ -702,8 +704,19 @@ function compiler(options = {}) {
702704
function onexitatxheadingsequence(token) {
703705
const node = /** @type {Heading} */ (this.stack[this.stack.length - 1])
704706
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
707720
}
708721
}
709722

@@ -736,7 +749,7 @@ function compiler(options = {}) {
736749
tail = text()
737750
// @ts-expect-error: we’ll add `end` later.
738751
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`.
740753
parent.children.push(tail)
741754
}
742755

test/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ test('commonmark', (t) => {
10151015
const example = commonmark[index]
10161016
const root = fromMarkdown(example.markdown.slice(0, -1))
10171017
const hast = toHast(root, {allowDangerousHtml: true})
1018-
// @ts-expect-error: `toHtml` too narrow / `toHast` to loose.
1018+
assert(hast && hast.type === 'root', 'expected `root`')
10191019
const html = toHtml(hast, {
10201020
allowDangerousHtml: true,
10211021
entities: {useNamedReferences: true},

0 commit comments

Comments
 (0)