1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.getOpposite = getOpposite;
|
---|
7 | exports.getCompletionRecords = getCompletionRecords;
|
---|
8 | exports.getSibling = getSibling;
|
---|
9 | exports.getPrevSibling = getPrevSibling;
|
---|
10 | exports.getNextSibling = getNextSibling;
|
---|
11 | exports.getAllNextSiblings = getAllNextSiblings;
|
---|
12 | exports.getAllPrevSiblings = getAllPrevSiblings;
|
---|
13 | exports.get = get;
|
---|
14 | exports._getKey = _getKey;
|
---|
15 | exports._getPattern = _getPattern;
|
---|
16 | exports.getBindingIdentifiers = getBindingIdentifiers;
|
---|
17 | exports.getOuterBindingIdentifiers = getOuterBindingIdentifiers;
|
---|
18 | exports.getBindingIdentifierPaths = getBindingIdentifierPaths;
|
---|
19 | exports.getOuterBindingIdentifierPaths = getOuterBindingIdentifierPaths;
|
---|
20 |
|
---|
21 | var _index = require("./index");
|
---|
22 |
|
---|
23 | var _t = require("@babel/types");
|
---|
24 |
|
---|
25 | const {
|
---|
26 | getBindingIdentifiers: _getBindingIdentifiers,
|
---|
27 | getOuterBindingIdentifiers: _getOuterBindingIdentifiers,
|
---|
28 | isDeclaration,
|
---|
29 | numericLiteral,
|
---|
30 | unaryExpression
|
---|
31 | } = _t;
|
---|
32 | const NORMAL_COMPLETION = 0;
|
---|
33 | const BREAK_COMPLETION = 1;
|
---|
34 |
|
---|
35 | function NormalCompletion(path) {
|
---|
36 | return {
|
---|
37 | type: NORMAL_COMPLETION,
|
---|
38 | path
|
---|
39 | };
|
---|
40 | }
|
---|
41 |
|
---|
42 | function BreakCompletion(path) {
|
---|
43 | return {
|
---|
44 | type: BREAK_COMPLETION,
|
---|
45 | path
|
---|
46 | };
|
---|
47 | }
|
---|
48 |
|
---|
49 | function getOpposite() {
|
---|
50 | if (this.key === "left") {
|
---|
51 | return this.getSibling("right");
|
---|
52 | } else if (this.key === "right") {
|
---|
53 | return this.getSibling("left");
|
---|
54 | }
|
---|
55 |
|
---|
56 | return null;
|
---|
57 | }
|
---|
58 |
|
---|
59 | function addCompletionRecords(path, records, context) {
|
---|
60 | if (path) {
|
---|
61 | records.push(..._getCompletionRecords(path, context));
|
---|
62 | }
|
---|
63 |
|
---|
64 | return records;
|
---|
65 | }
|
---|
66 |
|
---|
67 | function completionRecordForSwitch(cases, records, context) {
|
---|
68 | let lastNormalCompletions = [];
|
---|
69 |
|
---|
70 | for (let i = 0; i < cases.length; i++) {
|
---|
71 | const casePath = cases[i];
|
---|
72 |
|
---|
73 | const caseCompletions = _getCompletionRecords(casePath, context);
|
---|
74 |
|
---|
75 | const normalCompletions = [];
|
---|
76 | const breakCompletions = [];
|
---|
77 |
|
---|
78 | for (const c of caseCompletions) {
|
---|
79 | if (c.type === NORMAL_COMPLETION) {
|
---|
80 | normalCompletions.push(c);
|
---|
81 | }
|
---|
82 |
|
---|
83 | if (c.type === BREAK_COMPLETION) {
|
---|
84 | breakCompletions.push(c);
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | if (normalCompletions.length) {
|
---|
89 | lastNormalCompletions = normalCompletions;
|
---|
90 | }
|
---|
91 |
|
---|
92 | records.push(...breakCompletions);
|
---|
93 | }
|
---|
94 |
|
---|
95 | records.push(...lastNormalCompletions);
|
---|
96 | return records;
|
---|
97 | }
|
---|
98 |
|
---|
99 | function normalCompletionToBreak(completions) {
|
---|
100 | completions.forEach(c => {
|
---|
101 | c.type = BREAK_COMPLETION;
|
---|
102 | });
|
---|
103 | }
|
---|
104 |
|
---|
105 | function replaceBreakStatementInBreakCompletion(completions, reachable) {
|
---|
106 | completions.forEach(c => {
|
---|
107 | if (c.path.isBreakStatement({
|
---|
108 | label: null
|
---|
109 | })) {
|
---|
110 | if (reachable) {
|
---|
111 | c.path.replaceWith(unaryExpression("void", numericLiteral(0)));
|
---|
112 | } else {
|
---|
113 | c.path.remove();
|
---|
114 | }
|
---|
115 | }
|
---|
116 | });
|
---|
117 | }
|
---|
118 |
|
---|
119 | function getStatementListCompletion(paths, context) {
|
---|
120 | const completions = [];
|
---|
121 |
|
---|
122 | if (context.canHaveBreak) {
|
---|
123 | let lastNormalCompletions = [];
|
---|
124 |
|
---|
125 | for (let i = 0; i < paths.length; i++) {
|
---|
126 | const path = paths[i];
|
---|
127 | const newContext = Object.assign({}, context, {
|
---|
128 | inCaseClause: false
|
---|
129 | });
|
---|
130 |
|
---|
131 | if (path.isBlockStatement() && (context.inCaseClause || context.shouldPopulateBreak)) {
|
---|
132 | newContext.shouldPopulateBreak = true;
|
---|
133 | } else {
|
---|
134 | newContext.shouldPopulateBreak = false;
|
---|
135 | }
|
---|
136 |
|
---|
137 | const statementCompletions = _getCompletionRecords(path, newContext);
|
---|
138 |
|
---|
139 | if (statementCompletions.length > 0 && statementCompletions.every(c => c.type === BREAK_COMPLETION)) {
|
---|
140 | if (lastNormalCompletions.length > 0 && statementCompletions.every(c => c.path.isBreakStatement({
|
---|
141 | label: null
|
---|
142 | }))) {
|
---|
143 | normalCompletionToBreak(lastNormalCompletions);
|
---|
144 | completions.push(...lastNormalCompletions);
|
---|
145 |
|
---|
146 | if (lastNormalCompletions.some(c => c.path.isDeclaration())) {
|
---|
147 | completions.push(...statementCompletions);
|
---|
148 | replaceBreakStatementInBreakCompletion(statementCompletions, true);
|
---|
149 | }
|
---|
150 |
|
---|
151 | replaceBreakStatementInBreakCompletion(statementCompletions, false);
|
---|
152 | } else {
|
---|
153 | completions.push(...statementCompletions);
|
---|
154 |
|
---|
155 | if (!context.shouldPopulateBreak) {
|
---|
156 | replaceBreakStatementInBreakCompletion(statementCompletions, true);
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | break;
|
---|
161 | }
|
---|
162 |
|
---|
163 | if (i === paths.length - 1) {
|
---|
164 | completions.push(...statementCompletions);
|
---|
165 | } else {
|
---|
166 | lastNormalCompletions = [];
|
---|
167 |
|
---|
168 | for (let i = 0; i < statementCompletions.length; i++) {
|
---|
169 | const c = statementCompletions[i];
|
---|
170 |
|
---|
171 | if (c.type === BREAK_COMPLETION) {
|
---|
172 | completions.push(c);
|
---|
173 | }
|
---|
174 |
|
---|
175 | if (c.type === NORMAL_COMPLETION) {
|
---|
176 | lastNormalCompletions.push(c);
|
---|
177 | }
|
---|
178 | }
|
---|
179 | }
|
---|
180 | }
|
---|
181 | } else if (paths.length) {
|
---|
182 | for (let i = paths.length - 1; i >= 0; i--) {
|
---|
183 | const pathCompletions = _getCompletionRecords(paths[i], context);
|
---|
184 |
|
---|
185 | if (pathCompletions.length > 1 || pathCompletions.length === 1 && !pathCompletions[0].path.isVariableDeclaration()) {
|
---|
186 | completions.push(...pathCompletions);
|
---|
187 | break;
|
---|
188 | }
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | return completions;
|
---|
193 | }
|
---|
194 |
|
---|
195 | function _getCompletionRecords(path, context) {
|
---|
196 | let records = [];
|
---|
197 |
|
---|
198 | if (path.isIfStatement()) {
|
---|
199 | records = addCompletionRecords(path.get("consequent"), records, context);
|
---|
200 | records = addCompletionRecords(path.get("alternate"), records, context);
|
---|
201 | } else if (path.isDoExpression() || path.isFor() || path.isWhile() || path.isLabeledStatement()) {
|
---|
202 | return addCompletionRecords(path.get("body"), records, context);
|
---|
203 | } else if (path.isProgram() || path.isBlockStatement()) {
|
---|
204 | return getStatementListCompletion(path.get("body"), context);
|
---|
205 | } else if (path.isFunction()) {
|
---|
206 | return _getCompletionRecords(path.get("body"), context);
|
---|
207 | } else if (path.isTryStatement()) {
|
---|
208 | records = addCompletionRecords(path.get("block"), records, context);
|
---|
209 | records = addCompletionRecords(path.get("handler"), records, context);
|
---|
210 | } else if (path.isCatchClause()) {
|
---|
211 | return addCompletionRecords(path.get("body"), records, context);
|
---|
212 | } else if (path.isSwitchStatement()) {
|
---|
213 | return completionRecordForSwitch(path.get("cases"), records, context);
|
---|
214 | } else if (path.isSwitchCase()) {
|
---|
215 | return getStatementListCompletion(path.get("consequent"), {
|
---|
216 | canHaveBreak: true,
|
---|
217 | shouldPopulateBreak: false,
|
---|
218 | inCaseClause: true
|
---|
219 | });
|
---|
220 | } else if (path.isBreakStatement()) {
|
---|
221 | records.push(BreakCompletion(path));
|
---|
222 | } else {
|
---|
223 | records.push(NormalCompletion(path));
|
---|
224 | }
|
---|
225 |
|
---|
226 | return records;
|
---|
227 | }
|
---|
228 |
|
---|
229 | function getCompletionRecords() {
|
---|
230 | const records = _getCompletionRecords(this, {
|
---|
231 | canHaveBreak: false,
|
---|
232 | shouldPopulateBreak: false,
|
---|
233 | inCaseClause: false
|
---|
234 | });
|
---|
235 |
|
---|
236 | return records.map(r => r.path);
|
---|
237 | }
|
---|
238 |
|
---|
239 | function getSibling(key) {
|
---|
240 | return _index.default.get({
|
---|
241 | parentPath: this.parentPath,
|
---|
242 | parent: this.parent,
|
---|
243 | container: this.container,
|
---|
244 | listKey: this.listKey,
|
---|
245 | key: key
|
---|
246 | }).setContext(this.context);
|
---|
247 | }
|
---|
248 |
|
---|
249 | function getPrevSibling() {
|
---|
250 | return this.getSibling(this.key - 1);
|
---|
251 | }
|
---|
252 |
|
---|
253 | function getNextSibling() {
|
---|
254 | return this.getSibling(this.key + 1);
|
---|
255 | }
|
---|
256 |
|
---|
257 | function getAllNextSiblings() {
|
---|
258 | let _key = this.key;
|
---|
259 | let sibling = this.getSibling(++_key);
|
---|
260 | const siblings = [];
|
---|
261 |
|
---|
262 | while (sibling.node) {
|
---|
263 | siblings.push(sibling);
|
---|
264 | sibling = this.getSibling(++_key);
|
---|
265 | }
|
---|
266 |
|
---|
267 | return siblings;
|
---|
268 | }
|
---|
269 |
|
---|
270 | function getAllPrevSiblings() {
|
---|
271 | let _key = this.key;
|
---|
272 | let sibling = this.getSibling(--_key);
|
---|
273 | const siblings = [];
|
---|
274 |
|
---|
275 | while (sibling.node) {
|
---|
276 | siblings.push(sibling);
|
---|
277 | sibling = this.getSibling(--_key);
|
---|
278 | }
|
---|
279 |
|
---|
280 | return siblings;
|
---|
281 | }
|
---|
282 |
|
---|
283 | function get(key, context = true) {
|
---|
284 | if (context === true) context = this.context;
|
---|
285 | const parts = key.split(".");
|
---|
286 |
|
---|
287 | if (parts.length === 1) {
|
---|
288 | return this._getKey(key, context);
|
---|
289 | } else {
|
---|
290 | return this._getPattern(parts, context);
|
---|
291 | }
|
---|
292 | }
|
---|
293 |
|
---|
294 | function _getKey(key, context) {
|
---|
295 | const node = this.node;
|
---|
296 | const container = node[key];
|
---|
297 |
|
---|
298 | if (Array.isArray(container)) {
|
---|
299 | return container.map((_, i) => {
|
---|
300 | return _index.default.get({
|
---|
301 | listKey: key,
|
---|
302 | parentPath: this,
|
---|
303 | parent: node,
|
---|
304 | container: container,
|
---|
305 | key: i
|
---|
306 | }).setContext(context);
|
---|
307 | });
|
---|
308 | } else {
|
---|
309 | return _index.default.get({
|
---|
310 | parentPath: this,
|
---|
311 | parent: node,
|
---|
312 | container: node,
|
---|
313 | key: key
|
---|
314 | }).setContext(context);
|
---|
315 | }
|
---|
316 | }
|
---|
317 |
|
---|
318 | function _getPattern(parts, context) {
|
---|
319 | let path = this;
|
---|
320 |
|
---|
321 | for (const part of parts) {
|
---|
322 | if (part === ".") {
|
---|
323 | path = path.parentPath;
|
---|
324 | } else {
|
---|
325 | if (Array.isArray(path)) {
|
---|
326 | path = path[part];
|
---|
327 | } else {
|
---|
328 | path = path.get(part, context);
|
---|
329 | }
|
---|
330 | }
|
---|
331 | }
|
---|
332 |
|
---|
333 | return path;
|
---|
334 | }
|
---|
335 |
|
---|
336 | function getBindingIdentifiers(duplicates) {
|
---|
337 | return _getBindingIdentifiers(this.node, duplicates);
|
---|
338 | }
|
---|
339 |
|
---|
340 | function getOuterBindingIdentifiers(duplicates) {
|
---|
341 | return _getOuterBindingIdentifiers(this.node, duplicates);
|
---|
342 | }
|
---|
343 |
|
---|
344 | function getBindingIdentifierPaths(duplicates = false, outerOnly = false) {
|
---|
345 | const path = this;
|
---|
346 | const search = [path];
|
---|
347 | const ids = Object.create(null);
|
---|
348 |
|
---|
349 | while (search.length) {
|
---|
350 | const id = search.shift();
|
---|
351 | if (!id) continue;
|
---|
352 | if (!id.node) continue;
|
---|
353 | const keys = _getBindingIdentifiers.keys[id.node.type];
|
---|
354 |
|
---|
355 | if (id.isIdentifier()) {
|
---|
356 | if (duplicates) {
|
---|
357 | const _ids = ids[id.node.name] = ids[id.node.name] || [];
|
---|
358 |
|
---|
359 | _ids.push(id);
|
---|
360 | } else {
|
---|
361 | ids[id.node.name] = id;
|
---|
362 | }
|
---|
363 |
|
---|
364 | continue;
|
---|
365 | }
|
---|
366 |
|
---|
367 | if (id.isExportDeclaration()) {
|
---|
368 | const declaration = id.get("declaration");
|
---|
369 |
|
---|
370 | if (isDeclaration(declaration)) {
|
---|
371 | search.push(declaration);
|
---|
372 | }
|
---|
373 |
|
---|
374 | continue;
|
---|
375 | }
|
---|
376 |
|
---|
377 | if (outerOnly) {
|
---|
378 | if (id.isFunctionDeclaration()) {
|
---|
379 | search.push(id.get("id"));
|
---|
380 | continue;
|
---|
381 | }
|
---|
382 |
|
---|
383 | if (id.isFunctionExpression()) {
|
---|
384 | continue;
|
---|
385 | }
|
---|
386 | }
|
---|
387 |
|
---|
388 | if (keys) {
|
---|
389 | for (let i = 0; i < keys.length; i++) {
|
---|
390 | const key = keys[i];
|
---|
391 | const child = id.get(key);
|
---|
392 |
|
---|
393 | if (Array.isArray(child)) {
|
---|
394 | search.push(...child);
|
---|
395 | } else if (child.node) {
|
---|
396 | search.push(child);
|
---|
397 | }
|
---|
398 | }
|
---|
399 | }
|
---|
400 | }
|
---|
401 |
|
---|
402 | return ids;
|
---|
403 | }
|
---|
404 |
|
---|
405 | function getOuterBindingIdentifierPaths(duplicates) {
|
---|
406 | return this.getBindingIdentifierPaths(duplicates, true);
|
---|
407 | } |
---|