28
28
* @callback Handler
29
29
* @param {Context } ctx
30
30
* @param {P5Node } node
31
- * @param {Array.<Child> } children
31
+ * @param {Array.<Child> } [ children]
32
32
* @returns {Node }
33
33
*
34
34
* @typedef Options
38
38
*
39
39
* @typedef Context
40
40
* @property {Schema } schema
41
- * @property {VFile } file
42
- * @property {boolean } verbose
41
+ * @property {VFile|undefined } file
42
+ * @property {boolean|undefined } verbose
43
43
* @property {boolean } location
44
44
*/
45
45
@@ -68,7 +68,7 @@ const map = {
68
68
export function fromParse5 ( ast , options = { } ) {
69
69
/** @type {Options } */
70
70
let settings
71
- /** @type {VFile } */
71
+ /** @type {VFile|undefined } */
72
72
let file
73
73
74
74
if ( isFile ( options ) ) {
@@ -100,8 +100,9 @@ export function fromParse5(ast, options = {}) {
100
100
function transform ( ctx , ast ) {
101
101
const schema = ctx . schema
102
102
/** @type {Handler } */
103
+ // @ts -expect-error: index is fine.
103
104
const fn = own . call ( map , ast . nodeName ) ? map [ ast . nodeName ] : element
104
- /** @type {Array.<Child> } */
105
+ /** @type {Array.<Child>|undefined } */
105
106
let children
106
107
107
108
// Element.
@@ -116,7 +117,7 @@ function transform(ctx, ast) {
116
117
const result = fn ( ctx , ast , children )
117
118
118
119
if ( 'sourceCodeLocation' in ast && ast . sourceCodeLocation && ctx . file ) {
119
- // @ts -ignore It’s fine.
120
+ // @ts -expect-error It’s fine.
120
121
const position = location ( ctx , result , ast . sourceCodeLocation )
121
122
122
123
if ( position ) {
@@ -143,7 +144,7 @@ function nodes(ctx, children) {
143
144
const result = [ ]
144
145
145
146
while ( ++ index < children . length ) {
146
- // @ts -ignore Assume no roots in children.
147
+ // @ts -expect-error Assume no roots in children.
147
148
result [ index ] = transform ( ctx , children [ index ] )
148
149
}
149
150
@@ -186,7 +187,7 @@ function root(ctx, ast, children) {
186
187
* @returns {Doctype }
187
188
*/
188
189
function doctype ( ) {
189
- // @ts -ignore Types are out of date.
190
+ // @ts -expect-error Types are out of date.
190
191
return { type : 'doctype' }
191
192
}
192
193
@@ -235,17 +236,19 @@ function element(ctx, ast, children) {
235
236
const result = fn ( ast . tagName , props , children )
236
237
237
238
if ( result . tagName === 'template' && 'content' in ast ) {
238
- // @ts -ignore Types are wrong.
239
239
const pos = ast . sourceCodeLocation
240
- const start = pos && pos . startTag && position ( pos . startTag ) . end
241
- const end = pos && pos . endTag && position ( pos . endTag ) . start
240
+ const startTag = pos && pos . startTag && position ( pos . startTag )
241
+ const endTag = pos && pos . endTag && position ( pos . endTag )
242
242
243
- // @ts -ignore Types are wrong.
244
- result . content = transform ( ctx , ast . content )
243
+ /** @type {Root } */
244
+ // @ts -expect-error Types are wrong.
245
+ const content = transform ( ctx , ast . content )
245
246
246
- if ( ( start || end ) && ctx . file ) {
247
- result . content . position = { start, end}
247
+ if ( startTag && endTag && ctx . file ) {
248
+ content . position = { start : startTag . end , end : endTag . start }
248
249
}
250
+
251
+ result . content = content
249
252
}
250
253
251
254
return result
@@ -257,7 +260,7 @@ function element(ctx, ast, children) {
257
260
* @param {Context } ctx
258
261
* @param {Node } node
259
262
* @param {P5ElementLocation } location
260
- * @returns {Position }
263
+ * @returns {Position|null }
261
264
*/
262
265
function location ( ctx , node , location ) {
263
266
const result = position ( location )
@@ -267,12 +270,18 @@ function location(ctx, node, location) {
267
270
268
271
// Bug for unclosed with children.
269
272
// See: <https://github.com./inikulin/parse5/issues/109>.
270
- if ( ! location . endTag && tail && tail . position && tail . position . end ) {
273
+ if (
274
+ result &&
275
+ ! location . endTag &&
276
+ tail &&
277
+ tail . position &&
278
+ tail . position . end
279
+ ) {
271
280
result . end = Object . assign ( { } , tail . position . end )
272
281
}
273
282
274
283
if ( ctx . verbose ) {
275
- /** @type {Object.<string, Position> } */
284
+ /** @type {Object.<string, Position|null > } */
276
285
const props = { }
277
286
/** @type {string } */
278
287
let key
@@ -311,6 +320,7 @@ function position(loc) {
311
320
column : loc . endCol ,
312
321
offset : loc . endOffset
313
322
} )
323
+ // @ts -expect-error `null` is fine.
314
324
return start || end ? { start, end} : null
315
325
}
316
326
0 commit comments