Skip to content

Commit

Permalink
test case for insertBefore for jsx
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau committed May 26, 2019
1 parent a596da2 commit 7fd8eb5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/babel-traverse/src/path/modification.js
Expand Up @@ -26,7 +26,8 @@ export function insertBefore(nodes) {
} else if (
(this.isNodeType("Expression") &&
this.listKey !== "params" &&
this.listKey !== "arguments") ||
this.listKey !== "arguments" &&
!this.isJSXElement()) ||
(parentPath.isForStatement() && this.key === "init")
) {
if (this.node) nodes.push(this.node);
Expand Down
26 changes: 26 additions & 0 deletions packages/babel-traverse/test/modification.js
Expand Up @@ -116,6 +116,32 @@ describe("modification", function() {
);
});

it("returns inserted path with nested JSXElement", function() {
const ast = parse("<div><span>foo</span></div>", {
plugins: ["jsx"],
});
let path;
traverse(ast, {
Program: function(_path) {
path = _path.get("body.0");
},
JSXElement: function(path) {
const tagName = path.node.openingElement.name.name;
if (tagName !== "span") return;
path.insertBefore(
t.JSXElement(
t.JSXOpeningElement(t.JSXIdentifier("div"), [], false),
t.JSXClosingElement(t.JSXIdentifier("div")),
[],
),
);
},
});
expect(generateCode(path)).toBe(
"<div><div></div><span>foo</span></div>;",
);
});

describe("when the parent is an export declaration inserts the node before", function() {
it("the ExportNamedDeclaration", function() {
const bodyPath = getPath("export function a() {}", {
Expand Down

0 comments on commit 7fd8eb5

Please sign in to comment.