Skip to content

Commit

Permalink
Refactor types to use node types, not strings
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jan 3, 2023
1 parent 35a9ccc commit d2108dd
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
5 changes: 3 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import {zwitch} from 'zwitch'
import {configure} from './configure.js'
import {handle} from './handle/index.js'
import {handle as handlers} from './handle/index.js'
import {join} from './join.js'
import {unsafe} from './unsafe.js'

Expand All @@ -29,14 +29,15 @@ export function toMarkdown(tree, options = {}) {
stack: [],
unsafe: [],
join: [],
// @ts-expect-error: we’ll fill it next.
handlers: {},
options: {},
indexStack: [],
// @ts-expect-error: we’ll add `handle` later.
handle: undefined
}

configure(state, {unsafe, join, handlers: handle})
configure(state, {unsafe, join, handlers})
configure(state, options)

if (state.options.tightDefinitions) {
Expand Down
4 changes: 2 additions & 2 deletions lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
* @returns {string}
* Serialized markdown representing `node`.
*
* @typedef {Record<string, Handle>} Handlers
* @typedef {Record<Node['type'], Handle>} Handlers
* Handle particular nodes.
*
* Each key is a node type, each value its corresponding handler.
Expand Down Expand Up @@ -238,7 +238,7 @@
* }
* }
* ```
* @property {Handlers | null | undefined} [handlers={}]
* @property {Partial<Handlers> | null | undefined} [handlers={}]
* Handle particular nodes.
*
* Each key is a node type, each value its corresponding handler.
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ Serialized markdown representing `node` (`string`).

Handle particular nodes (TypeScript type).

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

###### Type
Expand Down
11 changes: 9 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* @typedef {import('mdast').BlockContent} BlockContent
* @typedef {import('mdast').List} List
* @typedef {import('../index.js').Handle} Handle
*/

import assert from 'node:assert/strict'
Expand Down Expand Up @@ -3810,7 +3811,10 @@ test('position (output)', function () {
},
{
handlers: {
/** @param {unknown} _ */
/**
* @type {Handle}
* @param {unknown} _
*/
unknown(_, _2, _3, info) {
const {now, lineShift} = info
assert.deepEqual(
Expand Down Expand Up @@ -3849,7 +3853,10 @@ test('position (output)', function () {
},
{
handlers: {
/** @param {unknown} _ */
/**
* @type {Handle}
* @param {unknown} _
*/
unknown(_, _2, _3, info) {
const {now, lineShift} = info
assert.deepEqual(
Expand Down

0 comments on commit d2108dd

Please sign in to comment.