1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.findParent = findParent;
|
---|
7 | exports.find = find;
|
---|
8 | exports.getFunctionParent = getFunctionParent;
|
---|
9 | exports.getStatementParent = getStatementParent;
|
---|
10 | exports.getEarliestCommonAncestorFrom = getEarliestCommonAncestorFrom;
|
---|
11 | exports.getDeepestCommonAncestorFrom = getDeepestCommonAncestorFrom;
|
---|
12 | exports.getAncestry = getAncestry;
|
---|
13 | exports.isAncestor = isAncestor;
|
---|
14 | exports.isDescendant = isDescendant;
|
---|
15 | exports.inType = inType;
|
---|
16 |
|
---|
17 | var _t = require("@babel/types");
|
---|
18 |
|
---|
19 | var _index = require("./index");
|
---|
20 |
|
---|
21 | const {
|
---|
22 | VISITOR_KEYS
|
---|
23 | } = _t;
|
---|
24 |
|
---|
25 | function findParent(callback) {
|
---|
26 | let path = this;
|
---|
27 |
|
---|
28 | while (path = path.parentPath) {
|
---|
29 | if (callback(path)) return path;
|
---|
30 | }
|
---|
31 |
|
---|
32 | return null;
|
---|
33 | }
|
---|
34 |
|
---|
35 | function find(callback) {
|
---|
36 | let path = this;
|
---|
37 |
|
---|
38 | do {
|
---|
39 | if (callback(path)) return path;
|
---|
40 | } while (path = path.parentPath);
|
---|
41 |
|
---|
42 | return null;
|
---|
43 | }
|
---|
44 |
|
---|
45 | function getFunctionParent() {
|
---|
46 | return this.findParent(p => p.isFunction());
|
---|
47 | }
|
---|
48 |
|
---|
49 | function getStatementParent() {
|
---|
50 | let path = this;
|
---|
51 |
|
---|
52 | do {
|
---|
53 | if (!path.parentPath || Array.isArray(path.container) && path.isStatement()) {
|
---|
54 | break;
|
---|
55 | } else {
|
---|
56 | path = path.parentPath;
|
---|
57 | }
|
---|
58 | } while (path);
|
---|
59 |
|
---|
60 | if (path && (path.isProgram() || path.isFile())) {
|
---|
61 | throw new Error("File/Program node, we can't possibly find a statement parent to this");
|
---|
62 | }
|
---|
63 |
|
---|
64 | return path;
|
---|
65 | }
|
---|
66 |
|
---|
67 | function getEarliestCommonAncestorFrom(paths) {
|
---|
68 | return this.getDeepestCommonAncestorFrom(paths, function (deepest, i, ancestries) {
|
---|
69 | let earliest;
|
---|
70 | const keys = VISITOR_KEYS[deepest.type];
|
---|
71 |
|
---|
72 | for (const ancestry of ancestries) {
|
---|
73 | const path = ancestry[i + 1];
|
---|
74 |
|
---|
75 | if (!earliest) {
|
---|
76 | earliest = path;
|
---|
77 | continue;
|
---|
78 | }
|
---|
79 |
|
---|
80 | if (path.listKey && earliest.listKey === path.listKey) {
|
---|
81 | if (path.key < earliest.key) {
|
---|
82 | earliest = path;
|
---|
83 | continue;
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | const earliestKeyIndex = keys.indexOf(earliest.parentKey);
|
---|
88 | const currentKeyIndex = keys.indexOf(path.parentKey);
|
---|
89 |
|
---|
90 | if (earliestKeyIndex > currentKeyIndex) {
|
---|
91 | earliest = path;
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | return earliest;
|
---|
96 | });
|
---|
97 | }
|
---|
98 |
|
---|
99 | function getDeepestCommonAncestorFrom(paths, filter) {
|
---|
100 | if (!paths.length) {
|
---|
101 | return this;
|
---|
102 | }
|
---|
103 |
|
---|
104 | if (paths.length === 1) {
|
---|
105 | return paths[0];
|
---|
106 | }
|
---|
107 |
|
---|
108 | let minDepth = Infinity;
|
---|
109 | let lastCommonIndex, lastCommon;
|
---|
110 | const ancestries = paths.map(path => {
|
---|
111 | const ancestry = [];
|
---|
112 |
|
---|
113 | do {
|
---|
114 | ancestry.unshift(path);
|
---|
115 | } while ((path = path.parentPath) && path !== this);
|
---|
116 |
|
---|
117 | if (ancestry.length < minDepth) {
|
---|
118 | minDepth = ancestry.length;
|
---|
119 | }
|
---|
120 |
|
---|
121 | return ancestry;
|
---|
122 | });
|
---|
123 | const first = ancestries[0];
|
---|
124 |
|
---|
125 | depthLoop: for (let i = 0; i < minDepth; i++) {
|
---|
126 | const shouldMatch = first[i];
|
---|
127 |
|
---|
128 | for (const ancestry of ancestries) {
|
---|
129 | if (ancestry[i] !== shouldMatch) {
|
---|
130 | break depthLoop;
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | lastCommonIndex = i;
|
---|
135 | lastCommon = shouldMatch;
|
---|
136 | }
|
---|
137 |
|
---|
138 | if (lastCommon) {
|
---|
139 | if (filter) {
|
---|
140 | return filter(lastCommon, lastCommonIndex, ancestries);
|
---|
141 | } else {
|
---|
142 | return lastCommon;
|
---|
143 | }
|
---|
144 | } else {
|
---|
145 | throw new Error("Couldn't find intersection");
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | function getAncestry() {
|
---|
150 | let path = this;
|
---|
151 | const paths = [];
|
---|
152 |
|
---|
153 | do {
|
---|
154 | paths.push(path);
|
---|
155 | } while (path = path.parentPath);
|
---|
156 |
|
---|
157 | return paths;
|
---|
158 | }
|
---|
159 |
|
---|
160 | function isAncestor(maybeDescendant) {
|
---|
161 | return maybeDescendant.isDescendant(this);
|
---|
162 | }
|
---|
163 |
|
---|
164 | function isDescendant(maybeAncestor) {
|
---|
165 | return !!this.findParent(parent => parent === maybeAncestor);
|
---|
166 | }
|
---|
167 |
|
---|
168 | function inType(...candidateTypes) {
|
---|
169 | let path = this;
|
---|
170 |
|
---|
171 | while (path) {
|
---|
172 | for (const type of candidateTypes) {
|
---|
173 | if (path.node.type === type) return true;
|
---|
174 | }
|
---|
175 |
|
---|
176 | path = path.parentPath;
|
---|
177 | }
|
---|
178 |
|
---|
179 | return false;
|
---|
180 | } |
---|