2 * @fileoverview Define the cursor which iterates tokens and comments in reverse.
3 * @author Toru Nagashima
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
11 const Cursor = require("./cursor");
12 const utils = require("./utils");
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
19 * The cursor which iterates tokens and comments in reverse.
21 module.exports = class BackwardTokenCommentCursor extends Cursor {
24 * Initializes this cursor.
25 * @param {Token[]} tokens The array of tokens.
26 * @param {Comment[]} comments The array of comments.
27 * @param {Object} indexMap The map from locations to indices in `tokens`.
28 * @param {number} startLoc The start location of the iteration range.
29 * @param {number} endLoc The end location of the iteration range.
31 constructor(tokens, comments, indexMap, startLoc, endLoc) {
34 this.comments = comments;
35 this.tokenIndex = utils.getLastIndex(tokens, indexMap, endLoc);
36 this.commentIndex = utils.search(comments, endLoc) - 1;
37 this.border = startLoc;
42 const token = (this.tokenIndex >= 0) ? this.tokens[this.tokenIndex] : null;
43 const comment = (this.commentIndex >= 0) ? this.comments[this.commentIndex] : null;
45 if (token && (!comment || token.range[1] > comment.range[1])) {
49 this.current = comment;
50 this.commentIndex -= 1;
55 return Boolean(this.current) && (this.border === -1 || this.current.range[0] >= this.border);