Skip to content

Commit 0b0d4d5

Browse files
committed
Remove getTokenOrCommentBefore/After
1 parent f57a16e commit 0b0d4d5

File tree

2 files changed

+198
-176
lines changed

2 files changed

+198
-176
lines changed

src/languages/json-source-code.js

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class JSONTraversalStep extends VisitNodeStep {
6666
* an array of comments and a map of starting token range to token index.
6767
*/
6868
function processTokens(tokens) {
69-
/** @type {Array<Token>} */
69+
/** @type {Array<Token>} */
7070
const comments = [];
7171
/** @type {Map<number, number>} */
7272
const starts = new Map();
@@ -334,87 +334,84 @@ export class JSONSourceCode extends TextSourceCodeBase {
334334
}
335335

336336
/**
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.
339341
* @returns {Token|null} The previous token or comment, or null if there is none.
340342
*/
341-
getTokenOrCommentBefore(nodeOrToken) {
342-
const { range } = nodeOrToken;
343-
const start = range[0];
344-
const tokens = this.ast.tokens;
345-
343+
getTokenBefore(nodeOrToken, { includeComments = false } = {}) {
346344
this.#ensureTokens();
347345

348-
const index = this.#tokenStarts.get(start);
346+
const index = this.#tokenStarts.get(nodeOrToken.range[0]);
347+
349348
if (index === undefined) {
350349
return null;
351350
}
352351

353-
const previousIndex = index - 1;
352+
let previousIndex = index - 1;
354353
if (previousIndex < 0) {
355354
return null;
356355
}
357356

358-
return tokens[previousIndex];
359-
}
357+
const tokens = this.ast.tokens;
358+
let tokenOrComment = tokens[previousIndex];
360359

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+
}
368363

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) {
372369
return null;
373370
}
374-
} while (tokenOrComment.type.endsWith("Comment"));
375371

372+
tokenOrComment = tokens[previousIndex];
373+
}
376374
return tokenOrComment;
377375
}
378376

379377
/**
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.
382382
* @returns {Token|null} The next token or comment, or null if there is none.
383383
*/
384-
getTokenOrCommentAfter(nodeOrToken) {
385-
const { range } = nodeOrToken;
386-
const start = range[0];
387-
const tokens = this.ast.tokens;
388-
384+
getTokenAfter(nodeOrToken, { includeComments = false } = {}) {
389385
this.#ensureTokens();
390386

391-
const index = this.#tokenStarts.get(start);
387+
const index = this.#tokenStarts.get(nodeOrToken.range[0]);
388+
392389
if (index === undefined) {
393390
return null;
394391
}
395392

396-
const nextIndex = index + 1;
393+
let nextIndex = index + 1;
394+
const tokens = this.ast.tokens;
397395
if (nextIndex >= tokens.length) {
398396
return null;
399397
}
400398

401-
return tokens[nextIndex];
402-
}
399+
let tokenOrComment = tokens[nextIndex];
403400

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+
}
411404

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) {
415410
return null;
416411
}
417-
} while (tokenOrComment.type.endsWith("Comment"));
412+
413+
tokenOrComment = tokens[nextIndex];
414+
}
418415

419416
return tokenOrComment;
420417
}

0 commit comments

Comments
 (0)