You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
3.3 KiB

4 years ago
  1. /**
  2. * @fileoverview Define utility functions for token store.
  3. * @author Toru Nagashima
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const lodash = require("lodash");
  10. //------------------------------------------------------------------------------
  11. // Helpers
  12. //------------------------------------------------------------------------------
  13. /**
  14. * Gets `token.range[0]` from the given token.
  15. * @param {Node|Token|Comment} token The token to get.
  16. * @returns {number} The start location.
  17. * @private
  18. */
  19. function getStartLocation(token) {
  20. return token.range[0];
  21. }
  22. //------------------------------------------------------------------------------
  23. // Exports
  24. //------------------------------------------------------------------------------
  25. /**
  26. * Binary-searches the index of the first token which is after the given location.
  27. * If it was not found, this returns `tokens.length`.
  28. * @param {(Token|Comment)[]} tokens It searches the token in this list.
  29. * @param {number} location The location to search.
  30. * @returns {number} The found index or `tokens.length`.
  31. */
  32. exports.search = function search(tokens, location) {
  33. return lodash.sortedIndexBy(
  34. tokens,
  35. { range: [location] },
  36. getStartLocation
  37. );
  38. };
  39. /**
  40. * Gets the index of the `startLoc` in `tokens`.
  41. * `startLoc` can be the value of `node.range[1]`, so this checks about `startLoc - 1` as well.
  42. * @param {(Token|Comment)[]} tokens The tokens to find an index.
  43. * @param {Object} indexMap The map from locations to indices.
  44. * @param {number} startLoc The location to get an index.
  45. * @returns {number} The index.
  46. */
  47. exports.getFirstIndex = function getFirstIndex(tokens, indexMap, startLoc) {
  48. if (startLoc in indexMap) {
  49. return indexMap[startLoc];
  50. }
  51. if ((startLoc - 1) in indexMap) {
  52. const index = indexMap[startLoc - 1];
  53. const token = (index >= 0 && index < tokens.length) ? tokens[index] : null;
  54. /*
  55. * For the map of "comment's location -> token's index", it points the next token of a comment.
  56. * In that case, +1 is unnecessary.
  57. */
  58. if (token && token.range[0] >= startLoc) {
  59. return index;
  60. }
  61. return index + 1;
  62. }
  63. return 0;
  64. };
  65. /**
  66. * Gets the index of the `endLoc` in `tokens`.
  67. * The information of end locations are recorded at `endLoc - 1` in `indexMap`, so this checks about `endLoc - 1` as well.
  68. * @param {(Token|Comment)[]} tokens The tokens to find an index.
  69. * @param {Object} indexMap The map from locations to indices.
  70. * @param {number} endLoc The location to get an index.
  71. * @returns {number} The index.
  72. */
  73. exports.getLastIndex = function getLastIndex(tokens, indexMap, endLoc) {
  74. if (endLoc in indexMap) {
  75. return indexMap[endLoc] - 1;
  76. }
  77. if ((endLoc - 1) in indexMap) {
  78. const index = indexMap[endLoc - 1];
  79. const token = (index >= 0 && index < tokens.length) ? tokens[index] : null;
  80. /*
  81. * For the map of "comment's location -> token's index", it points the next token of a comment.
  82. * In that case, -1 is necessary.
  83. */
  84. if (token && token.range[1] > endLoc) {
  85. return index - 1;
  86. }
  87. return index;
  88. }
  89. return tokens.length - 1;
  90. };