UNPKG

8.24 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.getOpposite = getOpposite;
7exports.getCompletionRecords = getCompletionRecords;
8exports.getSibling = getSibling;
9exports.getPrevSibling = getPrevSibling;
10exports.getNextSibling = getNextSibling;
11exports.getAllNextSiblings = getAllNextSiblings;
12exports.getAllPrevSiblings = getAllPrevSiblings;
13exports.get = get;
14exports._getKey = _getKey;
15exports._getPattern = _getPattern;
16exports.getBindingIdentifiers = getBindingIdentifiers;
17exports.getOuterBindingIdentifiers = getOuterBindingIdentifiers;
18exports.getBindingIdentifierPaths = getBindingIdentifierPaths;
19exports.getOuterBindingIdentifierPaths = getOuterBindingIdentifierPaths;
20
21var _index = _interopRequireDefault(require("./index"));
22
23var t = _interopRequireWildcard(require("@babel/types"));
24
25function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
26
27function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
28
29function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
30
31function getOpposite() {
32 if (this.key === "left") {
33 return this.getSibling("right");
34 } else if (this.key === "right") {
35 return this.getSibling("left");
36 }
37}
38
39function addCompletionRecords(path, paths) {
40 if (path) return paths.concat(path.getCompletionRecords());
41 return paths;
42}
43
44function completionRecordForSwitch(cases, paths) {
45 let isLastCaseWithConsequent = true;
46
47 for (let i = cases.length - 1; i >= 0; i--) {
48 const switchCase = cases[i];
49 const consequent = switchCase.get("consequent");
50 let breakStatement;
51
52 findBreak: for (const statement of consequent) {
53 if (statement.isBlockStatement()) {
54 for (const statementInBlock of statement.get("body")) {
55 if (statementInBlock.isBreakStatement()) {
56 breakStatement = statementInBlock;
57 break findBreak;
58 }
59 }
60 } else if (statement.isBreakStatement()) {
61 breakStatement = statement;
62 break;
63 }
64 }
65
66 if (breakStatement) {
67 while (breakStatement.key === 0 && breakStatement.parentPath.isBlockStatement()) {
68 breakStatement = breakStatement.parentPath;
69 }
70
71 const prevSibling = breakStatement.getPrevSibling();
72
73 if (breakStatement.key > 0 && (prevSibling.isExpressionStatement() || prevSibling.isBlockStatement())) {
74 paths = addCompletionRecords(prevSibling, paths);
75 breakStatement.remove();
76 } else {
77 breakStatement.replaceWith(breakStatement.scope.buildUndefinedNode());
78 paths = addCompletionRecords(breakStatement, paths);
79 }
80 } else if (isLastCaseWithConsequent) {
81 const statementFinder = statement => !statement.isBlockStatement() || statement.get("body").some(statementFinder);
82
83 const hasConsequent = consequent.some(statementFinder);
84
85 if (hasConsequent) {
86 paths = addCompletionRecords(consequent[consequent.length - 1], paths);
87 isLastCaseWithConsequent = false;
88 }
89 }
90 }
91
92 return paths;
93}
94
95function getCompletionRecords() {
96 let paths = [];
97
98 if (this.isIfStatement()) {
99 paths = addCompletionRecords(this.get("consequent"), paths);
100 paths = addCompletionRecords(this.get("alternate"), paths);
101 } else if (this.isDoExpression() || this.isFor() || this.isWhile()) {
102 paths = addCompletionRecords(this.get("body"), paths);
103 } else if (this.isProgram() || this.isBlockStatement()) {
104 paths = addCompletionRecords(this.get("body").pop(), paths);
105 } else if (this.isFunction()) {
106 return this.get("body").getCompletionRecords();
107 } else if (this.isTryStatement()) {
108 paths = addCompletionRecords(this.get("block"), paths);
109 paths = addCompletionRecords(this.get("handler"), paths);
110 } else if (this.isCatchClause()) {
111 paths = addCompletionRecords(this.get("body"), paths);
112 } else if (this.isSwitchStatement()) {
113 paths = completionRecordForSwitch(this.get("cases"), paths);
114 } else {
115 paths.push(this);
116 }
117
118 return paths;
119}
120
121function getSibling(key) {
122 return _index.default.get({
123 parentPath: this.parentPath,
124 parent: this.parent,
125 container: this.container,
126 listKey: this.listKey,
127 key: key
128 });
129}
130
131function getPrevSibling() {
132 return this.getSibling(this.key - 1);
133}
134
135function getNextSibling() {
136 return this.getSibling(this.key + 1);
137}
138
139function getAllNextSiblings() {
140 let _key = this.key;
141 let sibling = this.getSibling(++_key);
142 const siblings = [];
143
144 while (sibling.node) {
145 siblings.push(sibling);
146 sibling = this.getSibling(++_key);
147 }
148
149 return siblings;
150}
151
152function getAllPrevSiblings() {
153 let _key = this.key;
154 let sibling = this.getSibling(--_key);
155 const siblings = [];
156
157 while (sibling.node) {
158 siblings.push(sibling);
159 sibling = this.getSibling(--_key);
160 }
161
162 return siblings;
163}
164
165function get(key, context) {
166 if (context === true) context = this.context;
167 const parts = key.split(".");
168
169 if (parts.length === 1) {
170 return this._getKey(key, context);
171 } else {
172 return this._getPattern(parts, context);
173 }
174}
175
176function _getKey(key, context) {
177 const node = this.node;
178 const container = node[key];
179
180 if (Array.isArray(container)) {
181 return container.map((_, i) => {
182 return _index.default.get({
183 listKey: key,
184 parentPath: this,
185 parent: node,
186 container: container,
187 key: i
188 }).setContext(context);
189 });
190 } else {
191 return _index.default.get({
192 parentPath: this,
193 parent: node,
194 container: node,
195 key: key
196 }).setContext(context);
197 }
198}
199
200function _getPattern(parts, context) {
201 let path = this;
202
203 for (const part of parts) {
204 if (part === ".") {
205 path = path.parentPath;
206 } else {
207 if (Array.isArray(path)) {
208 path = path[part];
209 } else {
210 path = path.get(part, context);
211 }
212 }
213 }
214
215 return path;
216}
217
218function getBindingIdentifiers(duplicates) {
219 return t.getBindingIdentifiers(this.node, duplicates);
220}
221
222function getOuterBindingIdentifiers(duplicates) {
223 return t.getOuterBindingIdentifiers(this.node, duplicates);
224}
225
226function getBindingIdentifierPaths(duplicates = false, outerOnly = false) {
227 const path = this;
228 let search = [].concat(path);
229 const ids = Object.create(null);
230
231 while (search.length) {
232 const id = search.shift();
233 if (!id) continue;
234 if (!id.node) continue;
235 const keys = t.getBindingIdentifiers.keys[id.node.type];
236
237 if (id.isIdentifier()) {
238 if (duplicates) {
239 const _ids = ids[id.node.name] = ids[id.node.name] || [];
240
241 _ids.push(id);
242 } else {
243 ids[id.node.name] = id;
244 }
245
246 continue;
247 }
248
249 if (id.isExportDeclaration()) {
250 const declaration = id.get("declaration");
251
252 if (declaration.isDeclaration()) {
253 search.push(declaration);
254 }
255
256 continue;
257 }
258
259 if (outerOnly) {
260 if (id.isFunctionDeclaration()) {
261 search.push(id.get("id"));
262 continue;
263 }
264
265 if (id.isFunctionExpression()) {
266 continue;
267 }
268 }
269
270 if (keys) {
271 for (let i = 0; i < keys.length; i++) {
272 const key = keys[i];
273 const child = id.get(key);
274
275 if (Array.isArray(child) || child.node) {
276 search = search.concat(child);
277 }
278 }
279 }
280 }
281
282 return ids;
283}
284
285function getOuterBindingIdentifierPaths(duplicates) {
286 return this.getBindingIdentifierPaths(duplicates, true);
287}
\No newline at end of file