@@ -627,8 +627,8 @@ describe("TokenStore", () => {
627627 const tokenStore = new TokenStore ( ast . tokens , ast . comments ) ;
628628
629629 /*
630- * Actually, the first of nodes is always tokens, not comments .
631- * But I think this test case is needed for completeness.
630+ * A node must not start with a token: it can start with a comment or be empty .
631+ * This test case is needed for completeness.
632632 */
633633 const token = tokenStore . getFirstToken (
634634 { range : [ ast . comments [ 0 ] . range [ 0 ] , ast . tokens [ 5 ] . range [ 1 ] ] } ,
@@ -644,8 +644,8 @@ describe("TokenStore", () => {
644644 const tokenStore = new TokenStore ( ast . tokens , ast . comments ) ;
645645
646646 /*
647- * Actually, the first of nodes is always tokens, not comments .
648- * But I think this test case is needed for completeness.
647+ * A node must not start with a token: it can start with a comment or be empty .
648+ * This test case is needed for completeness.
649649 */
650650 const token = tokenStore . getFirstToken (
651651 { range : [ ast . comments [ 0 ] . range [ 0 ] , ast . tokens [ 5 ] . range [ 1 ] ] }
@@ -654,6 +654,38 @@ describe("TokenStore", () => {
654654 assert . strictEqual ( token . value , "c" ) ;
655655 } ) ;
656656
657+ it ( "should retrieve the first token if the root node contains a trailing comment" , ( ) => {
658+ const parser = require ( "../../fixtures/parsers/all-comments-parser" ) ;
659+ const code = "foo // comment" ;
660+ const ast = parser . parse ( code , { loc : true , range : true , tokens : true , comment : true } ) ;
661+ const tokenStore = new TokenStore ( ast . tokens , ast . comments ) ;
662+ const token = tokenStore . getFirstToken ( ast ) ;
663+
664+ assert . strictEqual ( token , ast . tokens [ 0 ] ) ;
665+ } ) ;
666+
667+ it ( "should return null if the source contains only comments" , ( ) => {
668+ const code = "// comment" ;
669+ const ast = espree . parse ( code , { loc : true , range : true , tokens : true , comment : true } ) ;
670+ const tokenStore = new TokenStore ( ast . tokens , ast . comments ) ;
671+ const token = tokenStore . getFirstToken ( ast , {
672+ filter ( ) {
673+ assert . fail ( "Unexpected call to filter callback" ) ;
674+ }
675+ } ) ;
676+
677+ assert . strictEqual ( token , null ) ;
678+ } ) ;
679+
680+ it ( "should return null if the source is empty" , ( ) => {
681+ const code = "" ;
682+ const ast = espree . parse ( code , { loc : true , range : true , tokens : true , comment : true } ) ;
683+ const tokenStore = new TokenStore ( ast . tokens , ast . comments ) ;
684+ const token = tokenStore . getFirstToken ( ast ) ;
685+
686+ assert . strictEqual ( token , null ) ;
687+ } ) ;
688+
657689 } ) ;
658690
659691 describe ( "when calling getLastTokens" , ( ) => {
@@ -814,8 +846,8 @@ describe("TokenStore", () => {
814846 const tokenStore = new TokenStore ( ast . tokens , ast . comments ) ;
815847
816848 /*
817- * Actually, the last of nodes is always tokens, not comments .
818- * But I think this test case is needed for completeness.
849+ * A node must not end with a token: it can end with a comment or be empty .
850+ * This test case is needed for completeness.
819851 */
820852 const token = tokenStore . getLastToken (
821853 { range : [ ast . tokens [ 0 ] . range [ 0 ] , ast . comments [ 0 ] . range [ 1 ] ] } ,
@@ -831,8 +863,8 @@ describe("TokenStore", () => {
831863 const tokenStore = new TokenStore ( ast . tokens , ast . comments ) ;
832864
833865 /*
834- * Actually, the last of nodes is always tokens, not comments .
835- * But I think this test case is needed for completeness.
866+ * A node must not end with a token: it can end with a comment or be empty .
867+ * This test case is needed for completeness.
836868 */
837869 const token = tokenStore . getLastToken (
838870 { range : [ ast . tokens [ 0 ] . range [ 0 ] , ast . comments [ 0 ] . range [ 1 ] ] }
@@ -841,6 +873,38 @@ describe("TokenStore", () => {
841873 assert . strictEqual ( token . value , "b" ) ;
842874 } ) ;
843875
876+ it ( "should retrieve the last token if the root node contains a trailing comment" , ( ) => {
877+ const parser = require ( "../../fixtures/parsers/all-comments-parser" ) ;
878+ const code = "foo // comment" ;
879+ const ast = parser . parse ( code , { loc : true , range : true , tokens : true , comment : true } ) ;
880+ const tokenStore = new TokenStore ( ast . tokens , ast . comments ) ;
881+ const token = tokenStore . getLastToken ( ast ) ;
882+
883+ assert . strictEqual ( token , ast . tokens [ 0 ] ) ;
884+ } ) ;
885+
886+ it ( "should return null if the source contains only comments" , ( ) => {
887+ const code = "// comment" ;
888+ const ast = espree . parse ( code , { loc : true , range : true , tokens : true , comment : true } ) ;
889+ const tokenStore = new TokenStore ( ast . tokens , ast . comments ) ;
890+ const token = tokenStore . getLastToken ( ast , {
891+ filter ( ) {
892+ assert . fail ( "Unexpected call to filter callback" ) ;
893+ }
894+ } ) ;
895+
896+ assert . strictEqual ( token , null ) ;
897+ } ) ;
898+
899+ it ( "should return null if the source is empty" , ( ) => {
900+ const code = "" ;
901+ const ast = espree . parse ( code , { loc : true , range : true , tokens : true , comment : true } ) ;
902+ const tokenStore = new TokenStore ( ast . tokens , ast . comments ) ;
903+ const token = tokenStore . getLastToken ( ast ) ;
904+
905+ assert . strictEqual ( token , null ) ;
906+ } ) ;
907+
844908 } ) ;
845909
846910 describe ( "when calling getFirstTokensBetween" , ( ) => {
0 commit comments