Skip to content

Commit 6a2abc0

Browse files
committed
Update zwitch
1 parent 67d8e8a commit 6a2abc0

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

lib/index.js

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
*
4141
* @typedef {'html'|'svg'} Space
4242
*
43-
* @typedef {(node: any, context: Context) => EstreeJsxChild?} Handle
43+
* @typedef {(node: any, context: Context) => EstreeJsxChild|null|undefined|void} Handle
4444
*
4545
* @typedef Options
4646
* @property {Space} [space='html']
@@ -90,28 +90,22 @@ export function toEstree(tree, options = {}) {
9090
schema: options.space === 'svg' ? svg : html,
9191
comments: [],
9292
esm: [],
93-
// @ts-expect-error: hush.
9493
handle: zwitch('type', {
9594
invalid,
96-
// @ts-expect-error: hush.
9795
unknown,
98-
// @ts-expect-error: hush.
99-
handlers: Object.assign(
100-
{},
101-
{
102-
comment,
103-
doctype: ignore,
104-
element,
105-
mdxjsEsm,
106-
mdxFlowExpression: mdxExpression,
107-
mdxJsxFlowElement: mdxJsxElement,
108-
mdxJsxTextElement: mdxJsxElement,
109-
mdxTextExpression: mdxExpression,
110-
root,
111-
text
112-
},
113-
options.handlers
114-
)
96+
handlers: {
97+
comment,
98+
doctype,
99+
element,
100+
mdxjsEsm,
101+
mdxFlowExpression: mdxExpression,
102+
mdxJsxFlowElement: mdxJsxElement,
103+
mdxJsxTextElement: mdxJsxElement,
104+
mdxTextExpression: mdxExpression,
105+
root,
106+
text,
107+
...options.handlers
108+
}
115109
})
116110
}
117111
let result = context.handle(tree, context)
@@ -147,13 +141,17 @@ function invalid(value) {
147141
}
148142

149143
/**
150-
* @param {Node} node
144+
* @param {unknown} node
151145
*/
152146
function unknown(node) {
147+
// @ts-expect-error: JS guarantees there’s a `type`.
153148
throw new Error('Cannot handle unknown node `' + node.type + '`')
154149
}
155150

156-
function ignore() {}
151+
/**
152+
* @param {import('hast').DocType} _node
153+
*/
154+
function doctype(_node) {}
157155

158156
/**
159157
* @param {Comment} node

0 commit comments

Comments
 (0)