Skip to content

Commit 70ba240

Browse files
committed
Refactor code-style
1 parent 49692d6 commit 70ba240

File tree

3 files changed

+467
-429
lines changed

3 files changed

+467
-429
lines changed

lib/index.js

Lines changed: 77 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,59 @@
11
/**
2-
* @typedef {import('parse5').DefaultTreeAdapterMap} DefaultTreeAdapterMap
3-
* @typedef {DefaultTreeAdapterMap['document']} P5Document
4-
* @typedef {DefaultTreeAdapterMap['documentFragment']} P5Fragment
5-
* @typedef {DefaultTreeAdapterMap['element']} P5Element
6-
* @typedef {DefaultTreeAdapterMap['node']} P5Node
7-
* @typedef {DefaultTreeAdapterMap['documentType']} P5Doctype
8-
* @typedef {DefaultTreeAdapterMap['commentNode']} P5Comment
9-
* @typedef {DefaultTreeAdapterMap['textNode']} P5Text
10-
* @typedef {DefaultTreeAdapterMap['parentNode']} P5Parent
11-
* @typedef {import('parse5').Token.Attribute} P5Attribute
12-
* @typedef {Exclude<P5Node, P5Document | P5Fragment>} P5Child
13-
* @typedef {import('property-information').Schema} Schema
14-
* @typedef {import('hast').Root} Root
2+
* @typedef {import('hast').Comment} Comment
153
* @typedef {import('hast').Doctype} Doctype
164
* @typedef {import('hast').Element} Element
5+
* @typedef {import('hast').Nodes} Nodes
6+
* @typedef {import('hast').Root} Root
7+
* @typedef {import('hast').RootContent} RootContent
178
* @typedef {import('hast').Text} Text
18-
* @typedef {import('hast').Comment} Comment
19-
* @typedef {import('hast').Content} Content
9+
*
10+
* @typedef {import('parse5').DefaultTreeAdapterMap['document']} Parse5Document
11+
* @typedef {import('parse5').DefaultTreeAdapterMap['documentFragment']} Parse5Fragment
12+
* @typedef {import('parse5').DefaultTreeAdapterMap['element']} Parse5Element
13+
* @typedef {import('parse5').DefaultTreeAdapterMap['node']} Parse5Nodes
14+
* @typedef {import('parse5').DefaultTreeAdapterMap['documentType']} Parse5Doctype
15+
* @typedef {import('parse5').DefaultTreeAdapterMap['commentNode']} Parse5Comment
16+
* @typedef {import('parse5').DefaultTreeAdapterMap['textNode']} Parse5Text
17+
* @typedef {import('parse5').DefaultTreeAdapterMap['parentNode']} Parse5Parent
18+
* @typedef {import('parse5').Token.Attribute} Parse5Attribute
19+
*
20+
* @typedef {import('property-information').Schema} Schema
2021
*/
2122

2223
/**
23-
* @typedef {Content | Root} Node
24-
* @typedef {'html' | 'svg'} Space
25-
*
2624
* @typedef Options
2725
* Configuration.
2826
* @property {Space | null | undefined} [space='html']
29-
* Which space the document is in.
27+
* Which space the document is in (default: `'html'`).
3028
*
3129
* When an `<svg>` element is found in the HTML space, this package already
3230
* automatically switches to and from the SVG space when entering and exiting
3331
* it.
32+
*
33+
* @typedef {Exclude<Parse5Nodes, Parse5Document | Parse5Fragment>} Parse5Content
34+
*
35+
* @typedef {'html' | 'svg'} Space
3436
*/
3537

3638
import {stringify as commas} from 'comma-separated-tokens'
37-
import {html, svg, find} from 'property-information'
39+
import {ok as assert} from 'devlop'
40+
import {find, html, svg} from 'property-information'
3841
import {stringify as spaces} from 'space-separated-tokens'
3942
import {webNamespaces} from 'web-namespaces'
4043
import {zwitch} from 'zwitch'
4144

4245
const own = {}.hasOwnProperty
4346

44-
/** @type {(from: Node, schema: Schema) => P5Node} */
4547
const one = zwitch('type', {handlers: {root, element, text, comment, doctype}})
4648

