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

printer: Support \n\n while printing JSXElement children #1396

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) {
Copy link

Choose a reason for hiding this comment

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

Fairly pedantic, but this won't work if there is any whitespace on the second empty line. You might consider making the regex something like /\n[ \t]*\n/

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);
});
});
Loading