Skip to content

Commit

Permalink
printer: Support \n\n while printing JSXElement children
Browse files Browse the repository at this point in the history
  • Loading branch information
JHilker committed Apr 1, 2024
1 parent 51d5c89 commit 33c78da
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,8 @@ function genericPrintNoParens(path: any, options: any, print: any) {
) {
if (/\S/.test(child.value)) {
return child.value.replace(/^\s+|\s+$/g, "");
} else if (/\n\n/.test(child.value)) {
return "\n\n";
} else if (/\n/.test(child.value)) {
return "\n";
}
Expand Down
29 changes: 29 additions & 0 deletions test/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2600,4 +2600,33 @@ describe("printer", function () {
),
);
});

it("can print JSXElement syntax with newlines", function () {
const code = ["<div>", " <span />", "", " <span />", "</div>;"].join(eol);

const ast = b.program([
b.expressionStatement(
b.jsxElement(
b.jsxOpeningElement(b.jsxIdentifier("div")),
b.jsxClosingElement(b.jsxIdentifier("div")),
[
b.jsxText("\n "),
b.jsxElement(
b.jsxOpeningElement(b.jsxIdentifier("span"), [], true),
),
b.jsxText("\n\n "),
b.jsxElement(
b.jsxOpeningElement(b.jsxIdentifier("span"), [], true),
),
b.jsxText("\n"),
],
),
),
]);

const printer = new Printer({ tabWidth: 2 });

const pretty = printer.print(ast).code;
assert.strictEqual(pretty, code);
});
});

0 comments on commit 33c78da

Please sign in to comment.