token-map.js.map 14 KB

1
  1. {"version":3,"names":["_t","require","traverseFast","VISITOR_KEYS","TokenMap","constructor","ast","tokens","source","_tokens","_source","_nodesToTokenIndexes","Map","_nodesOccurrencesCountCache","_tokensCache","node","indexes","_getTokensIndexesOfNode","length","set","has","getIndexes","get","find","condition","k","index","tok","findLastIndex","findMatching","test","occurrenceCount","i","count","cache","matchesOriginal","token","end","start","value","startsWith","startMatches","endMatches","first","last","_findTokensOfNode","low","children","childrenIterator","type","declaration","next","child","childTok","high","push","cached","_findFirstTokenOfNode","_findLastTokenOfNode","mid","exports","quasis","expressions","keys","key","Array","isArray"],"sources":["../src/token-map.ts"],"sourcesContent":["import type * as t from \"@babel/types\";\nimport type { Token } from \"@babel/parser\";\n\nimport { traverseFast, VISITOR_KEYS } from \"@babel/types\";\n\nexport class TokenMap {\n _tokens: Token[];\n _source: string;\n\n _nodesToTokenIndexes: Map<t.Node, number[]> = new Map();\n _nodesOccurrencesCountCache: Map<\n t.Node,\n { test: string; count: number; i: number }\n > = new Map();\n\n _tokensCache = new Map<t.Node, { first: number; last: number }>();\n\n constructor(ast: t.Node, tokens: Token[], source: string) {\n this._tokens = tokens;\n this._source = source;\n\n traverseFast(ast, node => {\n const indexes = this._getTokensIndexesOfNode(node);\n if (indexes.length > 0) this._nodesToTokenIndexes.set(node, indexes);\n });\n\n this._tokensCache = null;\n }\n\n has(node: t.Node): boolean {\n return this._nodesToTokenIndexes.has(node);\n }\n\n getIndexes(node: t.Node): readonly number[] | undefined {\n return this._nodesToTokenIndexes.get(node);\n }\n\n find(\n node: t.Node,\n condition: (token: Token, index: number) => boolean,\n ): Token | null {\n const indexes = this._nodesToTokenIndexes.get(node);\n if (indexes) {\n for (let k = 0; k < indexes.length; k++) {\n const index = indexes[k];\n const tok = this._tokens[index];\n if (condition(tok, index)) return tok;\n }\n }\n return null;\n }\n\n findLastIndex(\n node: t.Node,\n condition: (token: Token, index: number) => boolean,\n ): number {\n const indexes = this._nodesToTokenIndexes.get(node);\n if (indexes) {\n for (let k = indexes.length - 1; k >= 0; k--) {\n const index = indexes[k];\n const tok = this._tokens[index];\n if (condition(tok, index)) return index;\n }\n }\n return -1;\n }\n\n findMatching(\n node: t.Node,\n test: string,\n occurrenceCount: number = 0,\n ): Token | null {\n const indexes = this._nodesToTokenIndexes.get(node);\n if (indexes) {\n let i = 0;\n const count = occurrenceCount;\n\n // To avoid O(n^2) search when printing lists (such as arrays), we\n // cache the last index of a given token for a given occurrence count.\n // If then we are asked to find the next occurrence of the same token,\n // we start from the index of the previously found token.\n // This cache only kicks in after 2 tokens of the same type, to avoid\n // overhead in the simple case of having unique tokens per node.\n if (count > 1) {\n const cache = this._nodesOccurrencesCountCache.get(node);\n if (cache && cache.test === test && cache.count < count) {\n i = cache.i + 1;\n occurrenceCount -= cache.count + 1;\n }\n }\n\n for (; i < indexes.length; i++) {\n const tok = this._tokens[indexes[i]];\n if (this.matchesOriginal(tok, test)) {\n if (occurrenceCount === 0) {\n if (count > 0) {\n this._nodesOccurrencesCountCache.set(node, { test, count, i });\n }\n return tok;\n }\n occurrenceCount--;\n }\n }\n }\n return null;\n }\n\n matchesOriginal(token: Token, test: string) {\n if (token.end - token.start !== test.length) return false;\n if (token.value != null) return token.value === test;\n return this._source.startsWith(test, token.start);\n }\n\n startMatches(node: t.Node, test: string): boolean {\n const indexes = this._nodesToTokenIndexes.get(node);\n if (!indexes) return false;\n const tok = this._tokens[indexes[0]];\n if (tok.start !== node.start) return false;\n return this.matchesOriginal(tok, test);\n }\n\n endMatches(node: t.Node, test: string): boolean {\n const indexes = this._nodesToTokenIndexes.get(node);\n if (!indexes) return false;\n const tok = this._tokens[indexes[indexes.length - 1]];\n if (tok.end !== node.end) return false;\n return this.matchesOriginal(tok, test);\n }\n\n _getTokensIndexesOfNode(node: t.Node): number[] {\n if (node.start == null || node.end == null) return [];\n\n const { first, last } = this._findTokensOfNode(\n node,\n 0,\n this._tokens.length - 1,\n );\n\n let low = first;\n\n const children = childrenIterator(node);\n\n if (\n (node.type === \"ExportNamedDeclaration\" ||\n node.type === \"ExportDefaultDeclaration\") &&\n node.declaration &&\n node.declaration.type === \"ClassDeclaration\"\n ) {\n // Exported class declarations can be not properly nested inside\n // the export declaration that contains them. For example, in\n // `@dec export class Foo {}` the `export` is covered by the\n // ClassDeclaration range. Skip the class declaration from the list\n // of children to skip, so that when looking for `export` we also\n // traverse its tokens.\n children.next();\n }\n\n const indexes = [];\n\n for (const child of children) {\n if (child == null) continue;\n if (child.start == null || child.end == null) continue;\n\n const childTok = this._findTokensOfNode(child, low, last);\n\n const high = childTok.first;\n for (let k = low; k < high; k++) indexes.push(k);\n\n low = childTok.last + 1;\n }\n\n for (let k = low; k <= last; k++) indexes.push(k);\n\n return indexes;\n }\n\n _findTokensOfNode(node: t.Node, low: number, high: number) {\n const cached = this._tokensCache.get(node);\n if (cached) return cached;\n\n const first = this._findFirstTokenOfNode(node.start, low, high);\n const last = this._findLastTokenOfNode(node.end, first, high);\n\n this._tokensCache.set(node, { first, last });\n return { first, last };\n }\n\n _findFirstTokenOfNode(start: number, low: number, high: number): number {\n while (low <= high) {\n const mid = (high + low) >> 1;\n if (start < this._tokens[mid].start) {\n high = mid - 1;\n } else if (start > this._tokens[mid].start) {\n low = mid + 1;\n } else {\n return mid;\n }\n }\n return low;\n }\n\n _findLastTokenOfNode(end: number, low: number, high: number): number {\n while (low <= high) {\n const mid = (high + low) >> 1;\n if (end < this._tokens[mid].end) {\n high = mid - 1;\n } else if (end > this._tokens[mid].end) {\n low = mid + 1;\n } else {\n return mid;\n }\n }\n return high;\n }\n}\n\nfunction* childrenIterator(node: t.Node) {\n // We need special handling to iterate TemplateLiteral\n // children in order, since the two lists are interleaved.\n if (node.type === \"TemplateLiteral\") {\n yield node.quasis[0];\n for (let i = 1; i < node.quasis.length; i++) {\n yield node.expressions[i - 1];\n yield node.quasis[i];\n }\n return;\n }\n\n const keys = VISITOR_KEYS[node.type];\n for (const key of keys) {\n const child = (node as any)[key];\n if (!child) continue;\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