| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.default = void 0;
|
|---|
| 7 | var _index = require("./path/index.js");
|
|---|
| 8 | var _t = require("@babel/types");
|
|---|
| 9 | var _context = require("./path/context.js");
|
|---|
| 10 | var _hub = require("./hub.js");
|
|---|
| 11 | const {
|
|---|
| 12 | VISITOR_KEYS
|
|---|
| 13 | } = _t;
|
|---|
| 14 | class TraversalContext {
|
|---|
| 15 | constructor(scope, opts, state, parentPath) {
|
|---|
| 16 | this.queue = null;
|
|---|
| 17 | this.priorityQueue = null;
|
|---|
| 18 | this.parentPath = parentPath;
|
|---|
| 19 | this.scope = scope;
|
|---|
| 20 | this.state = state;
|
|---|
| 21 | this.opts = opts;
|
|---|
| 22 | }
|
|---|
| 23 | shouldVisit(node) {
|
|---|
| 24 | const opts = this.opts;
|
|---|
| 25 | if (opts.enter || opts.exit) return true;
|
|---|
| 26 | if (opts[node.type]) return true;
|
|---|
| 27 | const keys = VISITOR_KEYS[node.type];
|
|---|
| 28 | if (!(keys != null && keys.length)) return false;
|
|---|
| 29 | for (const key of keys) {
|
|---|
| 30 | if (node[key]) {
|
|---|
| 31 | return true;
|
|---|
| 32 | }
|
|---|
| 33 | }
|
|---|
| 34 | return false;
|
|---|
| 35 | }
|
|---|
| 36 | create(node, container, key, listKey) {
|
|---|
| 37 | const {
|
|---|
| 38 | parentPath
|
|---|
| 39 | } = this;
|
|---|
| 40 | const hub = parentPath == null ? node.type === "Program" || node.type === "File" ? new _hub.default() : undefined : parentPath.hub;
|
|---|
| 41 | return _index.default.get({
|
|---|
| 42 | parentPath,
|
|---|
| 43 | parent: node,
|
|---|
| 44 | container,
|
|---|
| 45 | key: key,
|
|---|
| 46 | listKey,
|
|---|
| 47 | hub
|
|---|
| 48 | });
|
|---|
| 49 | }
|
|---|
| 50 | maybeQueue(path, notPriority) {
|
|---|
| 51 | if (this.queue) {
|
|---|
| 52 | if (notPriority) {
|
|---|
| 53 | this.queue.push(path);
|
|---|
| 54 | } else {
|
|---|
| 55 | this.priorityQueue.push(path);
|
|---|
| 56 | }
|
|---|
| 57 | }
|
|---|
| 58 | }
|
|---|
| 59 | visitMultiple(container, parent, listKey) {
|
|---|
| 60 | if (container.length === 0) return false;
|
|---|
| 61 | const queue = [];
|
|---|
| 62 | for (let key = 0; key < container.length; key++) {
|
|---|
| 63 | const node = container[key];
|
|---|
| 64 | if (node && this.shouldVisit(node)) {
|
|---|
| 65 | queue.push(this.create(parent, container, key, listKey));
|
|---|
| 66 | }
|
|---|
| 67 | }
|
|---|
| 68 | return this.visitQueue(queue);
|
|---|
| 69 | }
|
|---|
| 70 | visitSingle(node, key) {
|
|---|
| 71 | if (this.shouldVisit(node[key])) {
|
|---|
| 72 | return this.visitQueue([this.create(node, node, key)]);
|
|---|
| 73 | } else {
|
|---|
| 74 | return false;
|
|---|
| 75 | }
|
|---|
| 76 | }
|
|---|
| 77 | visitQueue(queue) {
|
|---|
| 78 | this.queue = queue;
|
|---|
| 79 | this.priorityQueue = [];
|
|---|
| 80 | const visited = new WeakSet();
|
|---|
| 81 | let stop = false;
|
|---|
| 82 | let visitIndex = 0;
|
|---|
| 83 | for (; visitIndex < queue.length;) {
|
|---|
| 84 | const path = queue[visitIndex];
|
|---|
| 85 | visitIndex++;
|
|---|
| 86 | _context.resync.call(path);
|
|---|
| 87 | if (path.contexts.length === 0 || path.contexts[path.contexts.length - 1] !== this) {
|
|---|
| 88 | _context.pushContext.call(path, this);
|
|---|
| 89 | }
|
|---|
| 90 | if (path.key === null) continue;
|
|---|
| 91 | const {
|
|---|
| 92 | node
|
|---|
| 93 | } = path;
|
|---|
| 94 | if (visited.has(node)) continue;
|
|---|
| 95 | if (node) visited.add(node);
|
|---|
| 96 | if (path.visit()) {
|
|---|
| 97 | stop = true;
|
|---|
| 98 | break;
|
|---|
| 99 | }
|
|---|
| 100 | if (this.priorityQueue.length) {
|
|---|
| 101 | stop = this.visitQueue(this.priorityQueue);
|
|---|
| 102 | this.priorityQueue = [];
|
|---|
| 103 | this.queue = queue;
|
|---|
| 104 | if (stop) break;
|
|---|
| 105 | }
|
|---|
| 106 | }
|
|---|
| 107 | for (let i = 0; i < visitIndex; i++) {
|
|---|
| 108 | _context.popContext.call(queue[i]);
|
|---|
| 109 | }
|
|---|
| 110 | this.queue = null;
|
|---|
| 111 | return stop;
|
|---|
| 112 | }
|
|---|
| 113 | visit(node, key) {
|
|---|
| 114 | const nodes = node[key];
|
|---|
| 115 | if (!nodes) return false;
|
|---|
| 116 | if (Array.isArray(nodes)) {
|
|---|
| 117 | return this.visitMultiple(nodes, node, key);
|
|---|
| 118 | } else {
|
|---|
| 119 | return this.visitSingle(node, key);
|
|---|
| 120 | }
|
|---|
| 121 | }
|
|---|
| 122 | }
|
|---|
| 123 | exports.default = TraversalContext;
|
|---|
| 124 |
|
|---|
| 125 | //# sourceMappingURL=context.js.map
|
|---|