From 7b676d608b48a5281fe063d419e19254b33b1cb8 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 5 Jun 2015 07:46:10 +0100 Subject: [PATCH] allow NodePaths to be returned in node removal/replacement methods and coerce them to nodes --- src/babel/traversal/path/modification.js | 2 ++ src/babel/traversal/path/replacement.js | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/src/babel/traversal/path/modification.js b/src/babel/traversal/path/modification.js index ea0bb4263d..8cc79aa229 100644 --- a/src/babel/traversal/path/modification.js +++ b/src/babel/traversal/path/modification.js @@ -140,6 +140,8 @@ export function _verifyNodeList(nodes) { throw new Error(`Node list contains a non-object node with the index of ${i}`); } else if (!node.type) { throw new Error(`Node list contains a node without a type with the index of ${i}`); + } else if (node instanceof NodePath) { + nodes[i] = node.node; } } diff --git a/src/babel/traversal/path/replacement.js b/src/babel/traversal/path/replacement.js index 24efd393a0..5aa9da0258 100644 --- a/src/babel/traversal/path/replacement.js +++ b/src/babel/traversal/path/replacement.js @@ -1,4 +1,5 @@ import codeFrame from "../../helpers/code-frame"; +import NodePath from "./index"; import traverse from "../index"; import * as t from "../../types"; import parse from "../../helpers/parse"; @@ -79,6 +80,10 @@ export function replaceWith(replacement, whateverAllowed) { 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.dangerouslyRemove()` instead"); }