From 7a070895d7bf7f1e6607bb8d5d815664587096ca Mon Sep 17 00:00:00 2001 From: Happydev <81974850+MoustaphaDev@users.noreply.github.com> Date: Tue, 9 Jan 2024 14:48:18 +0000 Subject: [PATCH] Fix regression in expressions (#937) * add test * check expressions' first child siblings are empty * chore: changeset * simplify changeset --- .changeset/strong-chairs-walk.md | 5 +++++ internal/printer/print-to-js.go | 6 +----- internal/printer/printer_test.go | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 .changeset/strong-chairs-walk.md diff --git a/.changeset/strong-chairs-walk.md b/.changeset/strong-chairs-walk.md new file mode 100644 index 000000000..57d443431 --- /dev/null +++ b/.changeset/strong-chairs-walk.md @@ -0,0 +1,5 @@ +--- +'@astrojs/compiler': patch +--- + +Fixes a bug where expressions starting with whitespace, followed by anything else, weren't printed correctly. diff --git a/internal/printer/print-to-js.go b/internal/printer/print-to-js.go index 414510539..c43b0d8b2 100644 --- a/internal/printer/print-to-js.go +++ b/internal/printer/print-to-js.go @@ -316,11 +316,7 @@ func render1(p *printer, n *Node, opts RenderOptions) { // Tip! Comment this block out to debug expressions if n.Expression { - clean := "" - if n.FirstChild != nil { - clean = strings.TrimSpace(n.FirstChild.Data) - } - if n.FirstChild == nil || clean == "" { + if n.FirstChild == nil || emptyTextNodeWithoutSiblings(n.FirstChild) { p.print("${(void 0)") } else if expressionOnlyHasComment(n) { // we do not print expressions that only contain comment blocks diff --git a/internal/printer/printer_test.go b/internal/printer/printer_test.go index f43b34da3..b5380effa 100644 --- a/internal/printer/printer_test.go +++ b/internal/printer/printer_test.go @@ -2486,6 +2486,20 @@ const items = ["Dog", "Cat", "Platipus"]; code: `${$$maybeRenderHead($$result)}(${(void 0) })`, }, }, + { + name: "expression with leading whitespace", + source: `
+ +
`, + want: want{ + code: "${$$maybeRenderHead($$result)}
\n\n
", + }, + }, { name: "Empty attribute expression", source: "",