Skip to content

Commit

Permalink
Fix regression in expressions (#937)
Browse files Browse the repository at this point in the history
* add test

* check expressions' first child siblings are empty

* chore: changeset

* simplify changeset
  • Loading branch information
MoustaphaDev authored Jan 9, 2024
1 parent 5658fbd commit 7a07089
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-chairs-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Fixes a bug where expressions starting with whitespace, followed by anything else, weren't printed correctly.
6 changes: 1 addition & 5 deletions internal/printer/print-to-js.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions internal/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2486,6 +2486,20 @@ const items = ["Dog", "Cat", "Platipus"];
code: `${$$maybeRenderHead($$result)}<body>(${(void 0) })</body>`,
},
},
{
name: "expression with leading whitespace",
source: `<section>
<ul class="font-mono text-sm flex flex-col gap-0.5">
{
<li>Build: { new Date().toISOString() }</li>
<li>NODE_VERSION: { process.env.NODE_VERSION }</li>
}
</ul>
</section>`,
want: want{
code: "${$$maybeRenderHead($$result)}<section>\n<ul class=\"font-mono text-sm flex flex-col gap-0.5\">\n\t${\n\n\t\t$$render`<li>Build: ${ new Date().toISOString() }</li>\n\t\t<li>NODE_VERSION: ${ process.env.NODE_VERSION }</li>`\n\t}\n</ul>\n</section>",
},
},
{
name: "Empty attribute expression",
source: "<body attr={}></body>",
Expand Down

0 comments on commit 7a07089

Please sign in to comment.