@@ -209,12 +209,93 @@ describe('when calling getTokensBefore', () => {
209209} ) ;
210210
211211describe ( 'when calling getTokenBefore' , ( ) => {
212- /* oxlint-disable-next-line no-disabled-tests expect-expect */
213- it ( 'is to be implemented' ) ;
214- /* oxlint-disable-next-line no-unused-expressions */
215- getTokenBefore ;
216- /* oxlint-disable-next-line no-unused-expressions */
217- resetSourceAndAst ;
212+ it ( 'should retrieve one token before a node' , ( ) => {
213+ assert . strictEqual ( getTokenBefore ( BinaryExpression ) ! . value , '=' ) ;
214+ } ) ;
215+
216+ it ( 'should skip a given number of tokens' , ( ) => {
217+ assert . strictEqual ( getTokenBefore ( BinaryExpression , 1 ) ! . value , 'answer' ) ;
218+ assert . strictEqual ( getTokenBefore ( BinaryExpression , 2 ) ! . value , 'var' ) ;
219+ } ) ;
220+
221+ it ( 'should skip a given number of tokens with skip option' , ( ) => {
222+ assert . strictEqual ( getTokenBefore ( BinaryExpression , { skip : 1 } ) ! . value , 'answer' ) ;
223+ assert . strictEqual ( getTokenBefore ( BinaryExpression , { skip : 2 } ) ! . value , 'var' ) ;
224+ } ) ;
225+
226+ it ( 'should retrieve matched token with filter option' , ( ) => {
227+ assert . strictEqual ( getTokenBefore ( BinaryExpression , ( t ) => t . value !== '=' ) ! . value , 'answer' ) ;
228+ } ) ;
229+
230+ it ( 'should retrieve matched token with skip and filter options' , ( ) => {
231+ assert . strictEqual (
232+ getTokenBefore ( BinaryExpression , {
233+ skip : 1 ,
234+ filter : ( t ) => t . value !== '=' ,
235+ } ) ! . value ,
236+ 'var' ,
237+ ) ;
238+ } ) ;
239+
240+ it ( 'should retrieve one token or comment before a node with includeComments option' , ( ) => {
241+ assert . strictEqual (
242+ getTokenBefore ( BinaryExpression , {
243+ includeComments : true ,
244+ } ) ! . value ,
245+ 'C' ,
246+ ) ;
247+ } ) ;
248+
249+ it ( 'should retrieve one token or comment before a node with includeComments and skip options' , ( ) => {
250+ assert . strictEqual (
251+ getTokenBefore ( BinaryExpression , {
252+ includeComments : true ,
253+ skip : 1 ,
254+ } ) ! . value ,
255+ '=' ,
256+ ) ;
257+ } ) ;
258+
259+ it ( 'should retrieve one token or comment before a node with includeComments and skip and filter options' , ( ) => {
260+ assert . strictEqual (
261+ getTokenBefore ( BinaryExpression , {
262+ includeComments : true ,
263+ skip : 1 ,
264+ filter : ( t ) => t . type . startsWith ( 'Block' ) ,
265+ } ) ! . value ,
266+ 'B' ,
267+ ) ;
268+ } ) ;
269+
270+ it ( 'should retrieve the previous node if the comment at the end of source code is specified.' , ( ) => {
271+ resetSourceAndAst ( ) ;
272+ sourceText = 'a + b /*comment*/' ;
273+ // TODO: this verbatim range should be replaced with `ast.comments[0]`
274+ const token = getTokenBefore ( { range : [ 6 , 17 ] } as Node ) ;
275+
276+ assert . strictEqual ( token ! . value , 'b' ) ;
277+ resetSourceAndAst ( ) ;
278+ } ) ;
279+
280+ it ( 'should retrieve the previous comment if the first token is specified.' , ( ) => {
281+ resetSourceAndAst ( ) ;
282+ sourceText = '/*comment*/ a + b' ;
283+ // TODO: this verbatim range should be replaced with `ast.tokens[0]`
284+ const token = getTokenBefore ( { range : [ 12 , 13 ] } as Node , { includeComments : true } ) ;
285+
286+ assert . strictEqual ( token ! . value , 'comment' ) ;
287+ resetSourceAndAst ( ) ;
288+ } ) ;
289+
290+ it ( 'should retrieve null if the first comment is specified.' , ( ) => {
291+ resetSourceAndAst ( ) ;
292+ sourceText = '/*comment*/ a + b' ;
293+ // TODO: this verbatim range should be replaced with `ast.comments[0]`
294+ const token = getTokenBefore ( { range : [ 0 , 11 ] } as Node , { includeComments : true } ) ;
295+
296+ assert . strictEqual ( token , null ) ;
297+ resetSourceAndAst ( ) ;
298+ } ) ;
218299} ) ;
219300
220301describe ( 'when calling getTokenAfter' , ( ) => {
0 commit comments