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