Skip to content

Commit

Permalink
Avoid stringifying undefined in scoped class attributes (#1014)
Browse files Browse the repository at this point in the history
* Avoid stringifying `undefined` in scoped class attributes

* Add changeset
  • Loading branch information
delucis authored Jul 8, 2024
1 parent 7fcc29b commit 6b7c12f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/red-crabs-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Avoids stringifying `undefined` in scoped class attributes
2 changes: 1 addition & 1 deletion internal/transform/scope-html.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func injectScopedClass(n *astro.Node, opts TransformOptions) {
return
case astro.ExpressionAttribute:
// as an expression
attr.Val = fmt.Sprintf(`(%s) + " %s"`, attr.Val, scopedClass)
attr.Val = fmt.Sprintf(`((%s) ?? "") + " %s"`, attr.Val, scopedClass)
n.Attr[i] = attr
return
}
Expand Down
8 changes: 4 additions & 4 deletions internal/transform/scope-html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ func tests() []struct {
{
name: "expression string",
source: `<div class={"test"} />`,
want: `<div class={("test") + " astro-xxxxxx"}></div>`,
want: `<div class={(("test") ?? "") + " astro-xxxxxx"}></div>`,
},
{
name: "expression function",
source: `<div class={clsx({ [test]: true })} />`,
want: `<div class={(clsx({ [test]: true })) + " astro-xxxxxx"}></div>`,
want: `<div class={((clsx({ [test]: true })) ?? "") + " astro-xxxxxx"}></div>`,
},
{
name: "expression dynamic",
source: "<div class={condition ? 'a' : 'b'} />",
want: `<div class={(condition ? 'a' : 'b') + " astro-xxxxxx"}></div>`,
want: `<div class={((condition ? 'a' : 'b') ?? "") + " astro-xxxxxx"}></div>`,
},
{
name: "empty",
Expand All @@ -68,7 +68,7 @@ func tests() []struct {
{
name: "component className expression",
source: `<Component className={"test"} />`,
want: `<Component className={("test") + " astro-xxxxxx"}></Component>`,
want: `<Component className={(("test") ?? "") + " astro-xxxxxx"}></Component>`,
},
{
name: "component className shorthand",
Expand Down

0 comments on commit 6b7c12f

Please sign in to comment.