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