| 1 | // This file contains methods responsible for maintaining a TraversalContext. |
| 2 | |
| 3 | import { traverseNode } from "../traverse-node"; |
| 4 | import { SHOULD_SKIP, SHOULD_STOP } from "./index"; |
| 5 | import type TraversalContext from "../context"; |
| 6 | import type NodePath from "./index"; |
| 7 | import type * as t from "@babel/types"; |
| 8 | |
| 9 | export function call(this: NodePath, key: string): boolean { |
| 10 | const opts = this.opts; |
| 11 | |
| 12 | this.debug(key); |
| 13 | |
| 14 | if (this.node) { |
| 15 | if (this._call(opts[key])) return true; |
| 16 | } |
| 17 | |
| 18 | if (this.node) { |
| 19 | return this._call(opts[this.node.type] && opts[this.node.type][key]); |
| 20 | } |
| 21 | |
| 22 | return false; |
| 23 | } |
| 24 | |
| 25 | export function _call(this: NodePath, fns?: Array<Function>): boolean { |
| 26 | if (!fns) return false; |
| 27 | |
| 28 | for (const fn of fns) { |
| 29 | if (!fn) continue; |
| 30 | |
| 31 | const node = this.node; |
| 32 | if (!node) return true; |
| 33 | |
| 34 | const ret = fn.call(this.state, this, this.state); |
| 35 | if (ret && typeof ret === "object" && typeof ret.then === "function") { |
| 36 | throw new Error( |
| 37 | `You appear to be using a plugin with an async traversal visitor, ` + |
| 38 | `which your current version of Babel does not support. ` + |
| 39 | `If you're using a published plugin, you may need to upgrade ` + |
| 40 | `your @babel/core version.`, |
| 41 | ); |
| 42 | } |
| 43 | if (ret) { |
| 44 | throw new Error(`Unexpected return value from visitor method ${fn}`); |
| 45 | } |
| 46 | |
| 47 | // node has been replaced, it will have been requeued |
| 48 | if (this.node !== node) return true; |
| 49 | |
| 50 | // this.shouldSkip || this.shouldStop || this.removed |
| 51 | if (this._traverseFlags > 0) return true; |
| 52 | } |
| 53 | |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | export function isDenylisted(this: NodePath): boolean { |
| 58 | const denylist = this.opts.denylist ?? this.opts.blacklist; |
| 59 | return denylist && denylist.indexOf(this.node.type) > -1; |
| 60 | } |
| 61 | |
| 62 | // TODO: Remove in Babel 8 |
| 63 | export { isDenylisted as isBlacklisted }; |
| 64 | |
| 65 | function restoreContext(path: NodePath, context: TraversalContext) { |
| 66 | if (path.context !== context) { |
| 67 | path.context = context; |
| 68 | path.state = context.state; |
| 69 | path.opts = context.opts; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | export function visit(this: NodePath): boolean { |
| 74 | if (!this.node) { |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | if (this.isDenylisted()) { |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | if (this.opts.shouldSkip && this.opts.shouldSkip(this)) { |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | const currentContext = this.context; |
| 87 | // Note: We need to check "this.shouldSkip" first because |
| 88 | // another visitor can set it to true. Usually .shouldSkip is false |
| 89 | // before calling the enter visitor, but it can be true in case of |
| 90 | // a requeued node (e.g. by .replaceWith()) that is then marked |
| 91 | // with .skip(). |
| 92 | if (this.shouldSkip || this.call("enter")) { |
| 93 | this.debug("Skip..."); |
| 94 | return this.shouldStop; |
| 95 | } |
| 96 | restoreContext(this, currentContext); |
| 97 | |
| 98 | this.debug("Recursing into..."); |
| 99 | this.shouldStop = traverseNode( |
| 100 | this.node, |
| 101 | this.opts, |
| 102 | this.scope, |
| 103 | this.state, |
| 104 | this, |
| 105 | this.skipKeys, |
| 106 | ); |
| 107 | |
| 108 | restoreContext(this, currentContext); |
| 109 | |
| 110 | this.call("exit"); |
| 111 | |
| 112 | return this.shouldStop; |
| 113 | } |
| 114 | |
| 115 | export function skip(this: NodePath) { |
| 116 | this.shouldSkip = true; |
| 117 | } |
| 118 | |
| 119 | export function skipKey(this: NodePath, key: string) { |
| 120 | if (this.skipKeys == null) { |
| 121 | this.skipKeys = {}; |
| 122 | } |
| 123 | this.skipKeys[key] = true; |
| 124 | } |
| 125 | |
| 126 | export function stop(this: NodePath) { |
| 127 | // this.shouldSkip = true; this.shouldStop = true; |
| 128 | this._traverseFlags |= SHOULD_SKIP | SHOULD_STOP; |
| 129 | } |
| 130 | |
| 131 | export function setScope(this: NodePath) { |
| 132 | if (this.opts && this.opts.noScope) return; |
| 133 | |
| 134 | let path = this.parentPath; |
| 135 | |
| 136 | if ( |
| 137 | // Skip method scope if is computed method key or decorator expression |
| 138 | ((this.key === "key" || this.listKey === "decorators") && |
| 139 | path.isMethod()) || |
| 140 | // Skip switch scope if for discriminant (`x` in `switch (x) {}`). |
| 141 | (this.key === "discriminant" && path.isSwitchStatement()) |
| 142 | ) { |
| 143 | path = path.parentPath; |
| 144 | } |
| 145 | |
| 146 | let target; |
| 147 | while (path && !target) { |
| 148 | if (path.opts && path.opts.noScope) return; |
| 149 | |
| 150 | target = path.scope; |
| 151 | path = path.parentPath; |
| 152 | } |
| 153 | |
| 154 | this.scope = this.getScope(target); |
| 155 | if (this.scope) this.scope.init(); |
| 156 | } |
| 157 | |
| 158 | export function setContext<S = unknown>( |
| 159 | this: NodePath, |
| 160 | context?: TraversalContext<S>, |
| 161 | ) { |
| 162 | if (this.skipKeys != null) { |
| 163 | this.skipKeys = {}; |
| 164 | } |
| 165 | // this.shouldSkip = false; this.shouldStop = false; this.removed = false; |
| 166 | this._traverseFlags = 0; |
| 167 | |
| 168 | if (context) { |
| 169 | this.context = context; |
| 170 | this.state = context.state; |
| 171 | this.opts = context.opts; |
| 172 | } |
| 173 | |
| 174 | this.setScope(); |
| 175 | |
| 176 | return this; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Here we resync the node paths `key` and `container`. If they've changed according |
| 181 | * to what we have stored internally then we attempt to resync by crawling and looking |
| 182 | * for the new values. |
| 183 | */ |
| 184 | |
| 185 | export function resync(this: NodePath) { |
| 186 | if (this.removed) return; |
| 187 | |
| 188 | this._resyncParent(); |
| 189 | this._resyncList(); |
| 190 | this._resyncKey(); |
| 191 | //this._resyncRemoved(); |
| 192 | } |
| 193 | |
| 194 | export function _resyncParent(this: NodePath) { |
| 195 | if (this.parentPath) { |
| 196 | this.parent = this.parentPath.node; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | export function _resyncKey(this: NodePath) { |
| 201 | if (!this.container) return; |
| 202 | |
| 203 | if ( |
| 204 | this.node === |
| 205 | // @ts-expect-error this.key should present in this.container |
| 206 | this.container[this.key] |
| 207 | ) { |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | // grrr, path key is out of sync. this is likely due to a modification to the AST |
| 212 | // not done through our path APIs |
| 213 | |
| 214 | if (Array.isArray(this.container)) { |
| 215 | for (let i = 0; i < this.container.length; i++) { |
| 216 | if (this.container[i] === this.node) { |
| 217 | return this.setKey(i); |
| 218 | } |
| 219 | } |
| 220 | } else { |
| 221 | for (const key of Object.keys(this.container)) { |
| 222 | // @ts-expect-error this.key should present in this.container |
| 223 | if (this.container[key] === this.node) { |
| 224 | return this.setKey(key); |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | // ¯\_(ツ)_/¯ who knows where it's gone lol |
| 230 | this.key = null; |
| 231 | } |
| 232 | |
| 233 | export function _resyncList(this: NodePath) { |
| 234 | if (!this.parent || !this.inList) return; |
| 235 | |
| 236 | const newContainer = |
| 237 | // @ts-expect-error this.listKey should present in this.parent |
| 238 | this.parent[this.listKey]; |
| 239 | if (this.container === newContainer) return; |
| 240 | |
| 241 | // container is out of sync. this is likely the result of it being reassigned |
| 242 | this.container = newContainer || null; |
| 243 | } |
| 244 | |
| 245 | export function _resyncRemoved(this: NodePath) { |
| 246 | if ( |
| 247 | this.key == null || |
| 248 | !this.container || |
| 249 | // @ts-expect-error this.key should present in this.container |
| 250 | this.container[this.key] !== this.node |
| 251 | ) { |
| 252 | this._markRemoved(); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | export function popContext(this: NodePath) { |
| 257 | this.contexts.pop(); |
| 258 | if (this.contexts.length > 0) { |
| 259 | this.setContext(this.contexts[this.contexts.length - 1]); |
| 260 | } else { |
| 261 | this.setContext(undefined); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | export function pushContext(this: NodePath, context: TraversalContext) { |
| 266 | this.contexts.push(context); |
| 267 | this.setContext(context); |
| 268 | } |
| 269 | |
| 270 | export function setup( |
| 271 | this: NodePath, |
| 272 | parentPath: NodePath | undefined, |
| 273 | container: t.Node, |
| 274 | listKey: string, |
| 275 | key: string | number, |
| 276 | ) { |
| 277 | this.listKey = listKey; |
| 278 | this.container = container; |
| 279 | |
| 280 | this.parentPath = parentPath || this.parentPath; |
| 281 | this.setKey(key); |
| 282 | } |
| 283 | |
| 284 | export function setKey(this: NodePath, key: string | number) { |
| 285 | this.key = key; |
| 286 | this.node = |
| 287 | // @ts-expect-error this.key must present in this.container |
| 288 | this.container[this.key]; |
| 289 | this.type = this.node?.type; |
| 290 | } |
| 291 | |
| 292 | export function requeue(this: NodePath, pathToQueue = this) { |
| 293 | if (pathToQueue.removed) return; |
| 294 | |
| 295 | // If a path is skipped, and then replaced with a |
| 296 | // new one, the new one shouldn't probably be skipped. |
| 297 | if (process.env.BABEL_8_BREAKING) { |
| 298 | pathToQueue.shouldSkip = false; |
| 299 | } |
| 300 | |
| 301 | // TODO(loganfsmyth): This should be switched back to queue in parent contexts |
| 302 | // automatically once #2892 and #4135 have been resolved. See #4140. |
| 303 | // let contexts = this._getQueueContexts(); |
| 304 | const contexts = this.contexts; |
| 305 | |
| 306 | for (const context of contexts) { |
| 307 | context.maybeQueue(pathToQueue); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | export function _getQueueContexts(this: NodePath) { |
| 312 | let path = this; |
| 313 | let contexts = this.contexts; |
| 314 | while (!contexts.length) { |
| 315 | path = path.parentPath; |
| 316 | if (!path) break; |
| 317 | contexts = path.contexts; |
| 318 | } |
| 319 | return contexts; |
| 320 | } |