{"version":3,"names":["_context","require","_index","_t","_context2","VISITOR_KEYS","_visitPaths","ctx","paths","queue","priorityQueue","visited","Set","stop","visitIndex","length","path","resync","call","contexts","pushContext","key","node","has","add","_visit","i","popContext","_opts$denylist","opts","denylist","blacklist","includes","type","shouldSkip","shouldStop","_call","enter","_opts$node$type","_traverse","scope","state","skipKeys","exit","_opts$node$type2","visitSelf","keys","TraversalContext","parentKey","prop","Array","isArray","childPath","NodePath","get","parentPath","parent","container","listKey","push","traverseNode","context","visitQueue","visit"],"sources":["../src/traverse-node.ts"],"sourcesContent":["import TraversalContext from \"./context.ts\";\nimport type { ExplodedTraverseOptions } from \"./index.ts\";\nimport NodePath from \"./path/index.ts\";\nimport type Scope from \"./scope/index.ts\";\nimport type * as t from \"@babel/types\";\nimport { VISITOR_KEYS } from \"@babel/types\";\nimport { _call, popContext, pushContext, resync } from \"./path/context.ts\";\n\nfunction _visitPaths(ctx: TraversalContext, paths: NodePath[]): boolean {\n // set queue\n ctx.queue = paths;\n ctx.priorityQueue = [];\n\n const visited = new Set();\n let stop = false;\n let visitIndex = 0;\n\n for (; visitIndex < paths.length; ) {\n const path = paths[visitIndex];\n visitIndex++;\n\n resync.call(path);\n\n if (\n path.contexts.length === 0 ||\n path.contexts[path.contexts.length - 1] !== ctx\n ) {\n // The context might already have been pushed when this path was inserted and queued.\n // If we always re-pushed here, we could get duplicates and risk leaving contexts\n // on the stack after the traversal has completed, which could break things.\n pushContext.call(path, ctx);\n }\n\n // this path no longer belongs to the tree\n if (path.key === null) continue;\n\n // ensure we don't visit the same node twice\n const { node } = path;\n if (visited.has(node)) continue;\n if (node) visited.add(node);\n\n if (_visit(ctx, path)) {\n stop = true;\n break;\n }\n\n if (ctx.priorityQueue.length) {\n stop = _visitPaths(ctx, ctx.priorityQueue);\n ctx.priorityQueue = [];\n ctx.queue = paths;\n if (stop) break;\n }\n }\n\n // pop contexts\n for (let i = 0; i < visitIndex; i++) {\n popContext.call(paths[i]);\n }\n\n // clear queue\n ctx.queue = null;\n\n return stop;\n}\n\nfunction _visit(ctx: TraversalContext, path: NodePath) {\n const node = path.node;\n if (!node) {\n return false;\n }\n const opts = ctx.opts;\n\n // @ts-expect-error TODO(Babel 8): Remove blacklist\n const denylist = opts.denylist ?? opts.blacklist;\n if (denylist?.includes(node.type)) {\n return false;\n }\n\n if (opts.shouldSkip?.(path)) {\n return false;\n }\n\n // Note: We need to check \"this.shouldSkip\" first because\n // another visitor can set it to true. Usually .shouldSkip is false\n // before calling the enter visitor, but it can be true in case of\n // a requeued node (e.g. by .replaceWith()) that is then marked\n // with .skip().\n if (path.shouldSkip) return path.shouldStop;\n\n if (_call.call(path, opts.enter)) return path.shouldStop;\n if (path.node) {\n if (_call.call(path, opts[node.type]?.enter)) return path.shouldStop;\n }\n\n path.shouldStop = _traverse(\n path.node,\n opts,\n path.scope,\n ctx.state,\n path,\n path.skipKeys,\n );\n\n if (path.node) {\n if (_call.call(path, opts.exit)) return true;\n }\n if (path.node) {\n _call.call(path, opts[node.type]?.exit);\n }\n\n return path.shouldStop;\n}\n\nfunction _traverse(\n node: t.Node,\n opts: ExplodedTraverseOptions,\n scope?: Scope,\n state?: S,\n path?: NodePath,\n skipKeys?: Record,\n visitSelf?: boolean,\n) {\n const keys = VISITOR_KEYS[node.type];\n if (!keys?.length) return false;\n\n const ctx = new TraversalContext(scope, opts, state, path);\n if (visitSelf) {\n if (skipKeys?.[path.parentKey]) return false;\n return _visitPaths(ctx, [path]);\n }\n\n for (const key of keys) {\n if (skipKeys?.[key]) continue;\n // @ts-expect-error key must present in node\n const prop = node[key];\n if (!prop) continue;\n\n if (Array.isArray(prop)) {\n if (!prop.length) continue;\n const paths = [];\n for (let i = 0; i < prop.length; i++) {\n const childPath = NodePath.get({\n parentPath: path,\n parent: node,\n container: prop,\n key: i,\n listKey: key,\n });\n paths.push(childPath);\n }\n if (_visitPaths(ctx, paths)) return true;\n } else {\n if (\n _visitPaths(ctx, [\n NodePath.get({\n parentPath: path,\n parent: node,\n container: node,\n key,\n listKey: null,\n }),\n ])\n ) {\n return true;\n }\n }\n }\n\n return false;\n}\n\n/**\n * Traverse the children of given node\n * @param {Node} node\n * @param {TraverseOptions} opts The traverse options used to create a new traversal context\n * @param {scope} scope A traversal scope used to create a new traversal context. When opts.noScope is true, scope should not be provided\n * @param {any} state A user data storage provided as the second callback argument for traversal visitors\n * @param {NodePath} path A NodePath of given node\n * @param {Record} skipKeys A map from key names to whether that should be skipped during traversal. The skipKeys are applied to every descendants\n * @returns {boolean} Whether the traversal stops early\n\n * @note This function does not visit the given `node`.\n */\nexport function traverseNode(\n node: t.Node,\n opts: ExplodedTraverseOptions,\n scope?: Scope,\n state?: S,\n path?: NodePath,\n skipKeys?: Record,\n visitSelf?: boolean,\n): boolean {\n if (process.env.BABEL_8_BREAKING) {\n return _traverse(node, opts, scope, state, path, skipKeys, visitSelf);\n }\n\n const keys = VISITOR_KEYS[node.type];\n if (!keys) return false;\n\n const context = new TraversalContext(scope, opts, state, path);\n if (visitSelf) {\n if (skipKeys?.[path.parentKey]) return false;\n return context.visitQueue([path]);\n }\n\n for (const key of keys) {\n if (skipKeys?.[key]) continue;\n if (context.visit(node, key)) {\n return true;\n }\n }\n\n return false;\n}\n"],"mappings":";;;;;;AAAA,IAAAA,QAAA,GAAAC,OAAA;AAEA,IAAAC,MAAA,GAAAD,OAAA;AAGA,IAAAE,EAAA,GAAAF,OAAA;AACA,IAAAG,SAAA,GAAAH,OAAA;AAA2E;EADlEI;AAAY,IAAAF,EAAA;AAGrB,SAASG,WAAWA,CAACC,GAAqB,EAAEC,KAAiB,EAAW;EAEtED,GAAG,CAACE,KAAK,GAAGD,KAAK;EACjBD,GAAG,CAACG,aAAa,GAAG,EAAE;EAEtB,MAAMC,OAAO,GAAG,IAAIC,GAAG,CAAC,CAAC;EACzB,IAAIC,IAAI,GAAG,KAAK;EAChB,IAAIC,UAAU,GAAG,CAAC;EAElB,OAAOA,UAAU,GAAGN,KAAK,CAACO,MAAM,GAAI;IACl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