|
| 1 | +import * as es from 'estree' |
| 2 | +import { isImportDeclaration, getModuleDeclarationSource } from './utils'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Python style dictionary |
| 6 | + */ |
| 7 | +export default class Dict<K, V> { |
| 8 | + constructor(private readonly internalMap = new Map<K, V>()) {} |
| 9 | + |
| 10 | + public get size() { |
| 11 | + return this.internalMap.size |
| 12 | + } |
| 13 | + |
| 14 | + public [Symbol.iterator]() { |
| 15 | + return this.internalMap[Symbol.iterator]() |
| 16 | + } |
| 17 | + |
| 18 | + public get(key: K) { |
| 19 | + return this.internalMap.get(key) |
| 20 | + } |
| 21 | + |
| 22 | + public set(key: K, value: V) { |
| 23 | + return this.internalMap.set(key, value) |
| 24 | + } |
| 25 | + |
| 26 | + public has(key: K) { |
| 27 | + return this.internalMap.has(key) |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Similar to how the python dictionary's setdefault function works: |
| 32 | + * If the key is not present, it is set to the given value, then that value is returned |
| 33 | + * Otherwise, `setdefault` returns the value stored in the dictionary without |
| 34 | + * modifying it |
| 35 | + */ |
| 36 | + public setdefault(key: K, value: V) { |
| 37 | + if (!this.has(key)) { |
| 38 | + this.set(key, value) |
| 39 | + } |
| 40 | + |
| 41 | + return this.get(key)! |
| 42 | + } |
| 43 | + |
| 44 | + public update(key: K, defaultVal: V, updater: (oldV: V) => V) { |
| 45 | + const value = this.setdefault(key, defaultVal) |
| 46 | + const newValue = updater(value) |
| 47 | + this.set(key, newValue) |
| 48 | + return newValue |
| 49 | + } |
| 50 | + |
| 51 | + public entries() { |
| 52 | + return [...this.internalMap.entries()] |
| 53 | + } |
| 54 | + |
| 55 | + public forEach(func: (key: K, value: V) => void) { |
| 56 | + this.internalMap.forEach((v, k) => func(k, v)) |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Similar to `mapAsync`, but for an async mapping function that does not return any value |
| 61 | + */ |
| 62 | + public async forEachAsync(func: (k: K, v: V, index: number) => Promise<void>): Promise<void> { |
| 63 | + await Promise.all(this.map((key, value, i) => func(key, value, i))) |
| 64 | + } |
| 65 | + |
| 66 | + public map<T>(func: (key: K, value: V, index: number) => T) { |
| 67 | + return this.entries().map(([k, v], i) => func(k, v, i)) |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Using a mapping function that returns a promise, transform a map |
| 72 | + * to another map with different keys and values. All calls to the mapping function |
| 73 | + * execute asynchronously |
| 74 | + */ |
| 75 | + public mapAsync<U>(func: (key: K, value: V, index: number) => Promise<U>) { |
| 76 | + return Promise.all(this.map((key, value, i) => func(key, value, i))) |
| 77 | + } |
| 78 | + |
| 79 | + public flatMap<U>(func: (key: K, value: V, index: number) => U[]) { |
| 80 | + return this.entries().flatMap(([k, v], i) => func(k, v, i)) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Convenience class for maps that store an array of values |
| 86 | + */ |
| 87 | +export class ArrayMap<K, V> extends Dict<K, V[]> { |
| 88 | + public add(key: K, item: V) { |
| 89 | + this.setdefault(key, []).push(item) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + export function filterImportDeclarations({ |
| 94 | + body |
| 95 | + }: es.Program): [ |
| 96 | + ArrayMap<string, es.ImportDeclaration>, |
| 97 | + Exclude<es.Program['body'][0], es.ImportDeclaration>[] |
| 98 | + ] { |
| 99 | + return body.reduce( |
| 100 | + ([importNodes, otherNodes], node) => { |
| 101 | + if (!isImportDeclaration(node)) return [importNodes, [...otherNodes, node]] |
| 102 | + |
| 103 | + const moduleName = getModuleDeclarationSource(node) |
| 104 | + importNodes.add(moduleName, node) |
| 105 | + return [importNodes, otherNodes] |
| 106 | + }, |
| 107 | + [new ArrayMap(), []] as [ |
| 108 | + ArrayMap<string, es.ImportDeclaration>, |
| 109 | + Exclude<es.Program['body'][0], es.ImportDeclaration>[] |
| 110 | + ] |
| 111 | + ) |
| 112 | +} |
0 commit comments