@@ -627,8 +627,8 @@ describe("TokenStore", () => {
627
627
const tokenStore = new TokenStore ( ast . tokens , ast . comments ) ;
628
628
629
629
/*
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.
632
632
*/
633
633
const token = tokenStore . getFirstToken (
634
634
{ range : [ ast . comments [ 0 ] . range [ 0 ] , ast . tokens [ 5 ] . range [ 1 ] ] } ,
@@ -644,8 +644,8 @@ describe("TokenStore", () => {
644
644
const tokenStore = new TokenStore ( ast . tokens , ast . comments ) ;
645
645
646
646
/*
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.
649
649
*/
650
650
const token = tokenStore . getFirstToken (
651
651
{ range : [ ast . comments [ 0 ] . range [ 0 ] , ast . tokens [ 5 ] . range [ 1 ] ] }
@@ -654,6 +654,38 @@ describe("TokenStore", () => {
654
654
assert . strictEqual ( token . value , "c" ) ;
655
655
} ) ;
656
656
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
+
657
689
} ) ;
658
690
659
691
describe ( "when calling getLastTokens" , ( ) => {
@@ -814,8 +846,8 @@ describe("TokenStore", () => {
814
846
const tokenStore = new TokenStore ( ast . tokens , ast . comments ) ;
815
847
816
848
/*
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.
819
851
*/
820
852
const token = tokenStore . getLastToken (
821
853
{ range : [ ast . tokens [ 0 ] . range [ 0 ] , ast . comments [ 0 ] . range [ 1 ] ] } ,
@@ -831,8 +863,8 @@ describe("TokenStore", () => {
831
863
const tokenStore = new TokenStore ( ast . tokens , ast . comments ) ;
832
864
833
865
/*
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.
836
868
*/
837
869
const token = tokenStore . getLastToken (
838
870
{ range : [ ast . tokens [ 0 ] . range [ 0 ] , ast . comments [ 0 ] . range [ 1 ] ] }
@@ -841,6 +873,38 @@ describe("TokenStore", () => {
841
873
assert . strictEqual ( token . value , "b" ) ;
842
874
} ) ;
843
875
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
+
844
908
} ) ;
845
909
846
910
describe ( "when calling getFirstTokensBetween" , ( ) => {
0 commit comments