source: trip-planner-front/node_modules/@babel/traverse/lib/path/ancestry.js@ ceaed42

Last change on this file since ceaed42 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.6 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.findParent = findParent;
7exports.find = find;
8exports.getFunctionParent = getFunctionParent;
9exports.getStatementParent = getStatementParent;
10exports.getEarliestCommonAncestorFrom = getEarliestCommonAncestorFrom;
11exports.getDeepestCommonAncestorFrom = getDeepestCommonAncestorFrom;
12exports.getAncestry = getAncestry;
13exports.isAncestor = isAncestor;
14exports.isDescendant = isDescendant;
15exports.inType = inType;
16
17var _t = require("@babel/types");
18
19var _index = require("./index");
20
21const {
22 VISITOR_KEYS
23} = _t;
24
25function 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
35function 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
45function getFunctionParent() {
46 return this.findParent(p => p.isFunction());
47}
48
49function 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
67function 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
99function 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
149function 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
160function isAncestor(maybeDescendant) {
161 return maybeDescendant.isDescendant(this);
162}
163
164function isDescendant(maybeAncestor) {
165 return !!this.findParent(parent => parent === maybeAncestor);
166}
167
168function 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}
Note: See TracBrowser for help on using the repository browser.