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