[0c6b92a] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.TokenMap = void 0;
|
---|
| 7 | var _t = require("@babel/types");
|
---|
| 8 | const {
|
---|
| 9 | traverseFast,
|
---|
| 10 | VISITOR_KEYS
|
---|
| 11 | } = _t;
|
---|
| 12 | class TokenMap {
|
---|
| 13 | constructor(ast, tokens, source) {
|
---|
| 14 | this._tokens = void 0;
|
---|
| 15 | this._source = void 0;
|
---|
| 16 | this._nodesToTokenIndexes = new Map();
|
---|
| 17 | this._nodesOccurrencesCountCache = new Map();
|
---|
| 18 | this._tokensCache = new Map();
|
---|
| 19 | this._tokens = tokens;
|
---|
| 20 | this._source = source;
|
---|
| 21 | traverseFast(ast, node => {
|
---|
| 22 | const indexes = this._getTokensIndexesOfNode(node);
|
---|
| 23 | if (indexes.length > 0) this._nodesToTokenIndexes.set(node, indexes);
|
---|
| 24 | });
|
---|
| 25 | this._tokensCache = null;
|
---|
| 26 | }
|
---|
| 27 | has(node) {
|
---|
| 28 | return this._nodesToTokenIndexes.has(node);
|
---|
| 29 | }
|
---|
| 30 | getIndexes(node) {
|
---|
| 31 | return this._nodesToTokenIndexes.get(node);
|
---|
| 32 | }
|
---|
| 33 | find(node, condition) {
|
---|
| 34 | const indexes = this._nodesToTokenIndexes.get(node);
|
---|
| 35 | if (indexes) {
|
---|
| 36 | for (let k = 0; k < indexes.length; k++) {
|
---|
| 37 | const index = indexes[k];
|
---|
| 38 | const tok = this._tokens[index];
|
---|
| 39 | if (condition(tok, index)) return tok;
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
| 42 | return null;
|
---|
| 43 | }
|
---|
| 44 | findLastIndex(node, condition) {
|
---|
| 45 | const indexes = this._nodesToTokenIndexes.get(node);
|
---|
| 46 | if (indexes) {
|
---|
| 47 | for (let k = indexes.length - 1; k >= 0; k--) {
|
---|
| 48 | const index = indexes[k];
|
---|
| 49 | const tok = this._tokens[index];
|
---|
| 50 | if (condition(tok, index)) return index;
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
| 53 | return -1;
|
---|
| 54 | }
|
---|
| 55 | findMatching(node, test, occurrenceCount = 0) {
|
---|
| 56 | const indexes = this._nodesToTokenIndexes.get(node);
|
---|
| 57 | if (indexes) {
|
---|
| 58 | let i = 0;
|
---|
| 59 | const count = occurrenceCount;
|
---|
| 60 | if (count > 1) {
|
---|
| 61 | const cache = this._nodesOccurrencesCountCache.get(node);
|
---|
| 62 | if (cache && cache.test === test && cache.count < count) {
|
---|
| 63 | i = cache.i + 1;
|
---|
| 64 | occurrenceCount -= cache.count + 1;
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 | for (; i < indexes.length; i++) {
|
---|
| 68 | const tok = this._tokens[indexes[i]];
|
---|
| 69 | if (this.matchesOriginal(tok, test)) {
|
---|
| 70 | if (occurrenceCount === 0) {
|
---|
| 71 | if (count > 0) {
|
---|
| 72 | this._nodesOccurrencesCountCache.set(node, {
|
---|
| 73 | test,
|
---|
| 74 | count,
|
---|
| 75 | i
|
---|
| 76 | });
|
---|
| 77 | }
|
---|
| 78 | return tok;
|
---|
| 79 | }
|
---|
| 80 | occurrenceCount--;
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 | return null;
|
---|
| 85 | }
|
---|
| 86 | matchesOriginal(token, test) {
|
---|
| 87 | if (token.end - token.start !== test.length) return false;
|
---|
| 88 | if (token.value != null) return token.value === test;
|
---|
| 89 | return this._source.startsWith(test, token.start);
|
---|
| 90 | }
|
---|
| 91 | startMatches(node, test) {
|
---|
| 92 | const indexes = this._nodesToTokenIndexes.get(node);
|
---|
| 93 | if (!indexes) return false;
|
---|
| 94 | const tok = this._tokens[indexes[0]];
|
---|
| 95 | if (tok.start !== node.start) return false;
|
---|
| 96 | return this.matchesOriginal(tok, test);
|
---|
| 97 | }
|
---|
| 98 | endMatches(node, test) {
|
---|
| 99 | const indexes = this._nodesToTokenIndexes.get(node);
|
---|
| 100 | if (!indexes) return false;
|
---|
| 101 | const tok = this._tokens[indexes[indexes.length - 1]];
|
---|
| 102 | if (tok.end !== node.end) return false;
|
---|
| 103 | return this.matchesOriginal(tok, test);
|
---|
| 104 | }
|
---|
| 105 | _getTokensIndexesOfNode(node) {
|
---|
| 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.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 |
|
---|
| 191 | //# sourceMappingURL=token-map.js.map
|
---|