@@ -30,6 +30,10 @@ type Visitor<T, Meta extends Record<string, unknown>> = (
3030
3131type Visitors < T , Meta extends Record < string , unknown > > = Record < string , Visitor < T , Meta > >
3232
33+ function isNodeLike ( value : any ) : value is { type : string } {
34+ return typeof value ?. type === 'string'
35+ }
36+
3337// https://lihautan.com/manipulating-ast-with-javascript/
3438export function visit < T extends { } , Meta extends Record < string , unknown > > (
3539 ast : T ,
@@ -51,7 +55,7 @@ export function visit<T extends {}, Meta extends Record<string, unknown>>(
5155 const child = node [ keys [ i ] ]
5256 if ( Array . isArray ( child ) ) {
5357 for ( let j = 0 ; j < child . length ; j ++ ) {
54- if ( child [ j ] !== null ) {
58+ if ( isNodeLike ( child [ j ] ) ) {
5559 let newMeta = { ...meta }
5660 let newPath = [
5761 {
@@ -67,7 +71,7 @@ export function visit<T extends {}, Meta extends Record<string, unknown>>(
6771 _visit ( child [ j ] , newPath , newMeta )
6872 }
6973 }
70- } else if ( typeof child ?. type === 'string' ) {
74+ } else if ( isNodeLike ( child ) ) {
7175 let newMeta = { ...meta }
7276 let newPath = [
7377 {
0 commit comments