[d565449] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports._containerInsert = _containerInsert;
|
---|
| 7 | exports._containerInsertAfter = _containerInsertAfter;
|
---|
| 8 | exports._containerInsertBefore = _containerInsertBefore;
|
---|
| 9 | exports._verifyNodeList = _verifyNodeList;
|
---|
| 10 | exports.insertAfter = insertAfter;
|
---|
| 11 | exports.insertBefore = insertBefore;
|
---|
| 12 | exports.pushContainer = pushContainer;
|
---|
| 13 | exports.unshiftContainer = unshiftContainer;
|
---|
| 14 | exports.updateSiblingKeys = updateSiblingKeys;
|
---|
| 15 | var _cache = require("../cache.js");
|
---|
| 16 | var _hoister = require("./lib/hoister.js");
|
---|
| 17 | var _index = require("./index.js");
|
---|
| 18 | var _context = require("./context.js");
|
---|
| 19 | var _removal = require("./removal.js");
|
---|
| 20 | var _t = require("@babel/types");
|
---|
| 21 | const {
|
---|
| 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;
|
---|
| 38 | function 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 | }
|
---|
| 61 | function _containerInsert(from, nodes) {
|
---|
[0c6b92a] | 62 | updateSiblingKeys.call(this, from, nodes.length);
|
---|
[d565449] | 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) {
|
---|
[0c6b92a] | 71 | _context.pushContext.call(path, this.context);
|
---|
[d565449] | 72 | }
|
---|
| 73 | }
|
---|
| 74 | const contexts = _context._getQueueContexts.call(this);
|
---|
| 75 | for (const path of paths) {
|
---|
[0c6b92a] | 76 | _context.setScope.call(path);
|
---|
[d565449] | 77 | path.debug("Inserted.");
|
---|
| 78 | for (const context of contexts) {
|
---|
| 79 | context.maybeQueue(path, true);
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 | return paths;
|
---|
| 83 | }
|
---|
| 84 | function _containerInsertBefore(nodes) {
|
---|
| 85 | return _containerInsert.call(this, this.key, nodes);
|
---|
| 86 | }
|
---|
| 87 | function _containerInsertAfter(nodes) {
|
---|
| 88 | return _containerInsert.call(this, this.key + 1, nodes);
|
---|
| 89 | }
|
---|
| 90 | const last = arr => arr[arr.length - 1];
|
---|
| 91 | function isHiddenInSequenceExpression(path) {
|
---|
| 92 | return isSequenceExpression(path.parent) && (last(path.parent.expressions) !== path.node || isHiddenInSequenceExpression(path.parentPath));
|
---|
| 93 | }
|
---|
| 94 | function 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 | }
|
---|
| 101 | function 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 | }
|
---|
| 162 | function updateSiblingKeys(fromIndex, incrementBy) {
|
---|
| 163 | if (!this.parent) return;
|
---|
| 164 | const paths = (0, _cache.getCachedPaths)(this.hub, this.parent) || [];
|
---|
| 165 | for (const [, path] of paths) {
|
---|
[0c6b92a] | 166 | if (typeof path.key === "number" && path.container === this.container && path.key >= fromIndex) {
|
---|
[d565449] | 167 | path.key += incrementBy;
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 | function _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 | }
|
---|
| 197 | function 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 | }
|
---|
| 209 | function 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 | }
|
---|
[0c6b92a] | 222 | {
|
---|
| 223 | exports.hoist = function hoist(scope = this.scope) {
|
---|
| 224 | const hoister = new _hoister.default(this, scope);
|
---|
| 225 | return hoister.run();
|
---|
| 226 | };
|
---|
[d565449] | 227 | }
|
---|
| 228 |
|
---|
| 229 | //# sourceMappingURL=modification.js.map
|
---|