token-map.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. if (typeof test === "number") {
  58. test = String.fromCharCode(test);
  59. }
  60. let i = 0;
  61. const count = occurrenceCount;
  62. if (count > 1) {
  63. const cache = this._nodesOccurrencesCountCache.get(node);
  64. if ((cache == null ? void 0 : cache.test) === test && cache.count < count) {
  65. i = cache.i + 1;
  66. occurrenceCount -= cache.count + 1;
  67. }
  68. }
  69. for (; i < indexes.length; i++) {
  70. const tok = this._tokens[indexes[i]];
  71. if (this.matchesOriginal(tok, test)) {
  72. if (occurrenceCount === 0) {
  73. if (count > 0) {
  74. this._nodesOccurrencesCountCache.set(node, {
  75. test,
  76. count,
  77. i
  78. });
  79. }
  80. return tok;
  81. }
  82. occurrenceCount--;
  83. }
  84. }
  85. }
  86. return null;
  87. }
  88. matchesOriginal(token, test) {
  89. if (token.end - token.start !== test.length) return false;
  90. if (token.value != null) return token.value === test;
  91. return this._source.startsWith(test, token.start);
  92. }
  93. startMatches(node, test) {
  94. const indexes = this._nodesToTokenIndexes.get(node);
  95. if (!indexes) return false;
  96. const tok = this._tokens[indexes[0]];
  97. if (tok.start !== node.start) return false;
  98. return this.matchesOriginal(tok, test);
  99. }
  100. endMatches(node, test) {
  101. const indexes = this._nodesToTokenIndexes.get(node);
  102. if (!indexes) return false;
  103. const tok = this._tokens[indexes[indexes.length - 1]];
  104. if (tok.end !== node.end) return false;
  105. return this.matchesOriginal(tok, test);
  106. }
  107. _getTokensIndexesOfNode(node) {
  108. var _node$declaration;
  109. if (node.start == null || node.end == null) return [];
  110. const {
  111. first,
  112. last
  113. } = this._findTokensOfNode(node, 0, this._tokens.length - 1);
  114. let low = first;
  115. const children = childrenIterator(node);
  116. if ((node.type === "ExportNamedDeclaration" || node.type === "ExportDefaultDeclaration") && ((_node$declaration = node.declaration) == null ? void 0 : _node$declaration.type) === "ClassDeclaration") {
  117. children.next();
  118. }
  119. const indexes = [];
  120. for (const child of children) {
  121. if (child == null) continue;
  122. if (child.start == null || child.end == null) continue;
  123. const childTok = this._findTokensOfNode(child, low, last);
  124. const high = childTok.first;
  125. for (let k = low; k < high; k++) indexes.push(k);
  126. low = childTok.last + 1;
  127. }
  128. for (let k = low; k <= last; k++) indexes.push(k);
  129. return indexes;
  130. }
  131. _findTokensOfNode(node, low, high) {
  132. const cached = this._tokensCache.get(node);
  133. if (cached) return cached;
  134. const first = this._findFirstTokenOfNode(node.start, low, high);
  135. const last = this._findLastTokenOfNode(node.end, first, high);
  136. this._tokensCache.set(node, {
  137. first,
  138. last
  139. });
  140. return {
  141. first,
  142. last
  143. };
  144. }
  145. _findFirstTokenOfNode(start, low, high) {
  146. while (low <= high) {
  147. const mid = high + low >> 1;
  148. if (start < this._tokens[mid].start) {
  149. high = mid - 1;
  150. } else if (start > this._tokens[mid].start) {
  151. low = mid + 1;
  152. } else {
  153. return mid;
  154. }
  155. }
  156. return low;
  157. }
  158. _findLastTokenOfNode(end, low, high) {
  159. while (low <= high) {
  160. const mid = high + low >> 1;
  161. if (end < this._tokens[mid].end) {
  162. high = mid - 1;
  163. } else if (end > this._tokens[mid].end) {
  164. low = mid + 1;
  165. } else {
  166. return mid;
  167. }
  168. }
  169. return high;
  170. }
  171. }
  172. exports.TokenMap = TokenMap;
  173. function* childrenIterator(node) {
  174. if (node.type === "TemplateLiteral") {
  175. yield node.quasis[0];
  176. for (let i = 1; i < node.quasis.length; i++) {
  177. yield node.expressions[i - 1];
  178. yield node.quasis[i];
  179. }
  180. return;
  181. }
  182. const keys = VISITOR_KEYS[node.type];
  183. for (const key of keys) {
  184. const child = node[key];
  185. if (!child) continue;
  186. if (Array.isArray(child)) {
  187. yield* child;
  188. } else {
  189. yield child;
  190. }
  191. }
  192. }
  193. //# sourceMappingURL=token-map.js.map