2828 * @callback Handler
2929 * @param {Context } ctx
3030 * @param {P5Node } node
31- * @param {Array.<Child> } children
31+ * @param {Array.<Child> } [ children]
3232 * @returns {Node }
3333 *
3434 * @typedef Options
3838 *
3939 * @typedef Context
4040 * @property {Schema } schema
41- * @property {VFile } file
42- * @property {boolean } verbose
41+ * @property {VFile|undefined } file
42+ * @property {boolean|undefined } verbose
4343 * @property {boolean } location
4444 */
4545
@@ -68,7 +68,7 @@ const map = {
6868export function fromParse5 ( ast , options = { } ) {
6969 /** @type {Options } */
7070 let settings
71- /** @type {VFile } */
71+ /** @type {VFile|undefined } */
7272 let file
7373
7474 if ( isFile ( options ) ) {
@@ -100,8 +100,9 @@ export function fromParse5(ast, options = {}) {
100100function transform ( ctx , ast ) {
101101 const schema = ctx . schema
102102 /** @type {Handler } */
103+ // @ts -expect-error: index is fine.
103104 const fn = own . call ( map , ast . nodeName ) ? map [ ast . nodeName ] : element
104- /** @type {Array.<Child> } */
105+ /** @type {Array.<Child>|undefined } */
105106 let children
106107
107108 // Element.
@@ -116,7 +117,7 @@ function transform(ctx, ast) {
116117 const result = fn ( ctx , ast , children )
117118
118119 if ( 'sourceCodeLocation' in ast && ast . sourceCodeLocation && ctx . file ) {
119- // @ts -ignore It’s fine.
120+ // @ts -expect-error It’s fine.
120121 const position = location ( ctx , result , ast . sourceCodeLocation )
121122
122123 if ( position ) {
@@ -143,7 +144,7 @@ function nodes(ctx, children) {
143144 const result = [ ]
144145
145146 while ( ++ index < children . length ) {
146- // @ts -ignore Assume no roots in children.
147+ // @ts -expect-error Assume no roots in children.
147148 result [ index ] = transform ( ctx , children [ index ] )
148149 }
149150
@@ -186,7 +187,7 @@ function root(ctx, ast, children) {
186187 * @returns {Doctype }
187188 */
188189function doctype ( ) {
189- // @ts -ignore Types are out of date.
190+ // @ts -expect-error Types are out of date.
190191 return { type : 'doctype' }
191192}
192193
@@ -235,17 +236,19 @@ function element(ctx, ast, children) {
235236 const result = fn ( ast . tagName , props , children )
236237
237238 if ( result . tagName === 'template' && 'content' in ast ) {
238- // @ts -ignore Types are wrong.
239239 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 )
242242
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 )
245246
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 }
248249 }
250+
251+ result . content = content
249252 }
250253
251254 return result
@@ -257,7 +260,7 @@ function element(ctx, ast, children) {
257260 * @param {Context } ctx
258261 * @param {Node } node
259262 * @param {P5ElementLocation } location
260- * @returns {Position }
263+ * @returns {Position|null }
261264 */
262265function location ( ctx , node , location ) {
263266 const result = position ( location )
@@ -267,12 +270,18 @@ function location(ctx, node, location) {
267270
268271 // Bug for unclosed with children.
269272 // 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+ ) {
271280 result . end = Object . assign ( { } , tail . position . end )
272281 }
273282
274283 if ( ctx . verbose ) {
275- /** @type {Object.<string, Position> } */
284+ /** @type {Object.<string, Position|null > } */
276285 const props = { }
277286 /** @type {string } */
278287 let key
@@ -311,6 +320,7 @@ function position(loc) {
311320 column : loc . endCol ,
312321 offset : loc . endOffset
313322 } )
323+ // @ts -expect-error `null` is fine.
314324 return start || end ? { start, end} : null
315325}
316326
0 commit comments