@@ -124,12 +124,93 @@ describe('when calling getTokensBefore', () => {
124124} ) ;
125125
126126describe ( 'when calling getTokenBefore' , ( ) => {
127- /* oxlint-disable-next-line no-disabled-tests expect-expect */
128- it ( 'is to be implemented' ) ;
129- /* oxlint-disable-next-line no-unused-expressions */
130- getTokenBefore ;
131- /* oxlint-disable-next-line no-unused-expressions */
132- resetSourceAndAst ;
127+ it ( 'should retrieve one token before a node' , ( ) => {
128+ assert . strictEqual ( getTokenBefore ( BinaryExpression ) ! . value , '=' ) ;
129+ } ) ;
130+
131+ it ( 'should skip a given number of tokens' , ( ) => {
132+ assert . strictEqual ( getTokenBefore ( BinaryExpression , 1 ) ! . value , 'answer' ) ;
133+ assert . strictEqual ( getTokenBefore ( BinaryExpression , 2 ) ! . value , 'var' ) ;
134+ } ) ;
135+
136+ it ( 'should skip a given number of tokens with skip option' , ( ) => {
137+ assert . strictEqual ( getTokenBefore ( BinaryExpression , { skip : 1 } ) ! . value , 'answer' ) ;
138+ assert . strictEqual ( getTokenBefore ( BinaryExpression , { skip : 2 } ) ! . value , 'var' ) ;
139+ } ) ;
140+
141+ it ( 'should retrieve matched token with filter option' , ( ) => {
142+ assert . strictEqual ( getTokenBefore ( BinaryExpression , ( t ) => t . value !== '=' ) ! . value , 'answer' ) ;
143+ } ) ;
144+
145+ it ( 'should retrieve matched token with skip and filter options' , ( ) => {
146+ assert . strictEqual (
147+ getTokenBefore ( BinaryExpression , {
148+ skip : 1 ,
149+ filter : ( t ) => t . value !== '=' ,
150+ } ) ! . value ,
151+ 'var' ,
152+ ) ;
153+ } ) ;
154+
155+ it ( 'should retrieve one token or comment before a node with includeComments option' , ( ) => {
156+ assert . strictEqual (
157+ getTokenBefore ( BinaryExpression , {
158+ includeComments : true ,
159+ } ) ! . value ,
160+ 'C' ,
161+ ) ;
162+ } ) ;
163+
164+ it ( 'should retrieve one token or comment before a node with includeComments and skip options' , ( ) => {
165+ assert . strictEqual (
166+ getTokenBefore ( BinaryExpression , {
167+ includeComments : true ,
168+ skip : 1 ,
169+ } ) ! . value ,
170+ '=' ,
171+ ) ;
172+ } ) ;
173+
174+ it ( 'should retrieve one token or comment before a node with includeComments and skip and filter options' , ( ) => {
175+ assert . strictEqual (
176+ getTokenBefore ( BinaryExpression , {
177+ includeComments : true ,
178+ skip : 1 ,
179+ filter : ( t ) => t . type . startsWith ( 'Block' ) ,
180+ } ) ! . value ,
181+ 'B' ,
182+ ) ;
183+ } ) ;
184+
185+ it ( 'should retrieve the previous node if the comment at the end of source code is specified.' , ( ) => {
186+ resetSourceAndAst ( ) ;
187+ sourceText = 'a + b /*comment*/' ;
188+ // TODO: this verbatim range should be replaced with `ast.comments[0]`
189+ const token = getTokenBefore ( { range : [ 6 , 17 ] } as Node ) ;
190+
191+ assert . strictEqual ( token ! . value , 'b' ) ;
192+ resetSourceAndAst ( ) ;
193+ } ) ;
194+
195+ it ( 'should retrieve the previous comment if the first token is specified.' , ( ) => {
196+ resetSourceAndAst ( ) ;
197+ sourceText = '/*comment*/ a + b' ;
198+ // TODO: this verbatim range should be replaced with `ast.tokens[0]`
199+ const token = getTokenBefore ( { range : [ 12 , 13 ] } as Node , { includeComments : true } ) ;
200+
201+ assert . strictEqual ( token ! . value , 'comment' ) ;
202+ resetSourceAndAst ( ) ;
203+ } ) ;
204+
205+ it ( 'should retrieve null if the first comment is specified.' , ( ) => {
206+ resetSourceAndAst ( ) ;
207+ sourceText = '/*comment*/ a + b' ;
208+ // TODO: this verbatim range should be replaced with `ast.comments[0]`
209+ const token = getTokenBefore ( { range : [ 0 , 11 ] } as Node , { includeComments : true } ) ;
210+
211+ assert . strictEqual ( token , null ) ;
212+ resetSourceAndAst ( ) ;
213+ } ) ;
133214} ) ;
134215
135216describe ( 'when calling getTokenAfter' , ( ) => {
0 commit comments