allow NodePaths to be returned in node removal/replacement methods and coerce them to nodes

This commit is contained in:
Sebastian McKenzie 2015-06-05 07:46:10 +01:00
parent 48ecec1e2e
commit 7b676d608b
2 changed files with 7 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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");
}