Ohm-Management - Projektarbeit B-ME
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.

utils.js 3.4KB

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