utils.js 3.4 KB

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