]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/source-code/token-store/utils.js
2 * @fileoverview Define utility functions for token store.
3 * @author Toru Nagashima
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
11 const lodash
= require("lodash");
13 //------------------------------------------------------------------------------
15 //------------------------------------------------------------------------------
18 * Gets `token.range[0]` from the given token.
19 * @param {Node|Token|Comment} token The token to get.
20 * @returns {number} The start location.
23 function getStartLocation(token
) {
24 return token
.range
[0];
27 //------------------------------------------------------------------------------
29 //------------------------------------------------------------------------------
32 * Binary-searches the index of the first token which is after the given location.
33 * If it was not found, this returns `tokens.length`.
34 * @param {(Token|Comment)[]} tokens It searches the token in this list.
35 * @param {number} location The location to search.
36 * @returns {number} The found index or `tokens.length`.
38 exports
.search
= function search(tokens
, location
) {
39 return lodash
.sortedIndexBy(
41 { range
: [location
] },
47 * Gets the index of the `startLoc` in `tokens`.
48 * `startLoc` can be the value of `node.range[1]`, so this checks about `startLoc - 1` as well.
49 * @param {(Token|Comment)[]} tokens The tokens to find an index.
50 * @param {Object} indexMap The map from locations to indices.
51 * @param {number} startLoc The location to get an index.
52 * @returns {number} The index.
54 exports
.getFirstIndex
= function getFirstIndex(tokens
, indexMap
, startLoc
) {
55 if (startLoc
in indexMap
) {
56 return indexMap
[startLoc
];
58 if ((startLoc
- 1) in indexMap
) {
59 const index
= indexMap
[startLoc
- 1];
60 const token
= (index
>= 0 && index
< tokens
.length
) ? tokens
[index
] : null;
63 * For the map of "comment's location -> token's index", it points the next token of a comment.
64 * In that case, +1 is unnecessary.
66 if (token
&& token
.range
[0] >= startLoc
) {
75 * Gets the index of the `endLoc` in `tokens`.
76 * The information of end locations are recorded at `endLoc - 1` in `indexMap`, so this checks about `endLoc - 1` as well.
77 * @param {(Token|Comment)[]} tokens The tokens to find an index.
78 * @param {Object} indexMap The map from locations to indices.
79 * @param {number} endLoc The location to get an index.
80 * @returns {number} The index.
82 exports
.getLastIndex
= function getLastIndex(tokens
, indexMap
, endLoc
) {
83 if (endLoc
in indexMap
) {
84 return indexMap
[endLoc
] - 1;
86 if ((endLoc
- 1) in indexMap
) {
87 const index
= indexMap
[endLoc
- 1];
88 const token
= (index
>= 0 && index
< tokens
.length
) ? tokens
[index
] : null;
91 * For the map of "comment's location -> token's index", it points the next token of a comment.
92 * In that case, -1 is necessary.
94 if (token
&& token
.range
[1] > endLoc
) {
99 return tokens
.length
- 1;