@@ -7,6 +7,17 @@ import {
77 getTokensAfter ,
88 getTokenAfter ,
99 getFirstTokens ,
10+ getFirstToken ,
11+ getLastTokens ,
12+ getLastToken ,
13+ getFirstTokensBetween ,
14+ getFirstTokenBetween ,
15+ getLastTokenBetween ,
16+ getLastTokensBetween ,
17+ getTokenByRangeStart ,
18+ getTokensBetween ,
19+ getTokenOrCommentBefore ,
20+ getTokenOrCommentAfter ,
1021} from '../src-js/plugins/tokens.js' ;
1122import { resetSourceAndAst } from '../src-js/plugins/source_code.js' ;
1223import type { Node } from '../src-js/plugins/types.js' ;
@@ -585,64 +596,178 @@ describe('when calling getFirstTokens', () => {
585596describe ( 'when calling getFirstToken' , ( ) => {
586597 /* oxlint-disable-next-line no-disabled-tests expect-expect */
587598 it ( 'is to be implemented' ) ;
599+ /* oxlint-disable-next-line no-unused-expressions */
600+ getFirstToken ;
588601} ) ;
589602
603+ // https://github.com/eslint/eslint/blob/v9.39.1/tests/lib/languages/js/source-code/token-store.js#L851-L930
590604describe ( 'when calling getLastTokens' , ( ) => {
591- /* oxlint-disable-next-line no-disabled-tests expect-expect */
592- it ( 'is to be implemented' ) ;
605+ it ( "should retrieve zero tokens from the end of a node's token stream" , ( ) => {
606+ assert . deepStrictEqual (
607+ getLastTokens ( BinaryExpression , 0 ) . map ( ( token ) => token . value ) ,
608+ [ ] ,
609+ ) ;
610+ } ) ;
611+
612+ it ( "should retrieve one token from the end of a node's token stream" , ( ) => {
613+ assert . deepStrictEqual (
614+ getLastTokens ( BinaryExpression , 1 ) . map ( ( token ) => token . value ) ,
615+ [ 'b' ] ,
616+ ) ;
617+ } ) ;
618+
619+ it ( "should retrieve more than one token from the end of a node's token stream" , ( ) => {
620+ assert . deepStrictEqual (
621+ getLastTokens ( BinaryExpression , 2 ) . map ( ( token ) => token . value ) ,
622+ [ '*' , 'b' ] ,
623+ ) ;
624+ } ) ;
625+
626+ it ( "should retrieve all tokens from the end of a node's token stream" , ( ) => {
627+ assert . deepStrictEqual (
628+ getLastTokens ( BinaryExpression , 9e9 ) . map ( ( token ) => token . value ) ,
629+ [ 'a' , '*' , 'b' ] ,
630+ ) ;
631+ } ) ;
632+
633+ it ( "should retrieve more than one token from the end of a node's token stream with count option" , ( ) => {
634+ assert . deepStrictEqual (
635+ getLastTokens ( BinaryExpression , { count : 2 } ) . map ( ( token ) => token . value ) ,
636+ [ '*' , 'b' ] ,
637+ ) ;
638+ } ) ;
639+
640+ it ( "should retrieve matched tokens from the end of a node's token stream with filter option" , ( ) => {
641+ assert . deepStrictEqual (
642+ getLastTokens ( BinaryExpression , ( t ) => t . type === 'Identifier' ) . map ( ( token ) => token . value ) ,
643+ [ 'a' , 'b' ] ,
644+ ) ;
645+ assert . deepStrictEqual (
646+ getLastTokens ( BinaryExpression , {
647+ filter : ( t ) => t . type === 'Identifier' ,
648+ } ) . map ( ( token ) => token . value ) ,
649+ [ 'a' , 'b' ] ,
650+ ) ;
651+ } ) ;
652+
653+ it ( "should retrieve matched tokens from the end of a node's token stream with filter and count options" , ( ) => {
654+ assert . deepStrictEqual (
655+ getLastTokens ( BinaryExpression , {
656+ count : 1 ,
657+ filter : ( t ) => t . type === 'Identifier' ,
658+ } ) . map ( ( token ) => token . value ) ,
659+ [ 'b' ] ,
660+ ) ;
661+ } ) ;
662+
663+ it ( "should retrieve all tokens from the end of a node's token stream with includeComments option" , ( ) => {
664+ assert . deepStrictEqual (
665+ getLastTokens ( BinaryExpression , {
666+ includeComments : true ,
667+ } ) . map ( ( token ) => token . value ) ,
668+ [ 'a' , 'D' , '*' , 'b' ] ,
669+ ) ;
670+ } ) ;
671+
672+ it ( "should retrieve matched tokens from the end of a node's token stream with includeComments and count options" , ( ) => {
673+ assert . deepStrictEqual (
674+ getLastTokens ( BinaryExpression , {
675+ includeComments : true ,
676+ count : 3 ,
677+ } ) . map ( ( token ) => token . value ) ,
678+ [ 'D' , '*' , 'b' ] ,
679+ ) ;
680+ } ) ;
681+
682+ it ( "should retrieve matched tokens from the end of a node's token stream with includeComments and count and filter options" , ( ) => {
683+ assert . deepStrictEqual (
684+ getLastTokens ( BinaryExpression , {
685+ includeComments : true ,
686+ count : 3 ,
687+ filter : ( t ) => t . type !== 'Punctuator' ,
688+ } ) . map ( ( token ) => token . value ) ,
689+ [ 'a' , 'D' , 'b' ] ,
690+ ) ;
691+ } ) ;
593692} ) ;
594693
595694describe ( 'when calling getLastToken' , ( ) => {
596695 /* oxlint-disable-next-line no-disabled-tests expect-expect */
597696 it ( 'is to be implemented' ) ;
697+ /* oxlint-disable-next-line no-unused-expressions */
698+ getLastToken ;
598699} ) ;
599700
600701describe ( 'when calling getFirstTokensBetween' , ( ) => {
601702 /* oxlint-disable-next-line no-disabled-tests expect-expect */
602703 it ( 'is to be implemented' ) ;
704+ /* oxlint-disable-next-line no-unused-expressions */
705+ getFirstTokensBetween ;
603706} ) ;
604707
605708describe ( 'when calling getFirstTokenBetween' , ( ) => {
606709 /* oxlint-disable-next-line no-disabled-tests expect-expect */
607710 it ( 'is to be implemented' ) ;
711+ /* oxlint-disable-next-line no-unused-expressions */
712+ getFirstTokenBetween ;
608713} ) ;
609714
610715describe ( 'when calling getLastTokensBetween' , ( ) => {
611716 /* oxlint-disable-next-line no-disabled-tests expect-expect */
612717 it ( 'is to be implemented' ) ;
718+ /* oxlint-disable-next-line no-unused-expressions */
719+ getLastTokensBetween ;
613720} ) ;
614721
615722describe ( 'when calling getLastTokenBetween' , ( ) => {
616723 /* oxlint-disable-next-line no-disabled-tests expect-expect */
617724 it ( 'is to be implemented' ) ;
725+ /* oxlint-disable-next-line no-unused-expressions */
726+ getLastTokenBetween ;
618727} ) ;
619728
620729describe ( 'when calling getTokensBetween' , ( ) => {
621730 /* oxlint-disable-next-line no-disabled-tests expect-expect */
622731 it ( 'is to be implemented' ) ;
732+ /* oxlint-disable-next-line no-unused-expressions */
733+ getTokensBetween ;
623734} ) ;
624735
625736describe ( 'when calling getTokenByRangeStart' , ( ) => {
626737 /* oxlint-disable-next-line no-disabled-tests expect-expect */
627738 it ( 'is to be implemented' ) ;
739+ /* oxlint-disable-next-line no-unused-expressions */
740+ getTokenByRangeStart ;
628741} ) ;
629742
630743describe ( 'when calling getTokenOrCommentBefore' , ( ) => {
631744 /* oxlint-disable-next-line no-disabled-tests expect-expect */
632745 it ( 'is to be implemented' ) ;
746+ /* oxlint-disable-next-line no-unused-expressions */
747+ getTokenOrCommentBefore ;
633748} ) ;
634749
635750describe ( 'when calling getTokenOrCommentAfter' , ( ) => {
636751 /* oxlint-disable-next-line no-disabled-tests expect-expect */
637752 it ( 'is to be implemented' ) ;
753+ /* oxlint-disable-next-line no-unused-expressions */
754+ getTokenOrCommentAfter ;
638755} ) ;
639756
640757describe ( 'when calling getFirstToken & getTokenAfter' , ( ) => {
641758 /* oxlint-disable-next-line no-disabled-tests expect-expect */
642759 it ( 'is to be implemented' ) ;
760+ /* oxlint-disable-next-line no-unused-expressions */
761+ getFirstToken ;
762+ /* oxlint-disable-next-line no-unused-expressions */
763+ getTokenAfter ;
643764} ) ;
644765
645766describe ( 'when calling getLastToken & getTokenBefore' , ( ) => {
646767 /* oxlint-disable-next-line no-disabled-tests expect-expect */
647768 it ( 'is to be implemented' ) ;
769+ /* oxlint-disable-next-line no-unused-expressions */
770+ getLastToken ;
771+ /* oxlint-disable-next-line no-unused-expressions */
772+ getTokenBefore ;
648773} ) ;
0 commit comments