Skip to content

Commit d2108dd

Browse files
committedJan 3, 2023
Refactor types to use node types, not strings
1 parent 35a9ccc commit d2108dd

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
lines changed
 

‎lib/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import {zwitch} from 'zwitch'
1010
import {configure} from './configure.js'
11-
import {handle} from './handle/index.js'
11+
import {handle as handlers} from './handle/index.js'
1212
import {join} from './join.js'
1313
import {unsafe} from './unsafe.js'
1414

@@ -29,14 +29,15 @@ export function toMarkdown(tree, options = {}) {
2929
stack: [],
3030
unsafe: [],
3131
join: [],
32+
// @ts-expect-error: we’ll fill it next.
3233
handlers: {},
3334
options: {},
3435
indexStack: [],
3536
// @ts-expect-error: we’ll add `handle` later.
3637
handle: undefined
3738
}
3839

39-
configure(state, {unsafe, join, handlers: handle})
40+
configure(state, {unsafe, join, handlers})
4041
configure(state, options)
4142

4243
if (state.options.tightDefinitions) {

‎lib/types.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
* @returns {string}
8181
* Serialized markdown representing `node`.
8282
*
83-
* @typedef {Record<string, Handle>} Handlers
83+
* @typedef {Record<Node['type'], Handle>} Handlers
8484
* Handle particular nodes.
8585
*
8686
* Each key is a node type, each value its corresponding handler.
@@ -238,7 +238,7 @@
238238
* }
239239
* }
240240
* ```
241-
* @property {Handlers | null | undefined} [handlers={}]
241+
* @property {Partial<Handlers> | null | undefined} [handlers={}]
242242
* Handle particular nodes.
243243
*
244244
* Each key is a node type, each value its corresponding handler.

‎readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ Serialized markdown representing `node` (`string`).
208208

209209
Handle particular nodes (TypeScript type).
210210

211-
Each key is a node type (`string`), each value its corresponding handler
211+
Each key is a node type (`Node['type']`), each value its corresponding handler
212212
([`Handle`][handle]).
213213

214214
###### Type

‎test/index.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @typedef {import('mdast').BlockContent} BlockContent
33
* @typedef {import('mdast').List} List
4+
* @typedef {import('../index.js').Handle} Handle
45
*/
56

67
import assert from 'node:assert/strict'
@@ -3810,7 +3811,10 @@ test('position (output)', function () {
38103811
},
38113812
{
38123813
handlers: {
3813-
/** @param {unknown} _ */
3814+
/**
3815+
* @type {Handle}
3816+
* @param {unknown} _
3817+
*/
38143818
unknown(_, _2, _3, info) {
38153819
const {now, lineShift} = info
38163820
assert.deepEqual(
@@ -3849,7 +3853,10 @@ test('position (output)', function () {
38493853
},
38503854
{
38513855
handlers: {
3852-
/** @param {unknown} _ */
3856+
/**
3857+
* @type {Handle}
3858+
* @param {unknown} _
3859+
*/
38533860
unknown(_, _2, _3, info) {
38543861
const {now, lineShift} = info
38553862
assert.deepEqual(

0 commit comments

Comments
 (0)
Please sign in to comment.