4749
/**
4850
* Transform a hast tree to Parse5’s AST.
4951
*
50-
* @param {Node} tree
52+
* @param {Nodes} tree
5153
* Tree to transform.
5254
* @param {Options | Space | null | undefined} [options]
53-
* Configuration.
54-
* @returns {P5Node}
55+
* Configuration (optional).
56+
* @returns {Parse5Nodes}
5557
* `parse5` node.
5658
*/
5759
export function toParse5(tree, options) {
@@ -61,12 +63,14 @@ export function toParse5(tree, options) {
6163

6264
/**
6365
* @param {Root} node
66+
* Node (hast) to transform.
6467
* @param {Schema} schema
6568
* Current schema.
66-
* @returns {P5Document}
69+
* @returns {Parse5Document}
70+
* Parse5 node.
6771
*/
6872
function root(node, schema) {
69-
/** @type {P5Document} */
73+
/** @type {Parse5Document} */
7074
const result = {
7175
nodeName: '#document',
7276
// @ts-expect-error: `parse5` uses enums, which are actually strings.
@@ -80,12 +84,14 @@ function root(node, schema) {
8084

8185
/**
8286
* @param {Root} node
87+
* Node (hast) to transform.
8388
* @param {Schema} schema
8489
* Current schema.
85-
* @returns {P5Fragment}
90+
* @returns {Parse5Fragment}
91+
* Parse5 node.
8692
*/
8793
function fragment(node, schema) {
88-
/** @type {P5Fragment} */
94+
/** @type {Parse5Fragment} */
8995
const result = {nodeName: '#document-fragment', childNodes: []}
9096
result.childNodes = all(node.children, result, schema)
9197
patch(node, result)
@@ -94,17 +100,18 @@ function fragment(node, schema) {
94100

95101
/**
96102
* @param {Doctype} node
97-
* @returns {P5Doctype}
103+
* Node (hast) to transform.
104+
* @returns {Parse5Doctype}
105+
* Parse5 node.
98106
*/
99107
function doctype(node) {
100-
/** @type {P5Doctype} */
108+
/** @type {Parse5Doctype} */
101109
const result = {
102110
nodeName: '#documentType',
103111
name: 'html',
104112
publicId: '',
105113
systemId: '',
106-
// @ts-expect-error: change to `null` in a major?
107-
parentNode: undefined
114+
parentNode: null
108115
}
109116

110117
patch(node, result)
@@ -113,31 +120,33 @@ function doctype(node) {
113120

114121
/**
115122
* @param {Text} node
116-
* @returns {P5Text}
123+
* Node (hast) to transform.
124+
* @returns {Parse5Text}
125+
* Parse5 node.
117126
*/
118127
function text(node) {
119-
/** @type {P5Text} */
128+
/** @type {Parse5Text} */
120129
const result = {
121130
nodeName: '#text',
122131
value: node.value,
123-
// @ts-expect-error: no `parentNode`
124-
parentNode: undefined
132+
parentNode: null
125133
}
126134
patch(node, result)
127135
return result
128136
}
129137

130138
/**
131139
* @param {Comment} node
132-
* @returns {P5Comment}
140+
* Node (hast) to transform.
141+
* @returns {Parse5Comment}
142+
* Parse5 node.
133143
*/
134144
function comment(node) {
135-
/** @type {P5Comment} */
145+
/** @type {Parse5Comment} */
136146
const result = {
137147
nodeName: '#comment',
138148
data: node.value,
139-
// @ts-expect-error: no `parentNode`
140-
parentNode: undefined
149+
parentNode: null
141150
}
142151

143152
patch(node, result)
@@ -147,10 +156,11 @@ function comment(node) {
147156

148157
/**
149158
* @param {Element} node
159+
* Node (hast) to transform.
150160
* @param {Schema} schema
151161
* Current schema.
152-
* @returns {P5Element}
153-
* `parse5` node.
162+
* @returns {Parse5Element}
163+
* Parse5 node.
154164
*/
155165
function element(node, schema) {
156166
const parentSchema = schema
@@ -164,7 +174,7 @@ function element(node, schema) {
164174
currentSchema = svg
165175
}
166176

167-
/** @type {Array<P5Attribute>} */
177+
/** @type {Array<Parse5Attribute>} */
168178
const attrs = []
169179
/** @type {string} */
170180
let prop
@@ -185,16 +195,19 @@ function element(node, schema) {
185195
}
186196
}
187197

188-
/** @type {P5Element} */
198+
const space = currentSchema.space
199+
// `html` and `svg` both have a space.
200+
assert(space)
201+
202+
/** @type {Parse5Element} */
189203
const result = {
190204
nodeName: node.tagName,
191205
tagName: node.tagName,
192206
attrs,
193-
// @ts-expect-error: html and svg both have a space.
194-
namespaceURI: webNamespaces[currentSchema.space],
207+
// @ts-expect-error: `parse5` types are wrong.
208+
namespaceURI: webNamespaces[space],
195209
childNodes: [],
196-
// @ts-expect-error: no `parentNode`
197-
parentNode: undefined
210+
parentNode: null
198211
}
199212
result.childNodes = all(node.children, result, currentSchema)
200213
patch(node, result)
@@ -214,19 +227,19 @@ function element(node, schema) {
214227
* Current schema.
215228
* @param {string} prop
216229
* Key.
217-
* @param {Array<string | number> | string | number | boolean | null | undefined} value
230+
* @param {Array<number | string> | boolean | number | string | null | undefined} value
218231
* hast property value.
219-
* @returns {P5Attribute | void}
232+
* @returns {Parse5Attribute | undefined}
220233
* Field for runtime, optional.
221234
*/
222235
function createProperty(schema, prop, value) {
223236
const info = find(schema, prop)
224237

225238
// Ignore nullish and `NaN` values.
226239
if (
227-
value === undefined ||
228-
value === null ||
229240
value === false ||
241+
value === null ||
242+
value === undefined ||
230243
(typeof value === 'number' && Number.isNaN(value)) ||
231244
(!value && info.boolean)
232245
) {
@@ -239,7 +252,7 @@ function createProperty(schema, prop, value) {
239252
value = info.commaSeparated ? commas(value) : spaces(value)
240253
}
241254

242-
/** @type {P5Attribute} */
255+
/** @type {Parse5Attribute} */
243256
const attribute = {
244257
name: info.attribute,
245258
value: value === true ? '' : String(value)
@@ -264,24 +277,23 @@ function createProperty(schema, prop, value) {
264277
/**
265278
* Transform all hast nodes.
266279
*
267-
* @param {Array<Content>} children
280+
* @param {Array<RootContent>} children
268281
* List of children.
269-
* @param {P5Parent} parentNode
282+
* @param {Parse5Parent} parentNode
270283
* `parse5` parent node.
271284
* @param {Schema} schema
272285
* Current schema.
273-
* @returns {Array<P5Child>}
286+
* @returns {Array<Parse5Content>}
274287
* Transformed children.
275288
*/
276289
function all(children, parentNode, schema) {
277290
let index = -1
278-
/** @type {Array<P5Child>} */
291+
/** @type {Array<Parse5Content>} */
279292
const results = []
280293

281294
if (children) {
282295
while (++index < children.length) {
283-
/** @type {P5Child} */
284-
// @ts-expect-error assume no document.
296+
/** @type {Parse5Content} */
285297
const child = one(children[index], schema)
286298

287299
child.parentNode = parentNode
@@ -296,25 +308,26 @@ function all(children, parentNode, schema) {
296308
/**
297309
* Add position info from `from` to `to`.
298310
*
299-
* @param {Node} from
311+
* @param {Nodes} from
300312
* hast node.
301-
* @param {P5Node} to
313+
* @param {Parse5Nodes} to
302314
* `parse5` node.
303-
* @returns {void}
315+
* @returns {undefined}
304316
* Nothing.
305317
*/
306318
function patch(from, to) {
307319
const position = from.position
308320

309321
if (position && position.start && position.end) {
322+
assert(typeof position.start.offset === 'number')
323+
assert(typeof position.end.offset === 'number')
324+
310325
to.sourceCodeLocation = {
311326
startLine: position.start.line,
312327
startCol: position.start.column,
313-
// @ts-expect-error assume this is set.
314328
startOffset: position.start.offset,
315329
endLine: position.end.line,
316330
endCol: position.end.column,
317-
// @ts-expect-error assume this is set.
318331
endOffset: position.end.offset
319332
}
320333
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"dependencies": {
3737
"@types/hast": "^3.0.0",
3838
"comma-separated-tokens": "^2.0.0",
39+
"devlop": "^1.0.0",
3940
"property-information": "^6.0.0",
4041
"space-separated-tokens": "^2.0.0",
4142
"web-namespaces": "^2.0.0",

0 commit comments

Comments
 (0)