Skip to content

Commit 08fb392

Browse files
committed
Refactor code-style
1 parent f0713bc commit 08fb392

File tree

3 files changed

+333
-237
lines changed

3 files changed

+333
-237
lines changed

lib/index.js

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
/**
2-
* @typedef {import('property-information').Schema} Schema
3-
* @typedef {import('hast').Root} HastRoot
2+
* @typedef {import('hast').Comment} HastComment
43
* @typedef {import('hast').Doctype} HastDoctype
54
* @typedef {import('hast').Element} HastElement
6-
* @typedef {import('hast').Comment} HastComment
7-
* @typedef {import('hast').Text} HastText
85
* @typedef {import('hast').Nodes} HastNodes
9-
* @typedef {import('xast').Root} XastRoot
10-
* @typedef {import('xast').Element} XastElement
11-
* @typedef {import('xast').Text} XastText
6+
* @typedef {import('hast').Root} HastRoot
7+
* @typedef {import('hast').Text} HastText
8+
*
9+
* @typedef {import('property-information').Schema} Schema
10+
*
11+
* @typedef {import('xast').Attributes} XastAttributes
1212
* @typedef {import('xast').Comment} XastComment
1313
* @typedef {import('xast').Doctype} XastDoctype
14-
* @typedef {import('xast').Attributes} XastAttributes
15-
* @typedef {import('xast').RootContent} XastRootContent
16-
* @typedef {import('xast').Nodes} XastNodes
14+
* @typedef {import('xast').Element} XastElement
1715
* @typedef {import('xast').ElementContent} XastElementContent
16+
* @typedef {import('xast').Nodes} XastNodes
17+
* @typedef {import('xast').Root} XastRoot
18+
* @typedef {import('xast').RootContent} XastRootContent
19+
* @typedef {import('xast').Text} XastText
1820
*/
1921

2022
/**
21-
* @typedef {'html' | 'svg'} Space
22-
* Namespace.
23-
*
2423
* @typedef Options
2524
* Configuration.
2625
* @property {Space | null | undefined} [space='html']
27-
* Which space the document is in.
26+
* Which space the document is in (default: `'html'`).
2827
*
2928
* When an `<svg>` element is found in the HTML space, this package already
3029
* automatically switches to and from the SVG space when entering and exiting
@@ -33,21 +32,27 @@
3332
* You can also switch explicitly with `xmlns` properties in hast, but note
3433
* that only HTML and SVG are supported.
3534
*
35+
* @typedef {'html' | 'svg'} Space
36+
* Namespace.
37+
*
3638
* @typedef State
3739
* Info passed around about the current state.
38-
* @property {Schema} schema
39-
* Current schema.
4040
* @property {string | undefined} ns
4141
* Namespace.
42+
* @property {Schema} schema
43+
* Current schema.
4244
*/
4345

4446
import {stringify as commas} from 'comma-separated-tokens'
47+
import {find, html, svg} from 'property-information'
4548
import {stringify as spaces} from 'space-separated-tokens'
46-
import {html, svg, find} from 'property-information'
4749
import {position} from 'unist-util-position'
4850
import {webNamespaces} from 'web-namespaces'
4951
import {zwitch} from 'zwitch'
5052

53+
/** @type {Options} */
54+
const emptyOptions = {}
55+
5156
const own = {}.hasOwnProperty
5257

5358
/** @type {(node: HastNodes, state: State) => XastNodes} */
@@ -57,19 +62,20 @@ const one = zwitch('type', {
5762
unknown
5863
})
5964

65+
// To do: next major: remove `space` shortcut.
6066
/**
6167
* Turn a hast tree into a xast tree.
6268
*
6369
* @param {HastNodes} tree
6470
* hast tree to transform.
65-
* @param {Space | Options | null | undefined} [options]
66-
* Configuration.
71+
* @param {Options | Space | null | undefined} [options]
72+
* Configuration (optional).
6773
* @returns {XastNodes}
6874
* xast tree.
6975
*/
7076
export function toXast(tree, options) {
7177
const settings =
72-
typeof options === 'string' ? {space: options} : options || {}
78+
typeof options === 'string' ? {space: options} : options || emptyOptions
7379

7480
return one(tree, {
7581
schema: settings.space === 'svg' ? svg : html,
@@ -118,8 +124,9 @@ function root(node, state) {
118124
let index = -1
119125

120126
while (++index < node.children.length) {
121-
// @ts-expect-error never root.
122-
children[index] = one(node.children[index], state)
127+
const child = node.children[index]
128+
const result = /** @type {XastRootContent} */ (one(child, state))
129+
children[index] = result
123130
}
124131

125132
/** @type {XastRoot} */
@@ -232,11 +239,11 @@ function element(node, state) {
232239

233240
// Ignore nullish, false, and `NaN` values, and falsey known booleans.
234241
if (
235-
value === undefined ||
236-
value === null ||
237-
value === false ||
238242
(typeof value === 'number' && Number.isNaN(value)) ||
239-
(!value && info.boolean)
243+
(!value && info.boolean) ||
244+
value === false ||
245+
value === null ||
246+
value === undefined
240247
) {
241248
continue
242249
}
@@ -268,13 +275,15 @@ function element(node, state) {
268275
node.tagName === 'template' &&
269276
node.content
270277
) {
271-
// @ts-expect-error: never doctype.
272-
children.push(...root(node.content, childState).children)
278+
const results = /** @type {Array<XastElementContent>} */ (
279+
root(node.content, childState).children
280+
)
281+
children.push(...results)
273282
} else {
274283
while (++index < node.children.length) {
275284
const child = node.children[index]
276-
// @ts-expect-error: never root.
277-
children[index] = one(child, childState)
285+
const result = /** @type {XastElementContent} */ (one(child, childState))
286+
children[index] = result
278287
}
279288
}
280289

@@ -291,8 +300,11 @@ function element(node, state) {
291300

292301
/**
293302
* @param {HastNodes} origin
303+
* hast node.
294304
* @param {XastNodes} node
295-
* @returns {void}
305+
* xast node.
306+
* @returns {undefined}
307+
* Nothing.
296308
*/
297309
function patch(origin, node) {
298310
if (origin.position) node.position = position(origin)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"@types/node": "^20.0.0",
5050
"c8": "^8.0.0",
5151
"hastscript": "^8.0.0",
52+
"mdast-util-to-hast": "^13.0.0",
5253
"prettier": "^3.0.0",
5354
"remark-cli": "^11.0.0",
5455
"remark-preset-wooorm": "^9.0.0",

0 commit comments

Comments
 (0)