@@ -50,7 +50,7 @@ const reverseTypes = [
5050test ( 'unist-util-visit' , ( t ) => {
5151 t . throws (
5252 ( ) => {
53- // @ts -ignore runtime.
53+ // @ts -expect-error runtime.
5454 visit ( )
5555 } ,
5656 / T y p e E r r o r : v i s i t o r i s n o t a f u n c t i o n / ,
@@ -59,7 +59,7 @@ test('unist-util-visit', (t) => {
5959
6060 t . throws (
6161 ( ) => {
62- // @ts -ignore runtime.
62+ // @ts -expect-error runtime.
6363 visit ( tree )
6464 } ,
6565 / T y p e E r r o r : v i s i t o r i s n o t a f u n c t i o n / ,
@@ -146,58 +146,47 @@ test('unist-util-visit', (t) => {
146146 t . test ( 'should accept any `is`-compatible test function' , ( t ) => {
147147 let n = 0
148148
149- visit ( tree , test , visitor )
149+ visit ( tree , test , ( node , index , parent ) => {
150+ const info = '(' + ( parent && parent . type ) + ':' + index + ')'
151+ assert . ok ( test ( node , index ) , 'should be a requested node ' + info )
152+ n ++
153+ } )
150154
151155 t . equal ( n , 3 , 'should visit all passing nodes' )
152156
153157 t . end ( )
154158
155- /**
156- * @param {Node } node
157- * @param {number|null } index
158- * @param {Parent|null } parent
159- */
160- function visitor ( node , index , parent ) {
161- const info = '(' + ( parent && parent . type ) + ':' + index + ')'
162- assert . ok ( test ( node , index ) , 'should be a requested node ' + info )
163- n ++
164- }
165-
166159 /**
167160 * @param {Node } _
168- * @param {number|null } index
161+ * @param {number|null|undefined } index
169162 */
170163 function test ( _ , index ) {
171- return index > 3
164+ return typeof index === 'number' && index > 3
172165 }
173166 } )
174167
175168 t . test ( 'should accept an array of `is`-compatible tests' , ( t ) => {
176169 const expected = new Set ( [ 'root' , 'paragraph' , 'emphasis' , 'strong' ] )
177- const tests = [ test , 'paragraph' , { value : '.' } , 'emphasis' , 'strong' ]
170+ const tests = [
171+ /** @param {Node } node */
172+ ( node ) => node . type === 'root' ,
173+ 'paragraph' ,
174+ { value : '.' } ,
175+ 'emphasis' ,
176+ 'strong'
177+ ]
178178 let n = 0
179179
180- visit ( tree , tests , visitor )
181-
182- t . equal ( n , 5 , 'should visit all passing nodes' )
183-
184- t . end ( )
185-
186- /**
187- * @param {Literal } node
188- */
189- function visitor ( node ) {
180+ visit ( tree , tests , ( node ) => {
181+ // @ts -expect-error: indexable.
190182 const ok = expected . has ( node . type ) || node . value === '.'
191183 assert . ok ( ok , 'should be a requested type: ' + node . type )
192184 n ++
193- }
185+ } )
194186
195- /**
196- * @param {Node } node
197- */
198- function test ( node ) {
199- return node . type === 'root'
200- }
187+ t . equal ( n , 5 , 'should visit all passing nodes' )
188+
189+ t . end ( )
201190 } )
202191
203192 t . test ( 'should stop if `visitor` stops' , ( t ) => {
@@ -383,7 +372,7 @@ test('unist-util-visit', (t) => {
383372 'should be the expected type'
384373 )
385374
386- if ( again === false && node . type === 'strong' ) {
375+ if ( parent && again === false && node . type === 'strong' ) {
387376 again = true
388377 return parent . children . length // Skip siblings.
389378 }
@@ -424,7 +413,11 @@ test('unist-util-visit', (t) => {
424413 'should be the expected type'
425414 )
426415
427- if ( again === false && node . type === 'strong' ) {
416+ if (
417+ typeof index === 'number' &&
418+ again === false &&
419+ node . type === 'strong'
420+ ) {
428421 again = true
429422 return index + 2 // Skip to `inlineCode`.
430423 }
@@ -458,7 +451,7 @@ test('unist-util-visit', (t) => {
458451 function visitor ( _1 , _2 , parent ) {
459452 n ++
460453
461- if ( n === 2 ) {
454+ if ( parent && n === 2 ) {
462455 parent . children . push ( other )
463456 }
464457 }
0 commit comments