source: imaps-frontend/node_modules/@babel/traverse/lib/path/modification.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: 7.8 KB
RevLine 
[d565449]1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports._containerInsert = _containerInsert;
7exports._containerInsertAfter = _containerInsertAfter;
8exports._containerInsertBefore = _containerInsertBefore;
9exports._verifyNodeList = _verifyNodeList;
10exports.hoist = hoist;
11exports.insertAfter = insertAfter;
12exports.insertBefore = insertBefore;
13exports.pushContainer = pushContainer;
14exports.unshiftContainer = unshiftContainer;
15exports.updateSiblingKeys = updateSiblingKeys;
16var _cache = require("../cache.js");
17var _hoister = require("./lib/hoister.js");
18var _index = require("./index.js");
19var _context = require("./context.js");
20var _removal = require("./removal.js");
21var _t = require("@babel/types");
22const {
23 arrowFunctionExpression,
24 assertExpression,
25 assignmentExpression,
26 blockStatement,
27 callExpression,
28 cloneNode,
29 expressionStatement,
30 isAssignmentExpression,
31 isCallExpression,
32 isExportNamedDeclaration,
33 isExpression,
34 isIdentifier,
35 isSequenceExpression,
36 isSuper,
37 thisExpression
38} = _t;
39function insertBefore(nodes_) {
40 _removal._assertUnremoved.call(this);
41 const nodes = _verifyNodeList.call(this, nodes_);
42 const {
43 parentPath,
44 parent
45 } = this;
46 if (parentPath.isExpressionStatement() || parentPath.isLabeledStatement() || isExportNamedDeclaration(parent) || parentPath.isExportDefaultDeclaration() && this.isDeclaration()) {
47 return parentPath.insertBefore(nodes);
48 } else if (this.isNodeType("Expression") && !this.isJSXElement() || parentPath.isForStatement() && this.key === "init") {
49 if (this.node) nodes.push(this.node);
50 return this.replaceExpressionWithStatements(nodes);
51 } else if (Array.isArray(this.container)) {
52 return _containerInsertBefore.call(this, nodes);
53 } else if (this.isStatementOrBlock()) {
54 const node = this.node;
55 const shouldInsertCurrentNode = node && (!this.isExpressionStatement() || node.expression != null);
56 this.replaceWith(blockStatement(shouldInsertCurrentNode ? [node] : []));
57 return this.unshiftContainer("body", nodes);
58 } else {
59 throw new Error("We don't know what to do with this node type. " + "We were previously a Statement but we can't fit in here?");
60 }
61}
62function _containerInsert(from, nodes) {
63 this.updateSiblingKeys(from, nodes.length);
64 const paths = [];
65 this.container.splice(from, 0, ...nodes);
66 for (let i = 0; i < nodes.length; i++) {
67 var _this$context;
68 const to = from + i;
69 const path = this.getSibling(to);
70 paths.push(path);
71 if ((_this$context = this.context) != null && _this$context.queue) {
72 path.pushContext(this.context);
73 }
74 }
75 const contexts = _context._getQueueContexts.call(this);
76 for (const path of paths) {
77 path.setScope();
78 path.debug("Inserted.");
79 for (const context of contexts) {
80 context.maybeQueue(path, true);
81 }
82 }
83 return paths;
84}
85function _containerInsertBefore(nodes) {
86 return _containerInsert.call(this, this.key, nodes);
87}
88function _containerInsertAfter(nodes) {
89 return _containerInsert.call(this, this.key + 1, nodes);
90}
91const last = arr => arr[arr.length - 1];
92function isHiddenInSequenceExpression(path) {
93 return isSequenceExpression(path.parent) && (last(path.parent.expressions) !== path.node || isHiddenInSequenceExpression(path.parentPath));
94}
95function isAlmostConstantAssignment(node, scope) {
96 if (!isAssignmentExpression(node) || !isIdentifier(node.left)) {
97 return false;
98 }
99 const blockScope = scope.getBlockParent();
100 return blockScope.hasOwnBinding(node.left.name) && blockScope.getOwnBinding(node.left.name).constantViolations.length <= 1;
101}
102function insertAfter(nodes_) {
103 _removal._assertUnremoved.call(this);
104 if (this.isSequenceExpression()) {
105 return last(this.get("expressions")).insertAfter(nodes_);
106 }
107 const nodes = _verifyNodeList.call(this, nodes_);
108 const {
109 parentPath,
110 parent
111 } = this;
112 if (parentPath.isExpressionStatement() || parentPath.isLabeledStatement() || isExportNamedDeclaration(parent) || parentPath.isExportDefaultDeclaration() && this.isDeclaration()) {
113 return parentPath.insertAfter(nodes.map(node => {
114 return isExpression(node) ? expressionStatement(node) : node;
115 }));
116 } else if (this.isNodeType("Expression") && !this.isJSXElement() && !parentPath.isJSXElement() || parentPath.isForStatement() && this.key === "init") {
117 const self = this;
118 if (self.node) {
119 const node = self.node;
120 let {
121 scope
122 } = this;
123 if (scope.path.isPattern()) {
124 assertExpression(node);
125 self.replaceWith(callExpression(arrowFunctionExpression([], node), []));
126 self.get("callee.body").insertAfter(nodes);
127 return [self];
128 }
129 if (isHiddenInSequenceExpression(self)) {
130 nodes.unshift(node);
131 } else if (isCallExpression(node) && isSuper(node.callee)) {
132 nodes.unshift(node);
133 nodes.push(thisExpression());
134 } else if (isAlmostConstantAssignment(node, scope)) {
135 nodes.unshift(node);
136 nodes.push(cloneNode(node.left));
137 } else if (scope.isPure(node, true)) {
138 nodes.push(node);
139 } else {
140 if (parentPath.isMethod({
141 computed: true,
142 key: node
143 })) {
144 scope = scope.parent;
145 }
146 const temp = scope.generateDeclaredUidIdentifier();
147 nodes.unshift(expressionStatement(assignmentExpression("=", cloneNode(temp), node)));
148 nodes.push(expressionStatement(cloneNode(temp)));
149 }
150 }
151 return this.replaceExpressionWithStatements(nodes);
152 } else if (Array.isArray(this.container)) {
153 return _containerInsertAfter.call(this, nodes);
154 } else if (this.isStatementOrBlock()) {
155 const node = this.node;
156 const shouldInsertCurrentNode = node && (!this.isExpressionStatement() || node.expression != null);
157 this.replaceWith(blockStatement(shouldInsertCurrentNode ? [node] : []));
158 return this.pushContainer("body", nodes);
159 } else {
160 throw new Error("We don't know what to do with this node type. " + "We were previously a Statement but we can't fit in here?");
161 }
162}
163function updateSiblingKeys(fromIndex, incrementBy) {
164 if (!this.parent) return;
165 const paths = (0, _cache.getCachedPaths)(this.hub, this.parent) || [];
166 for (const [, path] of paths) {
167 if (typeof path.key === "number" && path.key >= fromIndex) {
168 path.key += incrementBy;
169 }
170 }
171}
172function _verifyNodeList(nodes) {
173 if (!nodes) {
174 return [];
175 }
176 if (!Array.isArray(nodes)) {
177 nodes = [nodes];
178 }
179 for (let i = 0; i < nodes.length; i++) {
180 const node = nodes[i];
181 let msg;
182 if (!node) {
183 msg = "has falsy node";
184 } else if (typeof node !== "object") {
185 msg = "contains a non-object node";
186 } else if (!node.type) {
187 msg = "without a type";
188 } else if (node instanceof _index.default) {
189 msg = "has a NodePath when it expected a raw object";
190 }
191 if (msg) {
192 const type = Array.isArray(node) ? "array" : typeof node;
193 throw new Error(`Node list ${msg} with the index of ${i} and type of ${type}`);
194 }
195 }
196 return nodes;
197}
198function unshiftContainer(listKey, nodes) {
199 _removal._assertUnremoved.call(this);
200 nodes = _verifyNodeList.call(this, nodes);
201 const path = _index.default.get({
202 parentPath: this,
203 parent: this.node,
204 container: this.node[listKey],
205 listKey,
206 key: 0
207 }).setContext(this.context);
208 return _containerInsertBefore.call(path, nodes);
209}
210function pushContainer(listKey, nodes) {
211 _removal._assertUnremoved.call(this);
212 const verifiedNodes = _verifyNodeList.call(this, nodes);
213 const container = this.node[listKey];
214 const path = _index.default.get({
215 parentPath: this,
216 parent: this.node,
217 container: container,
218 listKey,
219 key: container.length
220 }).setContext(this.context);
221 return path.replaceWithMultiple(verifiedNodes);
222}
223function hoist(scope = this.scope) {
224 const hoister = new _hoister.default(this, scope);
225 return hoister.run();
226}
227
228//# sourceMappingURL=modification.js.map
Note: See TracBrowser for help on using the repository browser.