token-map.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.TokenMap = void 0;
  6. var _t = require("@babel/types");
  7. const {
  8. traverseFast,
  9. VISITOR_KEYS
  10. } = _t;
  11. class TokenMap {
  12. constructor(ast, tokens, source) {
  13. this._tokens = void 0;
  14. this._source = void 0;
  15. this._nodesToTokenIndexes = new Map();
  16. this._nodesOccurrencesCountCache = new Map();
  17. this._tokensCache = new Map();
  18. this._tokens = tokens;
  19. this._source = source;
  20. traverseFast(ast, node => {
  21. const indexes = this._getTokensIndexesOfNode(node);
  22. if (indexes.length > 0) this._nodesToTokenIndexes.set(node, indexes);
  23. });
  24. this._tokensCache.clear();
  25. }
  26. has(node) {
  27. return this._nodesToTokenIndexes.has(node);
  28. }
  29. getIndexes(node) {
  30. return this._nodesToTokenIndexes.get(node);
  31. }
  32. find(node, condition) {
  33. const indexes = this._nodesToTokenIndexes.get(node);
  34. if (indexes) {
  35. for (let k = 0; k < indexes.length; k++) {
  36. const index = indexes[k];
  37. const tok = this._tokens[index];
  38. if (condition(tok, index)) return tok;
  39. }
  40. }
  41. return null;
  42. }
  43. findLastIndex(node, condition) {
  44. const indexes = this._nodesToTokenIndexes.get(node);
  45. if (indexes) {
  46. for (let k = indexes.length - 1; k >= 0; k--) {
  47. const index = indexes[k];
  48. const tok = this._tokens[index];
  49. if (condition(tok, index)) return index;
  50. }
  51. }
  52. return -1;
  53. }
  54. findMatching(node, test, occurrenceCount = 0) {
  55. const indexes = this._nodesToTokenIndexes.get(node);
  56. if (indexes) {
  57. let i = 0;
  58. const count = occurrenceCount;
  59. if (count > 1) {
  60. const cache = this._nodesOccurrencesCountCache.get(node);
  61. if ((cache == null ? void 0 : cache.test) === test && cache.count < count) {
  62. i = cache.i + 1;
  63. occurrenceCount -= cache.count + 1;
  64. }
  65. }
  66. for (; i < indexes.length; i++) {
  67. const tok = this._tokens[indexes[i]];
  68. if (this.matchesOriginal(tok, test)) {
  69. if (occurrenceCount === 0) {
  70. if (count > 0) {
  71. this._nodesOccurrencesCountCache.set(node, {
  72. test,
  73. count,
  74. i
  75. });
  76. }
  77. return tok;
  78. }
  79. occurrenceCount--;
  80. }
  81. }
  82. }
  83. return null;
  84. }
  85. matchesOriginal(token, test) {
  86. if (token.end - token.start !== test.length) return false;
  87. if (token.value != null) return token.value === test;
  88. return this._source.startsWith(test, token.start);
  89. }
  90. startMatches(node, test) {
  91. const indexes = this._nodesToTokenIndexes.get(node);
  92. if (!indexes) return false;
  93. const tok = this._tokens[indexes[0]];
  94. if (tok.start !== node.start) return false;
  95. return this.matchesOriginal(tok, test);
  96. }
  97. endMatches(node, test) {
  98. const indexes = this._nodesToTokenIndexes.get(node);
  99. if (!indexes) return false;
  100. const tok = this._tokens[indexes[indexes.length - 1]];
  101. if (tok.end !== node.end) return false;
  102. return this.matchesOriginal(tok, test);
  103. }
  104. _getTokensIndexesOfNode(node) {
  105. var _node$declaration;
  106. if (node.start == null || node.end == null) return [];
  107. const {
  108. first,
  109. last
  110. } = this._findTokensOfNode(node, 0, this._tokens.length - 1);
  111. let low = first;
  112. const children = childrenIterator(node);
  113. if ((node.type === "ExportNamedDeclaration" || node.type === "ExportDefaultDeclaration") && ((_node$declaration = node.declaration) == null ? void 0 : _node$declaration.type) === "ClassDeclaration") {
  114. children.next();
  115. }
  116. const indexes = [];
  117. for (const child of children) {
  118. if (child == null) continue;
  119. if (child.start == null || child.end == null) continue;
  120. const childTok = this._findTokensOfNode(child, low, last);
  121. const high = childTok.first;
  122. for (let k = low; k < high; k++) indexes.push(k);
  123. low = childTok.last + 1;
  124. }
  125. for (let k = low; k <= last; k++) indexes.push(k);
  126. return indexes;
  127. }
  128. _findTokensOfNode(node, low, high) {
  129. const cached = this._tokensCache.get(node);
  130. if (cached) return cached;
  131. const first = this._findFirstTokenOfNode(node.start, low, high);
  132. const last = this._findLastTokenOfNode(node.end, first, high);
  133. this._tokensCache.set(node, {
  134. first,
  135. last
  136. });
  137. return {
  138. first,
  139. last
  140. };
  141. }
  142. _findFirstTokenOfNode(start, low, high) {
  143. while (low <= high) {
  144. const mid = high + low >> 1;
  145. if (start < this._tokens[mid].start) {
  146. high = mid - 1;
  147. } else if (start > this._tokens[mid].start) {
  148. low = mid + 1;
  149. } else {
  150. return mid;
  151. }
  152. }
  153. return low;
  154. }
  155. _findLastTokenOfNode(end, low, high) {
  156. while (low <= high) {
  157. const mid = high + low >> 1;
  158. if (end < this._tokens[mid].end) {
  159. high = mid - 1;
  160. } else if (end > this._tokens[mid].end) {
  161. low = mid + 1;
  162. } else {
  163. return mid;
  164. }
  165. }
  166. return high;
  167. }
  168. }
  169. exports.TokenMap = TokenMap;
  170. function* childrenIterator(node) {
  171. if (node.type === "TemplateLiteral") {
  172. yield node.quasis[0];
  173. for (let i = 1; i < node.quasis.length; i++) {
  174. yield node.expressions[i - 1];
  175. yield node.quasis[i];
  176. }
  177. return;
  178. }
  179. const keys = VISITOR_KEYS[node.type];
  180. for (const key of keys) {
  181. const child = node[key];
  182. if (!child) continue;
  183. if (Array.isArray(child)) {
  184. yield* child;
  185. } else {
  186. yield child;
  187. }
  188. }
  189. }
  190. //# sourceMappingURL=token-map.js.map