@@ -66,7 +66,7 @@ class JSONTraversalStep extends VisitNodeStep {
66
66
* an array of comments and a map of starting token range to token index.
67
67
*/
68
68
function processTokens ( tokens ) {
69
- /** @type {Array<Token> } */
69
+ /** @type {Array<Token> } */
70
70
const comments = [ ] ;
71
71
/** @type {Map<number, number> } */
72
72
const starts = new Map ( ) ;
@@ -334,87 +334,84 @@ export class JSONSourceCode extends TextSourceCodeBase {
334
334
}
335
335
336
336
/**
337
- * Gets the token or comment before the given node or token.
338
- * @param {AnyNode|Token } nodeOrToken The node or token to get the previous token or comment for.
337
+ * Gets the token before the given node or token, optionally including comments.
338
+ * @param {AnyNode|Token } nodeOrToken The node or token to get the previous token for.
339
+ * @param {Object } [options] Options object.
340
+ * @param {boolean } [options.includeComments] If true, return comments when they are present.
339
341
* @returns {Token|null } The previous token or comment, or null if there is none.
340
342
*/
341
- getTokenOrCommentBefore ( nodeOrToken ) {
342
- const { range } = nodeOrToken ;
343
- const start = range [ 0 ] ;
344
- const tokens = this . ast . tokens ;
345
-
343
+ getTokenBefore ( nodeOrToken , { includeComments = false } = { } ) {
346
344
this . #ensureTokens( ) ;
347
345
348
- const index = this . #tokenStarts. get ( start ) ;
346
+ const index = this . #tokenStarts. get ( nodeOrToken . range [ 0 ] ) ;
347
+
349
348
if ( index === undefined ) {
350
349
return null ;
351
350
}
352
351
353
- const previousIndex = index - 1 ;
352
+ let previousIndex = index - 1 ;
354
353
if ( previousIndex < 0 ) {
355
354
return null ;
356
355
}
357
356
358
- return tokens [ previousIndex ] ;
359
- }
357
+ const tokens = this . ast . tokens ;
358
+ let tokenOrComment = tokens [ previousIndex ] ;
360
359
361
- /**
362
- * Gets the token before the given node or token, skipping any comments.
363
- * @param {AnyNode|Token } nodeOrToken The node or token to get the previous token for.
364
- * @returns {Token|null } The previous token, or null if there is none.
365
- */
366
- getTokenBefore ( nodeOrToken ) {
367
- let tokenOrComment = nodeOrToken ;
360
+ if ( includeComments ) {
361
+ return tokenOrComment ;
362
+ }
368
363
369
- do {
370
- tokenOrComment = this . getTokenOrCommentBefore ( tokenOrComment ) ;
371
- if ( ! tokenOrComment ) {
364
+ // skip comments
365
+ while ( tokenOrComment ?. type . endsWith ( "Comment" ) ) {
366
+ previousIndex -- ;
367
+
368
+ if ( previousIndex < 0 ) {
372
369
return null ;
373
370
}
374
- } while ( tokenOrComment . type . endsWith ( "Comment" ) ) ;
375
371
372
+ tokenOrComment = tokens [ previousIndex ] ;
373
+ }
376
374
return tokenOrComment ;
377
375
}
378
376
379
377
/**
380
- * Gets the token or comment after the given node or token.
381
- * @param {AnyNode|Token } nodeOrToken The node or token to get the next token or comment for.
378
+ * Gets the token after the given node or token, skipping any comments unless includeComments is true.
379
+ * @param {AnyNode|Token } nodeOrToken The node or token to get the next token for.
380
+ * @param {Object } [options] Options object.
381
+ * @param {boolean } [options.includeComments=false] If true, return comments when they are present.
382
382
* @returns {Token|null } The next token or comment, or null if there is none.
383
383
*/
384
- getTokenOrCommentAfter ( nodeOrToken ) {
385
- const { range } = nodeOrToken ;
386
- const start = range [ 0 ] ;
387
- const tokens = this . ast . tokens ;
388
-
384
+ getTokenAfter ( nodeOrToken , { includeComments = false } = { } ) {
389
385
this . #ensureTokens( ) ;
390
386
391
- const index = this . #tokenStarts. get ( start ) ;
387
+ const index = this . #tokenStarts. get ( nodeOrToken . range [ 0 ] ) ;
388
+
392
389
if ( index === undefined ) {
393
390
return null ;
394
391
}
395
392
396
- const nextIndex = index + 1 ;
393
+ let nextIndex = index + 1 ;
394
+ const tokens = this . ast . tokens ;
397
395
if ( nextIndex >= tokens . length ) {
398
396
return null ;
399
397
}
400
398
401
- return tokens [ nextIndex ] ;
402
- }
399
+ let tokenOrComment = tokens [ nextIndex ] ;
403
400
404
- /**
405
- * Gets the token after the given node or token, skipping any comments.
406
- * @param {AnyNode|Token } nodeOrToken The node or token to get the next token for.
407
- * @returns {Token|null } The next token, or null if there is none.
408
- */
409
- getTokenAfter ( nodeOrToken ) {
410
- let tokenOrComment = nodeOrToken ;
401
+ if ( includeComments ) {
402
+ return tokenOrComment ;
403
+ }
411
404
412
- do {
413
- tokenOrComment = this . getTokenOrCommentAfter ( tokenOrComment ) ;
414
- if ( ! tokenOrComment ) {
405
+ // skip comments
406
+ while ( tokenOrComment ?. type . endsWith ( "Comment" ) ) {
407
+ nextIndex ++ ;
408
+
409
+ if ( nextIndex >= tokens . length ) {
415
410
return null ;
416
411
}
417
- } while ( tokenOrComment . type . endsWith ( "Comment" ) ) ;
412
+
413
+ tokenOrComment = tokens [ nextIndex ] ;
414
+ }
418
415
419
416
return tokenOrComment ;
420
417
}
0 commit comments