From 33c78da3629526b30dba17546217464bd9b6c625 Mon Sep 17 00:00:00 2001 From: Jacob Hilker Date: Mon, 1 Apr 2024 18:50:06 -0400 Subject: [PATCH] printer: Support \n\n while printing JSXElement children --- lib/printer.ts | 2 ++ test/printer.ts | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/printer.ts b/lib/printer.ts index 5b3f9618..088d1295 100644 --- a/lib/printer.ts +++ b/lib/printer.ts @@ -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"; } diff --git a/test/printer.ts b/test/printer.ts index e25a961d..a913e62d 100644 --- a/test/printer.ts +++ b/test/printer.ts @@ -2600,4 +2600,33 @@ describe("printer", function () { ), ); }); + + it("can print JSXElement syntax with newlines", function () { + const code = ["
", " ", "", " ", "
;"].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); + }); });