export function insertAfter(nodes) { this._assertUnremoved(); nodes = this._verifyNodeList(nodes); if (this.parentPath.isExpressionStatement() || this.parentPath.isLabeledStatement()) { return this.parentPath.insertAfter(nodes); } else if (this.isNodeType("Expression") || (this.parentPath.isForStatement() && this.key === "init")) { if (this.node) { let temp = this.scope.generateDeclaredUidIdentifier(); nodes.unshift(t.expressionStatement(t.assignmentExpression("=", temp, this.node))); nodes.push(t.expressionStatement(temp)); } this.replaceExpressionWithStatements(nodes); } else { this._maybePopFromStatements(nodes); if (Array.isArray(this.container)) { return this._containerInsertAfter(nodes); } else if (this.isStatementOrBlock()) { if (this.node) nodes.unshift(this.node); this._replaceWith(t.blockStatement(nodes)); } else { 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?"); } } return [this]; }
export function replaceWith(replacement) { this.resync(); if (this.removed) { throw new Error("You can't replace this node, we've already removed it"); } if (replacement instanceof NodePath) { replacement = replacement.node; } if (!replacement) { throw new Error("You passed `path.replaceWith()` a falsy node, use `path.remove()` instead"); } if (this.node === replacement) { return; } if (this.isProgram() && !t.isProgram(replacement)) { throw new Error("You can only replace a Program root node with another Program node"); } if (Array.isArray(replacement)) { throw new Error("Don't use `path.replaceWith()` with an array of nodes, use `path.replaceWithMultiple()`"); } if (typeof replacement === "string") { throw new Error("Don't use `path.replaceWith()` with a source string, use `path.replaceWithSourceString()`"); } // replacing a statement with an expression so wrap it in an expression statement if (this.isNodeType("Statement") && t.isExpression(replacement) && !this.canHaveVariableDeclarationOrExpression()) { replacement = t.expressionStatement(replacement); } // replacing an expression with a statement so let's explode it if (this.isNodeType("Expression") && t.isStatement(replacement)) { return this.replaceExpressionWithStatements([replacement]); } let oldNode = this.node; if (oldNode) { t.inheritsComments(replacement, oldNode); t.removeComments(oldNode); } // replace the node this._replaceWith(replacement); this.type = replacement.type; // potentially create new scope this.setScope(); // requeue for visiting this.requeue(); }
}, VariableDeclaration(path) { if (path.node.kind !== "var") return; let bindings = path.getBindingIdentifiers(); for (let key in bindings) { path.scope.push({ id: bindings[key] }); } let exprs = []; for (let declar of (path.node.declarations: Array<Object>)) { if (declar.init) { exprs.push(t.expressionStatement( t.assignmentExpression("=", declar.id, declar.init) )); } } path.replaceWithMultiple(exprs); } }; /** * Replace a node with an array of multiple. This method performs the following steps: * * - Inherit the comments of first provided node with that of the current node. * - Insert the provided nodes after the current node. * - Remove the current node. */