| 1 | import NodePath from "./path"; |
| 2 | import { VISITOR_KEYS } from "@babel/types"; |
| 3 | import type Scope from "./scope"; |
| 4 | import type { TraverseOptions } from "."; |
| 5 | import type * as t from "@babel/types"; |
| 6 | import type { Visitor } from "./types"; |
| 7 | |
| 8 | export default class TraversalContext<S = unknown> { |
| 9 | constructor( |
| 10 | scope: Scope, |
| 11 | opts: TraverseOptions, |
| 12 | state: S, |
| 13 | parentPath: NodePath, |
| 14 | ) { |
| 15 | this.parentPath = parentPath; |
| 16 | this.scope = scope; |
| 17 | this.state = state; |
| 18 | this.opts = opts; |
| 19 | } |
| 20 | |
| 21 | declare parentPath: NodePath; |
| 22 | declare scope: Scope; |
| 23 | declare state: S; |
| 24 | declare opts: TraverseOptions; |
| 25 | queue: Array<NodePath> | null = null; |
| 26 | priorityQueue: Array<NodePath> | null = null; |
| 27 | |
| 28 | /** |
| 29 | * This method does a simple check to determine whether or not we really need to attempt |
| 30 | * visit a node. This will prevent us from constructing a NodePath. |
| 31 | */ |
| 32 | |
| 33 | shouldVisit(node: t.Node): boolean { |
| 34 | const opts = this.opts as Visitor; |
| 35 | if (opts.enter || opts.exit) return true; |
| 36 | |
| 37 | // check if we have a visitor for this node |
| 38 | if (opts[node.type]) return true; |
| 39 | |
| 40 | // check if we're going to traverse into this node |
| 41 | const keys: Array<string> | undefined = VISITOR_KEYS[node.type]; |
| 42 | if (!keys?.length) return false; |
| 43 | |
| 44 | // we need to traverse into this node so ensure that it has children to traverse into! |
| 45 | for (const key of keys) { |
| 46 | if ( |
| 47 | // @ts-expect-error key is from visitor keys |
| 48 | node[key] |
| 49 | ) { |
| 50 | return true; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | create( |
| 58 | node: t.Node, |
| 59 | container: t.Node | t.Node[], |
| 60 | key: string | number, |
| 61 | listKey?: string, |
| 62 | ): NodePath { |
| 63 | // We don't need to `.setContext()` here, since `.visitQueue()` already |
| 64 | // calls `.pushContext`. |
| 65 | return NodePath.get({ |
| 66 | parentPath: this.parentPath, |
| 67 | parent: node, |
| 68 | container, |
| 69 | key: key, |
| 70 | listKey, |
| 71 | }); |
| 72 | } |
| 73 | |
| 74 | maybeQueue(path: NodePath, notPriority?: boolean) { |
| 75 | if (this.queue) { |
| 76 | if (notPriority) { |
| 77 | this.queue.push(path); |
| 78 | } else { |
| 79 | this.priorityQueue.push(path); |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | visitMultiple(container: t.Node[], parent: t.Node, listKey: string) { |
| 85 | // nothing to traverse! |
| 86 | if (container.length === 0) return false; |
| 87 | |
| 88 | const queue = []; |
| 89 | |
| 90 | // build up initial queue |
| 91 | for (let key = 0; key < container.length; key++) { |
| 92 | const node = container[key]; |
| 93 | if (node && this.shouldVisit(node)) { |
| 94 | queue.push(this.create(parent, container, key, listKey)); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return this.visitQueue(queue); |
| 99 | } |
| 100 | |
| 101 | visitSingle(node: t.Node, key: string): boolean { |
| 102 | if ( |
| 103 | this.shouldVisit( |
| 104 | // @ts-expect-error key may not index node |
| 105 | node[key], |
| 106 | ) |
| 107 | ) { |
| 108 | return this.visitQueue([this.create(node, node, key)]); |
| 109 | } else { |
| 110 | return false; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | visitQueue(queue: Array<NodePath>): boolean { |
| 115 | // set queue |
| 116 | this.queue = queue; |
| 117 | this.priorityQueue = []; |
| 118 | |
| 119 | const visited = new WeakSet(); |
| 120 | let stop = false; |
| 121 | |
| 122 | // visit the queue |
| 123 | for (const path of queue) { |
| 124 | path.resync(); |
| 125 | |
| 126 | if ( |
| 127 | path.contexts.length === 0 || |
| 128 | path.contexts[path.contexts.length - 1] !== this |
| 129 | ) { |
| 130 | // The context might already have been pushed when this path was inserted and queued. |
| 131 | // If we always re-pushed here, we could get duplicates and risk leaving contexts |
| 132 | // on the stack after the traversal has completed, which could break things. |
| 133 | path.pushContext(this); |
| 134 | } |
| 135 | |
| 136 | // this path no longer belongs to the tree |
| 137 | if (path.key === null) continue; |
| 138 | |
| 139 | // ensure we don't visit the same node twice |
| 140 | const { node } = path; |
| 141 | if (visited.has(node)) continue; |
| 142 | if (node) visited.add(node); |
| 143 | |
| 144 | if (path.visit()) { |
| 145 | stop = true; |
| 146 | break; |
| 147 | } |
| 148 | |
| 149 | if (this.priorityQueue.length) { |
| 150 | stop = this.visitQueue(this.priorityQueue); |
| 151 | this.priorityQueue = []; |
| 152 | this.queue = queue; |
| 153 | if (stop) break; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // clear queue |
| 158 | for (const path of queue) { |
| 159 | path.popContext(); |
| 160 | } |
| 161 | |
| 162 | // clear queue |
| 163 | this.queue = null; |
| 164 | |
| 165 | return stop; |
| 166 | } |
| 167 | |
| 168 | visit(node: t.Node, key: string) { |
| 169 | // @ts-expect-error key may not index node |
| 170 | const nodes = node[key] as t.Node | t.Node[] | null; |
| 171 | if (!nodes) return false; |
| 172 | |
| 173 | if (Array.isArray(nodes)) { |
| 174 | return this.visitMultiple(nodes, node, key); |
| 175 | } else { |
| 176 | return this.visitSingle(node, key); |
| 177 | } |
| 178 | } |
| 179 | } |