Skip to content
This repository was archived by the owner on Aug 9, 2023. It is now read-only.

Commit 38eb037

Browse files
committed
Refactor code-style
1 parent 0b1fb57 commit 38eb037

File tree

3 files changed

+18
-37
lines changed

3 files changed

+18
-37
lines changed

index.js

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
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'
3229
import {stringify as commas} from 'comma-separated-tokens'
3330
import style from 'style-to-object'
3431
import {webNamespaces} from 'web-namespaces'
35-
import {convert} from 'unist-util-is'
3632

37-
const ns = /** @type {Record<string, string>} */ (webNamespaces)
3833
const toReact = /** @type {Record<string, string>} */ (hastToReact)
3934

4035
const 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
5945
export 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
*/
258244
function 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
*/
286271
function 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
*/
299283
function 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
})

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
"property-information": "^6.0.0",
4747
"space-separated-tokens": "^2.0.0",
4848
"style-to-object": "^0.3.0",
49-
"unist-util-is": "^5.0.0",
5049
"web-namespaces": "^2.0.0"
5150
},
5251
"devDependencies": {

test.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,10 @@ test('hast-to-hyperscript', (t) => {
4444
}, /Expected root or element, not `undefined`/)
4545

4646
t.throws(() => {
47-
// @ts-expect-error runtime.
4847
toH(h, u('text', 'Alpha'))
4948
}, /Error: Expected root or element, not `text`/)
5049

5150
t.throws(() => {
52-
// @ts-expect-error runtime.
5351
toH(h, u('text', 'value'))
5452
}, /Expected root or element/)
5553

0 commit comments

Comments
 (0)