summaryrefslogtreecommitdiff
path: root/tools/node_modules/eslint/lib/source-code/token-store/utils.js
diff options
context:
space:
mode:
Diffstat (limited to 'tools/node_modules/eslint/lib/source-code/token-store/utils.js')
-rw-r--r--tools/node_modules/eslint/lib/source-code/token-store/utils.js37
1 files changed, 21 insertions, 16 deletions
diff --git a/tools/node_modules/eslint/lib/source-code/token-store/utils.js b/tools/node_modules/eslint/lib/source-code/token-store/utils.js
index 859831916e..3e01470321 100644
--- a/tools/node_modules/eslint/lib/source-code/token-store/utils.js
+++ b/tools/node_modules/eslint/lib/source-code/token-store/utils.js
@@ -5,20 +5,6 @@
"use strict";
//------------------------------------------------------------------------------
-// Helpers
-//------------------------------------------------------------------------------
-
-/**
- * Gets `token.range[0]` from the given token.
- * @param {Node|Token|Comment} token The token to get.
- * @returns {number} The start location.
- * @private
- */
-function getStartLocation(token) {
- return token.range[0];
-}
-
-//------------------------------------------------------------------------------
// Exports
//------------------------------------------------------------------------------
@@ -30,9 +16,28 @@ function getStartLocation(token) {
* @returns {number} The found index or `tokens.length`.
*/
exports.search = function search(tokens, location) {
- const index = tokens.findIndex(el => location <= getStartLocation(el));
+ for (let minIndex = 0, maxIndex = tokens.length - 1; minIndex <= maxIndex;) {
- return index === -1 ? tokens.length : index;
+ /*
+ * Calculate the index in the middle between minIndex and maxIndex.
+ * `| 0` is used to round a fractional value down to the nearest integer: this is similar to
+ * using `Math.trunc()` or `Math.floor()`, but performance tests have shown this method to
+ * be faster.
+ */
+ const index = (minIndex + maxIndex) / 2 | 0;
+ const token = tokens[index];
+ const tokenStartLocation = token.range[0];
+
+ if (location <= tokenStartLocation) {
+ if (index === minIndex) {
+ return index;
+ }
+ maxIndex = index;
+ } else {
+ minIndex = index + 1;
+ }
+ }
+ return tokens.length;
};
/**