22 * @typedef {import('hast').Element } Element
33 * @typedef {import('hast').Root } Root
44 * @typedef {import('hast').Text } Text
5- *
6- * @typedef {import('unist-util-is').AssertPredicate<Element> } AssertElement
7- * @typedef {import('unist-util-is').AssertPredicate<Text> } AssertText
8- * @typedef {import('unist-util-is').AssertPredicate<Root> } AssertRoot
5+ * @typedef {import('hast').Root|import('hast').Content } Node
96 *
107 * @callback CreateElementLike
118 * @param {string } name
129 * @param {any } attributes
13- * @param {Array.<string| any> } [children]
10+ * @param {Array< any> } [children]
1411 * @returns {any }
1512 *
1613 * @typedef Context
@@ -32,30 +29,19 @@ import {stringify as spaces} from 'space-separated-tokens'
3229import { stringify as commas } from 'comma-separated-tokens'
3330import style from 'style-to-object'
3431import { webNamespaces } from 'web-namespaces'
35- import { convert } from 'unist-util-is'
3632
37- const ns = /** @type {Record<string, string> } */ ( webNamespaces )
3833const toReact = /** @type {Record<string, string> } */ ( hastToReact )
3934
4035const own = { } . hasOwnProperty
4136
42- /** @type {AssertRoot } */
43- // @ts -expect-error it’s correct.
44- const root = convert ( 'root' )
45- /** @type {AssertElement } */
46- // @ts -expect-error it’s correct.
47- const element = convert ( 'element' )
48- /** @type {AssertText } */
49- // @ts -expect-error it’s correct.
50- const text = convert ( 'text' )
51-
5237/**
5338 * @template {CreateElementLike} H
5439 * @param {H } h
55- * @param {Element|Root } tree
40+ * @param {Node } tree
5641 * @param {string|boolean|Options } [options]
5742 * @returns {ReturnType<H> }
5843 */
44+ // eslint-disable-next-line complexity
5945export function toH ( h , tree , options ) {
6046 if ( typeof h !== 'function' ) {
6147 throw new TypeError ( 'h is not a function' )
@@ -77,22 +63,22 @@ export function toH(h, tree, options) {
7763 prefix = options . prefix
7864 }
7965
80- if ( root ( tree ) ) {
66+ if ( tree && tree . type === 'root' ) {
67+ const head = tree . children [ 0 ]
8168 // @ts -expect-error Allow `doctypes` in there, we’ll filter them out later.
8269 node =
83- tree . children . length === 1 && element ( tree . children [ 0 ] )
84- ? tree . children [ 0 ]
70+ tree . children . length === 1 && head . type === 'element'
71+ ? head
8572 : {
8673 type : 'element' ,
8774 tagName : 'div' ,
8875 properties : { } ,
8976 children : tree . children
9077 }
91- } else if ( element ( tree ) ) {
78+ } else if ( tree && tree . type === 'element' ) {
9279 node = tree
9380 } else {
9481 throw new Error (
95- // @ts -expect-error runtime.
9682 'Expected root or element, not `' + ( ( tree && tree . type ) || tree ) + '`'
9783 )
9884 }
@@ -131,7 +117,7 @@ function transform(h, node, ctx) {
131117 let name = node . tagName
132118 /** @type {Record<string, unknown> } */
133119 const attributes = { }
134- /** @type {Array. <ReturnType<H>|string> } */
120+ /** @type {Array<ReturnType<H>|string> } */
135121 const nodes = [ ]
136122 let index = - 1
137123 /** @type {string } */
@@ -152,7 +138,7 @@ function transform(h, node, ctx) {
152138 if ( schema . space === 'html' ) {
153139 name = name . toUpperCase ( )
154140 } else if ( schema . space ) {
155- attributes . namespace = ns [ schema . space ]
141+ attributes . namespace = webNamespaces [ schema . space ]
156142 }
157143 }
158144
@@ -165,9 +151,9 @@ function transform(h, node, ctx) {
165151 while ( ++ index < node . children . length ) {
166152 const value = node . children [ index ]
167153
168- if ( element ( value ) ) {
154+ if ( value . type === 'element' ) {
169155 nodes . push ( transform ( h , value , ctx ) )
170- } else if ( text ( value ) ) {
156+ } else if ( value . type === 'text' ) {
171157 nodes . push ( value . value )
172158 }
173159 }
@@ -256,8 +242,7 @@ function addAttribute(props, prop, value, ctx, name) {
256242 * @returns {boolean }
257243 */
258244function react ( h ) {
259- /** @type {unknown } */
260- const node = h ( 'div' , { } )
245+ const node = /** @type {unknown } */ ( h ( 'div' , { } ) )
261246 return Boolean (
262247 node &&
263248 // @ts -expect-error Looks like a React node.
@@ -284,8 +269,7 @@ function hyperscript(h) {
284269 * @returns {boolean }
285270 */
286271function vdom ( h ) {
287- /** @type {unknown } */
288- const node = h ( 'div' , { } )
272+ const node = /** @type {unknown } */ ( h ( 'div' , { } ) )
289273 // @ts -expect-error Looks like a vnode.
290274 return node . type === 'VirtualNode'
291275}
@@ -297,8 +281,7 @@ function vdom(h) {
297281 * @returns {boolean }
298282 */
299283function vue ( h ) {
300- /** @type {unknown } */
301- const node = h ( 'div' , { } )
284+ const node = /** @type {unknown } */ ( h ( 'div' , { } ) )
302285 // @ts -expect-error Looks like a Vue node.
303286 return Boolean ( node && node . context && node . context . _isVue )
304287}
@@ -323,7 +306,8 @@ function parseStyle(value, tagName) {
323306 * @param {string } _
324307 * @param {string } $1
325308 * @returns {string }
326- */ ( _ , $1 ) => $1 . toUpperCase ( )
309+ */
310+ ( _ , $1 ) => $1 . toUpperCase ( )
327311 )
328312 ] = value
329313 } )
0 commit comments