token-map.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 = null;
  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 && 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. if (node.start == null || node.end == null) return [];
  106. const {
  107. first,
  108. last
  109. } = this._findTokensOfNode(node, 0, this._tokens.length - 1);
  110. let low = first;
  111. const children = childrenIterator(node);
  112. if ((node.type === "ExportNamedDeclaration" || node.type === "ExportDefaultDeclaration") && node.declaration && node.declaration.type === "ClassDeclaration") {
  113. children.next();
  114. }
  115. const indexes = [];
  116. for (const child of children) {
  117. if (child == null) continue;
  118. if (child.start == null || child.end == null) continue;
  119. const childTok = this._findTokensOfNode(child, low, last);
  120. const high = childTok.first;
  121. for (let k = low; k < high; k++) indexes.push(k);
  122. low = childTok.last + 1;
  123. }
  124. for (let k = low; k <= last; k++) indexes.push(k);
  125. return indexes;
  126. }
  127. _findTokensOfNode(node, low, high) {
  128. const cached = this._tokensCache.get(node);
  129. if (cached) return cached;
  130. const first = this._findFirstTokenOfNode(node.start, low, high);
  131. const last = this._findLastTokenOfNode(node.end, first, high);
  132. this._tokensCache.set(node, {
  133. first,
  134. last
  135. });
  136. return {
  137. first,
  138. last
  139. };
  140. }
  141. _findFirstTokenOfNode(start, low, high) {
  142. while (low <= high) {
  143. const mid = high + low >> 1;
  144. if (start < this._tokens[mid].start) {
  145. high = mid - 1;
  146. } else if (start > this._tokens[mid].start) {
  147. low = mid + 1;
  148. } else {
  149. return mid;
  150. }
  151. }
  152. return low;
  153. }
  154. _findLastTokenOfNode(end, low, high) {
  155. while (low <= high) {
  156. const mid = high + low >> 1;
  157. if (end < this._tokens[mid].end) {
  158. high = mid - 1;
  159. } else if (end > this._tokens[mid].end) {
  160. low = mid + 1;
  161. } else {
  162. return mid;
  163. }
  164. }
  165. return high;
  166. }
  167. }
  168. exports.TokenMap = TokenMap;
  169. function* childrenIterator(node) {
  170. if (node.type === "TemplateLiteral") {
  171. yield node.quasis[0];
  172. for (let i = 1; i < node.quasis.length; i++) {
  173. yield node.expressions[i - 1];
  174. yield node.quasis[i];
  175. }
  176. return;
  177. }
  178. const keys = VISITOR_KEYS[node.type];
  179. for (const key of keys) {
  180. const child = node[key];
  181. if (!child) continue;
  182. if (Array.isArray(child)) {
  183. yield* child;
  184. } else {
  185. yield child;
  186. }
  187. }
  188. }
  189. //# sourceMappingURL=token-map.js.map