@@ -312,11 +312,95 @@ describe('when calling getTokensAfter', () => {
312312 getTokensAfter ;
313313} ) ;
314314
315+ // https://github.com/eslint/eslint/blob/v9.39.1/tests/lib/languages/js/source-code/token-store.js#L594-L673
315316describe ( 'when calling getFirstTokens' , ( ) => {
316- /* oxlint-disable-next-line no-disabled-tests expect-expect */
317- it ( 'is to be implemented' ) ;
318- /* oxlint-disable-next-line no-unused-expressions */
319- getFirstTokens ;
317+ it ( "should retrieve zero tokens from a node's token stream" , ( ) => {
318+ assert . deepStrictEqual (
319+ getFirstTokens ( BinaryExpression , 0 ) . map ( ( token ) => token . value ) ,
320+ [ ] ,
321+ ) ;
322+ } ) ;
323+
324+ it ( "should retrieve one token from a node's token stream" , ( ) => {
325+ assert . deepStrictEqual (
326+ getFirstTokens ( BinaryExpression , 1 ) . map ( ( token ) => token . value ) ,
327+ [ 'a' ] ,
328+ ) ;
329+ } ) ;
330+
331+ it ( "should retrieve more than one token from a node's token stream" , ( ) => {
332+ assert . deepStrictEqual (
333+ getFirstTokens ( BinaryExpression , 2 ) . map ( ( token ) => token . value ) ,
334+ [ 'a' , '*' ] ,
335+ ) ;
336+ } ) ;
337+
338+ it ( "should retrieve all tokens from a node's token stream" , ( ) => {
339+ assert . deepStrictEqual (
340+ getFirstTokens ( BinaryExpression , 9e9 ) . map ( ( token ) => token . value ) ,
341+ [ 'a' , '*' , 'b' ] ,
342+ ) ;
343+ } ) ;
344+
345+ it ( "should retrieve more than one token from a node's token stream with count option" , ( ) => {
346+ assert . deepStrictEqual (
347+ getFirstTokens ( BinaryExpression , { count : 2 } ) . map ( ( token ) => token . value ) ,
348+ [ 'a' , '*' ] ,
349+ ) ;
350+ } ) ;
351+
352+ it ( "should retrieve matched tokens from a node's token stream with filter option" , ( ) => {
353+ assert . deepStrictEqual (
354+ getFirstTokens ( BinaryExpression , ( t ) => t . type === 'Identifier' ) . map ( ( token ) => token . value ) ,
355+ [ 'a' , 'b' ] ,
356+ ) ;
357+ assert . deepStrictEqual (
358+ getFirstTokens ( BinaryExpression , {
359+ filter : ( t ) => t . type === 'Identifier' ,
360+ } ) . map ( ( token ) => token . value ) ,
361+ [ 'a' , 'b' ] ,
362+ ) ;
363+ } ) ;
364+
365+ it ( "should retrieve matched tokens from a node's token stream with filter and count options" , ( ) => {
366+ assert . deepStrictEqual (
367+ getFirstTokens ( BinaryExpression , {
368+ count : 1 ,
369+ filter : ( t ) => t . type === 'Identifier' ,
370+ } ) . map ( ( token ) => token . value ) ,
371+ [ 'a' ] ,
372+ ) ;
373+ } ) ;
374+
375+ it ( "should retrieve all tokens and comments from a node's token stream with includeComments option" , ( ) => {
376+ assert . deepStrictEqual (
377+ getFirstTokens ( BinaryExpression , {
378+ includeComments : true ,
379+ } ) . map ( ( token ) => token . value ) ,
380+ [ 'a' , 'D' , '*' , 'b' ] ,
381+ ) ;
382+ } ) ;
383+
384+ it ( "should retrieve several tokens and comments from a node's token stream with includeComments and count options" , ( ) => {
385+ assert . deepStrictEqual (
386+ getFirstTokens ( BinaryExpression , {
387+ includeComments : true ,
388+ count : 3 ,
389+ } ) . map ( ( token ) => token . value ) ,
390+ [ 'a' , 'D' , '*' ] ,
391+ ) ;
392+ } ) ;
393+
394+ it ( "should retrieve several tokens and comments from a node's token stream with includeComments and count and filter options" , ( ) => {
395+ assert . deepStrictEqual (
396+ getFirstTokens ( BinaryExpression , {
397+ includeComments : true ,
398+ count : 3 ,
399+ filter : ( t ) => t . value !== 'a' ,
400+ } ) . map ( ( token ) => token . value ) ,
401+ [ 'D' , '*' , 'b' ] ,
402+ ) ;
403+ } ) ;
320404} ) ;
321405
322406describe ( 'when calling getFirstToken' , ( ) => {
0 commit comments