source: imaps-frontend/node_modules/@babel/traverse/lib/path/modification.js@ 0c6b92a

main
Last change on this file since 0c6b92a was 0c6b92a, checked in by stefan toskovski <stefantoska84@…>, 5 weeks ago

Pred finalna verzija

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