source: imaps-frontend/node_modules/@babel/traverse/lib/path/ancestry.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

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