11/**
2- * @typedef {import('vfile').VFile } VFile
3- * @typedef {import('property-information').Schema } Schema
4- * @typedef {import('unist').Position } Position
5- * @typedef {import('unist').Point } Point
62 * @typedef {import('hast').Element } Element
3+ * @typedef {import('hast').Nodes } Nodes
74 * @typedef {import('hast').Root } Root
85 * @typedef {import('hast').RootContent } RootContent
9- * @typedef { import('hast').Nodes } Nodes
6+ *
107 * @typedef {import('parse5').DefaultTreeAdapterMap } DefaultTreeAdapterMap
118 * @typedef {import('parse5').Token.ElementLocation } P5ElementLocation
129 * @typedef {import('parse5').Token.Location } P5Location
10+ *
11+ * @typedef {import('property-information').Schema } Schema
12+ *
13+ * @typedef {import('unist').Point } Point
14+ * @typedef {import('unist').Position } Position
15+ *
16+ * @typedef {import('vfile').VFile } VFile
1317 */
1418
1519/**
2125 * @typedef {DefaultTreeAdapterMap['element'] } P5Element
2226 * @typedef {DefaultTreeAdapterMap['node'] } P5Node
2327 * @typedef {DefaultTreeAdapterMap['template'] } P5Template
24- *
25- * @typedef {'html' | 'svg' } Space
26- * Namespace.
27- *
28+ */
29+
30+ /**
2831 * @typedef Options
2932 * Configuration.
3033 * @property {Space | null | undefined } [space='html']
31- * Which space the document is in.
34+ * Which space the document is in (default: `'html'`) .
3235 *
3336 * When an `<svg>` element is found in the HTML space, this package already
3437 * automatically switches to and from the SVG space when entering and exiting
3538 * it.
3639 * @property {VFile | null | undefined } [file]
37- * File used to add positional info to nodes.
40+ * File used to add positional info to nodes (optional) .
3841 *
3942 * If given, the file should represent the original HTML source.
4043 * @property {boolean } [verbose=false]
4144 * Whether to add extra positional info about starting tags, closing tags,
42- * and attributes to elements.
45+ * and attributes to elements (default: `false`) .
4346 *
4447 * > 👉 **Note**: only used when `file` is given.
4548 *
49+ * @typedef {'html' | 'svg' } Space
50+ * Namespace.
51+ *
4652 * @typedef State
4753 * Info passed around about the current state.
48- * @property {Schema } schema
49- * Current schema.
5054 * @property {VFile | undefined } file
5155 * Corresponding file.
52- * @property {boolean | undefined } verbose
53- * Add extra positional info.
5456 * @property {boolean } location
5557 * Whether location info was found.
58+ * @property {Schema } schema
59+ * Current schema.
60+ * @property {boolean | undefined } verbose
61+ * Add extra positional info.
5662 */
5763
64+ import { ok as assert } from 'devlop'
5865import { h , s } from 'hastscript'
59- import { html , svg , find } from 'property-information'
66+ import { find , html , svg } from 'property-information'
6067import { location } from 'vfile-location'
6168import { webNamespaces } from 'web-namespaces'
6269
@@ -71,7 +78,7 @@ const proto = Object.prototype
7178 * @param {P5Node } tree
7279 * `parse5` tree to transform.
7380 * @param {Options | VFile | null | undefined } [options]
74- * Configuration.
81+ * Configuration (optional) .
7582 * @returns {Nodes }
7683 * hast tree.
7784 */
@@ -92,10 +99,10 @@ export function fromParse5(tree, options) {
9299
93100 return one (
94101 {
95- schema : settings . space === 'svg' ? svg : html ,
96102 file,
97- verbose : settings . verbose ,
98- location : false
103+ location : false ,
104+ schema : settings . space === 'svg' ? svg : html ,
105+ verbose : settings . verbose
99106 } ,
100107 tree
101108 )
@@ -142,7 +149,9 @@ function one(state, node) {
142149 const loc = location ( doc )
143150 const start = loc . toPoint ( 0 )
144151 const end = loc . toPoint ( doc . length )
145- // @ts -expect-error: always defined as we give valid input.
152+ // Always defined as we give valid input.
153+ assert ( start , 'expected `start`' )
154+ assert ( end , 'expected `end`' )
146155 result . position = { start, end}
147156 }
148157
@@ -185,14 +194,15 @@ function one(state, node) {
185194function all ( state , nodes ) {
186195 let index = - 1
187196 /** @type {Array<RootContent> } */
188- const result = [ ]
197+ const results = [ ]
189198
190199 while ( ++ index < nodes . length ) {
191- // @ts -expect-error Assume no roots in `nodes`.
192- result [ index ] = one ( state , nodes [ index ] )
200+ // Assume no roots in `nodes`.
201+ const result = /** @type {RootContent } */ ( one ( state , nodes [ index ] ) )
202+ results . push ( result )
193203 }
194204
195- return result
205+ return results
196206}
197207
198208/**
@@ -236,9 +246,8 @@ function element(state, node) {
236246 const startTag = pos && pos . startTag && position ( pos . startTag )
237247 const endTag = pos && pos . endTag && position ( pos . endTag )
238248
239- /** @type {Root } */
240- // @ts -expect-error Types are wrong.
241- const content = one ( state , reference . content )
249+ // Root in, root out.
250+ const content = /** @type {Root } */ ( one ( state , reference . content ) )
242251
243252 if ( startTag && endTag && state . file ) {
244253 content . position = { start : startTag . end , end : endTag . start }
@@ -261,7 +270,7 @@ function element(state, node) {
261270 * p5 node.
262271 * @param {Nodes } to
263272 * hast node.
264- * @returns {void }
273+ * @returns {undefined }
265274 * Nothing.
266275 */
267276function patch ( state , from , to ) {
@@ -321,14 +330,15 @@ function createLocation(state, node, location) {
321330 }
322331 }
323332
324- node . data = {
325- position : {
326- // @ts -expect-error: assume not `undefined`.
327- opening : position ( location . startTag ) ,
328- closing : location . endTag ? position ( location . endTag ) : null ,
329- properties : props
330- }
331- }
333+ assert ( location . startTag , 'a start tag should exist' )
334+ const opening = position ( location . startTag )
335+ const closing = location . endTag ? position ( location . endTag ) : undefined
336+ /** @type {Record<string, unknown> } */
337+ const data = { opening}
338+ if ( closing ) data . closing = closing
339+ data . properties = props
340+
341+ node . data = { position : data }
332342 }
333343 }
334344
@@ -354,7 +364,9 @@ function position(loc) {
354364 column : loc . endCol ,
355365 offset : loc . endOffset
356366 } )
357- // @ts -expect-error `undefined` is fine.
367+
368+ // @ts -expect-error: we do use `undefined` for points if one or the other
369+ // exists.
358370 return start || end ? { start, end} : undefined
359371}
360372
0 commit comments