Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parentheses on replaceWithMultiple for JSX #10598

Merged
merged 3 commits into from Nov 11, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/babel-traverse/src/path/modification.js
Expand Up @@ -115,7 +115,9 @@ export function insertAfter(nodes) {
}),
);
} else if (
(this.isNodeType("Expression") && !this.isJSXElement()) ||
(this.isNodeType("Expression") &&
!this.isJSXElement() &&
!parentPath.isJSXElement()) ||
(parentPath.isForStatement() && this.key === "init")
) {
if (this.node) {
Expand Down
24 changes: 24 additions & 0 deletions packages/babel-traverse/test/replacement.js
@@ -1,7 +1,12 @@
import traverse from "../lib";
import { parse } from "@babel/parser";
import generate from "@babel/generator";
import * as t from "@babel/types";

function generateCode(path) {
return generate(path.parentPath.node).code;
}

describe("path/replacement", function() {
describe("replaceWith", function() {
it("replaces declaration in ExportDefaultDeclaration node", function() {
Expand Down Expand Up @@ -97,4 +102,23 @@ describe("path/replacement", function() {
);
});
});
describe("replaceWithMultiple", () => {
JLHwung marked this conversation as resolved.
Show resolved Hide resolved
it("ReplaceWithMultiple for a JSXElement with a JSXElement parent", () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it("ReplaceWithMultiple for a JSXElement with a JSXElement parent", () => {
it("does not add extra parenthesis for a JSXElement with a JSXElement parent", () => {

const ast = parse(`<div><span><p></p><h></h></span></div>`, {
plugins: ["jsx"],
});
let path;
traverse(ast, {
Program: _path => {
path = _path.get("body.0");
},
JSXElement: path => {
if (path.node.openingElement.name.name === "span") {
path.replaceWithMultiple(path.node.children.filter(t.isJSXElement));
}
},
});
expect(generateCode(path)).toBe("<div><p></p><h></h></div>;");
JLHwung marked this conversation as resolved.
Show resolved Hide resolved
});
});
});