Skip to content

Commit 2454165

Browse files
committed
Refactor types
1 parent caf7d6a commit 2454165

File tree

6 files changed

+316
-185
lines changed

6 files changed

+316
-185
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88
coverage/
99
node_modules/
1010
yarn.lock
11+
!/dev/lib/types.d.ts
1112
!/dev/index.d.ts

dev/index.d.ts

+3-57
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,17 @@
11
export type {Encoding, Token, Value} from 'micromark-util-types'
22
export type {
33
CompileContext,
4+
CompileData,
45
Extension,
6+
Handles,
57
Handle,
68
OnEnterError,
79
OnExitError,
810
Options,
911
Transform
10-
} from './lib/index.js'
11-
12+
} from './lib/types.js'
1213
export {fromMarkdown} from './lib/index.js'
1314

14-
/**
15-
* Interface of tracked data.
16-
*
17-
* When working on extensions that use more data, extend the corresponding
18-
* interface to register their types:
19-
*
20-
* ```ts
21-
* declare module 'mdast-util-from-markdown' {
22-
* interface CompileData {
23-
* // Register a new field.
24-
* mathFlowInside?: boolean | undefined
25-
* }
26-
* }
27-
* ```
28-
*/
29-
export interface CompileData {
30-
/**
31-
* Whether we’re inside a hard break.
32-
*/
33-
atHardBreak?: boolean | undefined
34-
35-
/**
36-
* Current character reference type.
37-
*/
38-
characterReferenceType?:
39-
| 'characterReferenceMarkerHexadecimal'
40-
| 'characterReferenceMarkerNumeric'
41-
| undefined
42-
43-
/**
44-
* Whether a first list item value (`1` in `1. a`) is expected.
45-
*/
46-
expectingFirstListItemValue?: boolean | undefined
47-
48-
/**
49-
* Whether we’re in flow code.
50-
*/
51-
flowCodeInside?: boolean | undefined
52-
53-
/**
54-
* Whether we’re in a reference.
55-
*/
56-
inReference?: boolean | undefined
57-
58-
/**
59-
* Whether we’re expecting a line ending from a setext heading, which can be slurped.
60-
*/
61-
setextHeadingSlurpLineEnding?: boolean | undefined
62-
63-
/**
64-
* Current reference.
65-
*/
66-
referenceType?: 'collapsed' | 'full' | undefined
67-
}
68-
6915
declare module 'micromark-util-types' {
7016
interface TokenTypeMap {
7117
listItem: 'listItem'

dev/lib/index.js

+14-127
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
* List,
1515
* Nodes,
1616
* Paragraph,
17-
* Parent,
1817
* PhrasingContent,
1918
* ReferenceType,
2019
* Root,
@@ -25,113 +24,19 @@
2524
* @import {
2625
* Encoding,
2726
* Event,
28-
* ParseOptions,
29-
* TokenizeContext,
3027
* Token,
3128
* Value
3229
* } from 'micromark-util-types'
3330
* @import {Point} from 'unist'
34-
* @import {CompileData} from 'mdast-util-from-markdown'
35-
*/
36-
37-
/**
38-
* @typedef {Omit<Parent, 'children' | 'type'> & {type: 'fragment', children: Array<PhrasingContent>}} Fragment
39-
*/
40-
41-
/**
42-
* @callback Transform
43-
* Extra transform, to change the AST afterwards.
44-
* @param {Root} tree
45-
* Tree to transform.
46-
* @returns {Root | null | undefined | void}
47-
* New tree or nothing (in which case the current tree is used).
48-
*
49-
* @callback Handle
50-
* Handle a token.
51-
* @param {CompileContext} this
52-
* Context.
53-
* @param {Token} token
54-
* Current token.
55-
* @returns {undefined | void}
56-
* Nothing.
57-
*
58-
* @typedef {Record<string, Handle>} Handles
59-
* Token types mapping to handles
60-
*
61-
* @callback OnEnterError
62-
* Handle the case where the `right` token is open, but it is closed (by the
63-
* `left` token) or because we reached the end of the document.
64-
* @param {Omit<CompileContext, 'sliceSerialize'>} this
65-
* Context.
66-
* @param {Token | undefined} left
67-
* Left token.
68-
* @param {Token} right
69-
* Right token.
70-
* @returns {undefined}
71-
* Nothing.
72-
*
73-
* @callback OnExitError
74-
* Handle the case where the `right` token is open but it is closed by
75-
* exiting the `left` token.
76-
* @param {Omit<CompileContext, 'sliceSerialize'>} this
77-
* Context.
78-
* @param {Token} left
79-
* Left token.
80-
* @param {Token} right
81-
* Right token.
82-
* @returns {undefined}
83-
* Nothing.
84-
*
85-
* @typedef {[Token, OnEnterError | undefined]} TokenTuple
86-
* Open token on the stack, with an optional error handler for when
87-
* that token isn’t closed properly.
88-
*/
89-
90-
/**
91-
* @typedef Config
92-
* Configuration.
93-
*
94-
* We have our defaults, but extensions will add more.
95-
* @property {Array<string>} canContainEols
96-
* Token types where line endings are used.
97-
* @property {Handles} enter
98-
* Opening handles.
99-
* @property {Handles} exit
100-
* Closing handles.
101-
* @property {Array<Transform>} transforms
102-
* Tree transforms.
103-
*
104-
* @typedef {Partial<Config>} Extension
105-
* Change how markdown tokens from micromark are turned into mdast.
106-
*
107-
* @typedef CompileContext
108-
* mdast compiler context.
109-
* @property {Array<Fragment | Nodes>} stack
110-
* Stack of nodes.
111-
* @property {Array<TokenTuple>} tokenStack
112-
* Stack of tokens.
113-
* @property {(this: CompileContext) => undefined} buffer
114-
* Capture some of the output data.
115-
* @property {(this: CompileContext) => string} resume
116-
* Stop capturing and access the output data.
117-
* @property {(this: CompileContext, node: Nodes, token: Token, onError?: OnEnterError) => undefined} enter
118-
* Enter a node.
119-
* @property {(this: CompileContext, token: Token, onError?: OnExitError) => undefined} exit
120-
* Exit a node.
121-
* @property {TokenizeContext['sliceSerialize']} sliceSerialize
122-
* Get the string value of a token.
123-
* @property {Config} config
124-
* Configuration.
125-
* @property {CompileData} data
126-
* Info passed around; key/value store.
127-
*
128-
* @typedef FromMarkdownOptions
129-
* Configuration for how to build mdast.
130-
* @property {Array<Extension | Array<Extension>> | null | undefined} [mdastExtensions]
131-
* Extensions for this utility to change how tokens are turned into a tree.
132-
*
133-
* @typedef {ParseOptions & FromMarkdownOptions} Options
134-
* Configuration.
31+
* @import {
32+
* CompileContext,
33+
* CompileData,
34+
* Config,
35+
* Extension,
36+
* Handle,
37+
* OnEnterError,
38+
* Options
39+
* } from './types.js'
13540
*/
13641

13742
import {ok as assert} from 'devlop'
@@ -562,24 +467,14 @@ function compiler(options) {
562467
}
563468

564469
/**
565-
* @this {CompileContext}
566-
* @returns {undefined}
470+
* @type {CompileContext['buffer']}
567471
*/
568472
function buffer() {
569473
this.stack.push({type: 'fragment', children: []})
570474
}
571475

572476
/**
573-
* @this {CompileContext}
574-
* Context.
575-
* @param {Nodes} node
576-
* Node to enter.
577-
* @param {Token} token
578-
* Corresponding token.
579-
* @param {OnEnterError | undefined} [errorHandler]
580-
* Handle the case where this token is open, but it is closed by something else.
581-
* @returns {undefined}
582-
* Nothing.
477+
* @type {CompileContext['enter']}
583478
*/
584479
function enter(node, token, errorHandler) {
585480
const parent = this.stack[this.stack.length - 1]
@@ -589,7 +484,7 @@ function compiler(options) {
589484
const siblings = parent.children
590485
siblings.push(node)
591486
this.stack.push(node)
592-
this.tokenStack.push([token, errorHandler])
487+
this.tokenStack.push([token, errorHandler || undefined])
593488
node.position = {
594489
start: point(token.start),
595490
// @ts-expect-error: `end` will be patched later.
@@ -620,14 +515,7 @@ function compiler(options) {
620515
}
621516

622517
/**
623-
* @this {CompileContext}
624-
* Context.
625-
* @param {Token} token
626-
* Corresponding token.
627-
* @param {OnExitError | undefined} [onExitError]
628-
* Handle the case where another token is open.
629-
* @returns {undefined}
630-
* Nothing.
518+
* @type {CompileContext['exit']}
631519
*/
632520
function exit(token, onExitError) {
633521
const node = this.stack.pop()
@@ -657,8 +545,7 @@ function compiler(options) {
657545
}
658546

659547
/**
660-
* @this {CompileContext}
661-
* @returns {string}
548+
* @type {CompileContext['resume']}
662549
*/
663550
function resume() {
664551
return toString(this.stack.pop())

0 commit comments

Comments
 (0)